I'm new to this website as I've just begun to take C++ courses. Anyways, I have a few problems I would like to ask for some opinions about.
First off, I have to write a program that remembers a user's input. I have decided to use a while loop, something like:
while (...)
{
if (...)
else if (...)
else if (...)
else \\ here's my question - how to continue correctly
{
cout << "Invalid input. Continue (y/n)?" << endl;
cin >> validity \\already defined
if (validity == 'n' || validity == 'N')
return 0;
if (validity == 'y' || validity == 'Y')
return main ();
}
It works crudely, but my problem is that if I use return main(), my previous inputs will be forgotten. e.g. if the user wants the program to remember the numbers 2, 5, 9, 11 and then types an incorrect number, after validity == 'y', 2, 5, 9, 11 are gone. Is there another way I can use a continue function that remembers my previous inputs?
Also, does anyone know what to use if I don't want to allow the user to input the same number twice? e.g. if the user wants the program to remember the numbers 2, 5, 9 , 11 and s/he types 5 again, is there a way I can use the same "Invalid input, continue (y/n)?" ?
Never call main() from anywhere inside your program. A function that calls itself is called recursive. That is sometimes useful, but the main() function should never be called recursively.
What you should do depends on the control condition of your while loop (what's inside the parentheses). Where you have written "return 0" and "return main()", you should instead make the control condition false. For example:
Ohhh, thanks!!! I got it to work by replacing return main() with my cin >> ... and it works now! :D. Can I ask, however, if you know how I can not allow the same input to be written twice?