c++ - Can getline() be skipped w/o user input? -


this question has answer here:

edit

the issue was using cin >> @ point in program, , there trailing newline in stream buffer.


so main question getline(), in order put perspective have see code first. odd reason, when run program, runs through loop fine first time. yet second time skips getline(cin, inputmenu) statment. , yes know very basic program, , know there no other errors i've tested literally every other aspect of it. there getline() don't know?

while (1)    {       // reset input each loop       inputmenu = "abc";        // menu       cout << "menu\n  p (purchase)\n  s (shut down)" << "\n\n  decide: ";        /* put if statement test, make sure runs getline.          odd reason when run (see run below)*/       if(1)       getline(cin, inputmenu);        //blah blah other stuff if enter p or s. (not infinite  loop)           cout << "\nyou earned " << inputyogurt << " stamps!"             << " bringing grand total " << numstamps << "!\n" << endl;       }    }   ---------------------- run -------------------- menu   p (purchase)   s (shut down)    decide: p  how many yogurts buy? 3  earned 3 stamps! bringing grand total 3!   menu   p (purchase)   s (shut down)    decide: menu     <<<<<<<<<<<<<<<<<<<<<<<<<< thats error   p (purchase)   s (shut down)    decide:  ------------------------------------------------------- 

it skips getline() statement , runs loop again. maybe don't understand getline() enough, because seems issue. thought when use getline, must wait user input, wrong?

it happens. way deal call cin.get() before cin.getline(). there method call cin.flush() before cin.getline(), may not work.

while (1)    {       // reset input each loop       inputmenu = "abc";        // menu       cout << "menu\n  p (purchase)\n  s (shut down)" << "\n\n  decide: ";        /* put if statement test, make sure runs getline.          odd reason when run (see run below)*/         cin.get(); // add input       getline(cin, inputmenu);        //blah blah other stuff if enter p or s.           cout << "\nyou earned " << inputyogurt << " stamps!"             << " bringing grand total " << numstamps << "!\n" << endl;       }    } 

or try use

cin.ignore(1000, '\n');  getline(cin, inputmenu); 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -