![]() Std::string filename = username + ".txt" void save_user(const std::string &username, const std::string &password) It does require passing both the username and the password though. This one is pretty similar to your writetofile() function, except it is easier to read. Now we need a function that can save the username and password. Password2 = get_input ("Please re-enter your password.") Std::string password2 = get_input ("Please re-enter your password.") Std::cout ("Please enter your password.") Username = get_input ("Please enter a username.") "Hello, Would you like to log in or register?" "\n" All other cases result in nothing being done. Notice that we only need two switch cases. Using this strategy make your main_menu() class easier to read. Even if the string literals are on separate lines, as long as there are no semicolons or anything else between them, then the compiler will concatenate them. Notice that in C++, two string literals next to each other (such as " Login" "\n") are automatically concatenated into one string literal by the compiler. Second we will use our new get_input() function. Std::cin.ignore(std::numeric_limits ::max(), '\n') įirst we will rename mainmenu() to main_menu(). If bad input is given to this function, then it asks the user to try again. It can return ints, std::strings, and other variable types. One thing we can do is create a helper function that can read user input for various values. We want to get rid of the ugly recursion that we have going on. Now with that out of the way, let's see how we can rewrite this program. While cin.ignore(10000,'\n') should work in most cases, std::cin.ignore(std::numeric_limits ::max(), '\n') will work in more cases. This is one of the reasons why limiting the scope of your variables is important. The std::ofstream will automatically clean up its own resources once it goes out of scope. In your writetofile function, you manually call writefile.close(). It is one of the most important aspects of the C++ language. This program can be rewritten to use no recursion at all. Unfortunately, it's very ugly in this situation. Recursion is very nice an clean in some situations. ![]() mainmenu() should be either main_menu() or MainMenu(), writetofile() should be either write_to_file() or WriteToFile(), etc. This program can be rewritten to use no global variable at all. You want to limit the scope of your variables as much as possible. See this link for more information.Īvoid using global variables when they are not necessary. Putting it into a CPP file is not as bad as putting it into a header, but the same dangers exist. If you put using namespace std into a header, you pollute the global namespace of every user that uses your header. You are polluting the global namespace and increasing the probability of naming clashes. You could actually remove this header altogether because you really should not be calling exit() in this program.Īvoid using namespace std. Could someone have a look through and give me some constructive criticism on what should I change or keep the same? #include ![]() I've got my fourth "software" under development. I've recently stared learning C++ so I'm new to all of it.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |