Hi, i am new in c++. I am doing an assignment where I need a menu for user to select the operation.

My menu coding is as below.

int main()
{
    myCarSystem b;
    char ch;
    string temp1, temp2;
    double temp3, temp4;
    while(1)
    {
       cout<<endl<<endl;
       cout<<" My Car System Operations "<<endl;
       cout<<" ----------------------------- "<<endl;
       cout<<" 1. Insertion/Creation "<<endl;
       cout<<" 2. In-Order Traversal "<<endl;
       cout<<" 3. Removal "<<endl;
       cout<<" 4. Exit "<<endl;
       cout<<" Enter your choice : ";
       cin>>ch;
       switch(ch)
       {
           case 1 : cout<<" Enter car model to be inserted : " <<endl;
                    fflush(stdin);
                    getline(cin, temp1);
                    cout<<"\n Enter car colour type : "<<endl;
                    fflush(stdin);
                    getline(cin, temp2);
                    cout<<"\n Enter car price : "<<endl;
                    fflush(stdin);
                    cin>>temp3;
                    cout<<"\n Enter car deposit : "<<endl;
                    fflush(stdin);
                    cin>>temp4;
                    b.insert(temp1, temp2, temp3, temp4);
                    break;
           case 2 : cout<<endl;
                    cout<<" In-Order Traversal "<<endl;
                    cout<<" -------------------"<<endl;
                    b.print_inorder();
                    break;
           case 3 : cout<<" Enter data to be deleted : ";
                    cin>>temp1;
                    b.remove(temp1);
                    break;
           case 4 : system("pause");
                    return 0;
                    break;
       }
    }

    return 0;
}

My problem is, when user is selects the operation, like example user selects 1 to enter car information, the screen will jump back to the menu and skip the input information part.
please help me to check this problem.
Thank you.
Cjjack88.

Thank you for reply.

I add cin.ignore();, but the problem still occur.

int main()
{
    myCarSystem b;
    char ch;
    string temp1, temp2;
    double temp3, temp4;
    while(1)
    {
       cout<<endl<<endl;
       cout<<" My Car System Operations "<<endl;
       cout<<" ----------------------------- "<<endl;
       cout<<" 1. Insertion/Creation "<<endl;
       cout<<" 2. In-Order Traversal "<<endl;
       cout<<" 3. Removal "<<endl;
       cout<<" 4. Exit "<<endl;
       cout<<" Enter your choice : ";
       cin>>ch;
       switch(ch)
       {
           case 1 : cout<<" Enter car model to be inserted : " <<endl;
                    fflush(stdin);
                    cin.ignore(256, ' ');
                    getline(cin, temp1);
                    cout<<"\n Enter car colour type : "<<endl;
                    fflush(stdin);
                    cin.ignore(256, ' ');
                    getline(cin, temp2);
                    cout<<"\n Enter car price : "<<endl;
                    fflush(stdin);
                    cin.ignore();
                    cin>>temp3;
                    cout<<"\n Enter car deposit : "<<endl;
                    fflush(stdin);
                    cin.ignore();
                    cin>>temp4;
                    b.insert(temp1, temp2, temp3, temp4);
                    break;
           case 2 : cout<<endl;
                    cout<<" In-Order Traversal "<<endl;
                    cout<<" -------------------"<<endl;
                    b.print_inorder();
                    break;
           case 3 : cout<<" Enter data to be deleted : ";
                    cin>>temp1;
                    b.remove(temp1);
                    break;
           case 4 : system("pause");
                    return 0;
                    break;
       }
    }

    return 0;
}

Try adding a cin.ignore(); statement before each of your inputs.

See this: http://www.cplusplus.com/reference/iostream/istream/ignore/

Incidentally, its better you don't use fflush(stdin); . See here for more info.

int main()
{
    myCarSystem b;
    char ch;
    string temp1, temp2;
    double temp3, temp4;
    while(1)
    {
       cout<<endl<<endl;
       cout<<" My Car System Operations "<<endl;
       cout<<" ----------------------------- "<<endl;
       cout<<" 1. Insertion/Creation "<<endl;
       cout<<" 2. In-Order Traversal "<<endl;
       cout<<" 3. Removal "<<endl;
       cout<<" 4. Exit "<<endl;
       cout<<" Enter your choice : ";
       cin>>ch;
       switch(ch)
       {
           case 1 : cout<<" Enter car model to be inserted : " <<endl;
                    cin.ignore();
                    getline(cin, temp1);
                    
                    cout<<"\n Enter car colour type : "<<endl;
                    cin.ignore();
                    getline(cin, temp2);
                    
                    cout<<"\n Enter car price : "<<endl;
                    cin.ignore();
                    cin>>temp3;
                    
                    cout<<"\n Enter car deposit : "<<endl;
                    cin.ignore();
                    cin>>temp4;
                    
                    b.insert(temp1, temp2, temp3, temp4);
                    break;
           case 2 : cout<<endl;
                    cout<<" In-Order Traversal "<<endl;
                    cout<<" -------------------"<<endl;
                    b.print_inorder();
                    break;
           case 3 : cout<<" Enter data to be deleted : ";
                    cin>>temp1;
                    b.remove(temp1);
                    break;
           case 4 : system("pause");
                    return 0;
                    break;
       }
    }

    return 0;
}

The problem still same.
Anyway thank you for the information.

If cin.ignore(); fails, then try clearing the input stream, before accepting input. This can be done with cin.clear(); .

If all your inputs are failing, then maybe try the following:

1. Clear the input stream.
2. Read the rest of the characters in the stream into a temporary string.
3. Accept the user's input

So, for example, the statements to do this might be:

cin.clear();
        string rubbish;
        cin >> rubbish;

Before worrying about big things please pay some attention towards small things...
There is a lot of difference between 1 and '1' .
The input you are taking is a character so the cases of switch should be as case '1' : case '2' : and all , else make the input to be taken to a integer rather than a character.

And ya usage of fflush(stdin) is bad. One more point of concern is all the inputs you are taking doesn't actually need a space and hence you don't even need to use getline().You can directly use cin >> temp1; 4 posts without even concentrating on the main problem.This way you will add all the statements in this world but still wont be able to take the inputs.

Edited 7 Years Ago by csurfer: n/a

Comments
Well Spotted! Man am I blind!!

I found where is the problem. I did a careless mistake.
amrith92 , thank you for your help and informations.

Before worrying about big things please pay some attention towards small things...
There is a lot of difference between 1 and '1' .
The input you are taking is a character so the cases of switch should be as case '1' case '2' and all , else make the input to be taken to a integer rather than a character.

And ya usage of fflush(stdin) is bad. One more point of concern is all the inputs you are taking doesn't actually need a space and hence you don't even need to use getline().You can directly use cin >> temp1;

Thank you for reply. I did a careless mistake.

This question has already been answered. Start a new discussion instead.