0

I have this program that gets the choice of the user. After that, the user needs to enter his name. I tried using cin>>nameInput because I'm just entering one name. But I wanted to enter a full name so I changed it to getline(cin,nameInput); and now the program skips the first prompt.

Here is the part of the code:

void menu(){
    char choice;
    string nameInput,inTime,outTime;
    while(true){
        cout << "======================\n";
        cout << "|   [1] Log in       |\n";
        cout << "|   [2] Log out      |\n";
        cout << "|   [3] View Records |\n";
        cout << "|   [0] Exit         |\n";
        cout << "|____________________|\n";
        cout << "Choice: ";
        cin >> choice;
        cin.clear();
        cout << endl;
        switch(choice){
            case '1':
                cout << "Enter name: ";
                cin >> nameInput;
                cout << "Time in: ";
                cin >> inTime;
                in(nameInput,inTime);
                system("cls");
                break;

I have tried using cin.clear(), but it still won't work.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
newbie
  • 1
  • 1
  • Basic rule: If the user is expected to press the enter key after some input, then use `getline`. Basic rules of posting a question where your program doesn't work is to show the _actual input_ you gave it, the _actual output_ it delivered, and the _expected output_ for a program that works the way you want. – paddy Dec 10 '20 at 02:00
  • 1
    You did not show the code for `in()`, but I suspect you are encountering this issue: [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/) – Remy Lebeau Dec 10 '20 at 02:02
  • `char choice; cin >> choice;` is almost definitely to blame here. – paddy Dec 10 '20 at 02:03
  • Your code works correctly, check what you are typing at `cin >> choice;` – Victor Dec 10 '20 at 03:23

0 Answers0