c++ - How to allow user to do input more than twice? -
i new c++. trying understand how utilize c++ common input (cin). trying write program checks amount of characters of sentence , amount of vowels in input sentence. have done it, problem happens when try code run through 1 more time. when runs through 1 more time, doesn't allow second input anymore. simplified code below.
#include <iostream> #include <string> using namespace std; int main() { char rerun = 'y'; string input; int a_counter, e_counter, i_counter, o_counter, u_counter; a_counter = e_counter = i_counter = o_counter = u_counter = 0; { getline(cin, input); // asking user input sentence // written code here uses loop vowel counting // written code use cout command output result cin >> rerun; // ask type 'y' or 'n' continue, assume user types y or n } while (rerun == 'y'); } //end of main function when running program, @ first user allowed input sentence and, after input , result displayed, user asked put in 'y' or 'n'. if answer y, code not allow put in sentence (where getline is) , display result of (a_counter...) 0 , jump straight requesting put 'y' or 'n'. can me? appreciated.
when line
cin >> rerun; is executed, '\n' left in input stream. next time run
getline(cin, input); you empty line.
to fix problem, add line
cin.ignore(); right after
cin >> rerun; here's loop should like:
do { getline(cin, input); cin >> rerun; cin.ignore(); }while (rerun == 'y');
Comments
Post a Comment