I'm writing a menu where the users can choose, any options will make them input a string, after that the program will ask if they want to continue or not, if the users input y then it will continue, otherwise it will return back to the main menu so the users can choose another options.
Until now, I have successfully make the user continue by input y, but I don't know how to make the program return to the main menu if the users input n so they can choose other options. Can you guys help me with this problem ? Thank you
Thank you, I want to ask what is the differrence between the line of code while( str.empty() ) std::getline( std::cin, str ) ; and cin.ignore() ? Which one is better ?
Either should work in this case; but they are somewhat different.
std::cin.ignore( 1'000'000, '\n' ) extracts and discards the rest of the current input line.
If the next line is an empty line, std::getline( std::cin, str ) ; would accept that.
while( str.empty() ) std::getline( std::cin, str ) ; repeats the input till a non-empty line is entered.
If the current line has some extra characters before the new line, it would accept that.
If we want, we can combine the two:
1 2
std::cin.ignore( 1'000'000, '\n' ) ; // extract and discard the rest of the current input line.
while( str.empty() ) std::getline( std::cin, str ) ; // repeat till a non-empty line is entered