0

My c++ program is mentioned below.

int choice;
while(choice!=2){
    cin>>choice;
}
string name;
string pass;
cout<<"Username: ";
getline(cin, name);
cout<<"Password: ";
getline(cin, pass);

I want my program to work like until I enter 2, it keeps on asking me input number. And when I enter 2 it prompts me for username and password. Starting program is working fine but the problem is that I have used getline function to take string input but it is asking separate input for username and password. It is only asking input in one line like USERNAME: PASWORD: OUTPUT:

OUTPUT

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Adnan Khan
  • 43
  • 7
  • `while(choice!=2)` is undefined behavior as `choice` is uninitialized. Also see [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – Jason Dec 13 '22 at 09:41
  • When you type `2`, `cin>>choice;` reads the `2`, leaving the `` in the buffer and `getline(cin, name);` will read this. – mch Dec 13 '22 at 09:43
  • Offtopic: https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question – Marek R Dec 13 '22 at 11:03

0 Answers0