0

Ok, so I'm trying to write a main where it will ask the user to enter a number 1 through 6 and if the number is 6 it will end the program. if it is higher than 6, it will ask to re-enter the number. The thing is, when I run it, it doesn't check the "if" statements and automatically goes to this line "please enter another option"

any thoughts why my program would do something like this?

UPDATE: I'm saying that it automatically skips all of the if statements and asks the last question in the while loop.

int main()
{
    int userChoice = 0;

    print(); //printing all of the options.

    cout << "Please enter one of the options listed below" <<endl;
    cin >> userChoice;

    while(userChoice != 6)// 6 = the user wishing the end the program when they press 6.
    {
        if(userChoice == 1) //adding integer to the front of the list
        {
            addValueFront();
        }
        else if(userChoice == 2)//adding integer to the back of the list
        {
            addValueBack();
        }
        else if(userChoice == 3)//removing from the list
        {
            int n = 0;
            cout << "Please enter the integer you wish to remove" << endl;
            cin >> n;
            removeValue(n);
        }
        else if(userChoice == 4)//printing the list
        {
            printList();
        }
        else if(userChoice == 5)//printing the number of items from the list
        {
            printItem();
        }

        else
        {
            cout << "The number you have entered is too high. Please try again" << endl;
            cin >> userChoice;
        }
        cout << "please enter another option" <<endl;

        cin >> userChoice; //sets up which option the user can choose from.
    }
}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
booky99
  • 1,436
  • 4
  • 27
  • 45

4 Answers4

2

Add a "continue" to the end of your else block.

cout << "The number you have entered is too high. Please try again" << endl;
cin >> userChoice;
continue;
user1052842
  • 121
  • 1
  • 11
  • +1 This has to be the most minimal change to address the OP's issue (the code has other issues, but this one line will address what I think he's asking about in this question). – WhozCraig Mar 02 '13 at 04:48
1

I think the following program is what you want:

int main()                                                                     
{                                                                              
  int userChoice = 0;                                                         

  print(); //printing all of the options.                                      

  cout << "Please enter one of the options listed below" <<endl;                                                                                                  
  do // 6 = the user wishing the end the program when they press 6.                  
  {                                                                            
    cin >> userChoice;
    if(userChoice > 6)                                                      
    {                                                                       
      cout << "The number you have entered is too high. Please try again" << endl;
      cout << "please enter another option" <<endl;                         
    }                                                                       
    else if(userChoice == 1) //adding integer to the front of the list         
    {                                                                          
      addValueFront();                                                         
    }                                                                          
    else if(userChoice == 2)//adding integer to the back of the list           
    {
      addValueBack();                                                          
    }
    else if(userChoice == 3)//removing from the list                           
    {
      int n = 0;                                                               
      cout << "Please enter the integer you wish to remove" << endl;           
      cin >> n;                                                                
      removeValue(n);                                                          
    }                                                                          
    else if(userChoice == 4)//printing the list                                
    {                                                                          
      printList();                                                             
    }                                                                          
    else if(userChoice == 5)//printing the number of items from the list       
    {                                                                          
      printItem();                                                             
    }                                                                          
  } while(userChoice != 6);                                                    
}                                                                            
fLOyd
  • 385
  • 1
  • 2
  • 9
0

Please refer to the answers for this question and this question. You need to call cin.clear() and cin.ignore() before every cin<<, to flush the keyboard buffers so that it behaves properly and consistently.

In addition, you should remove the cin from the else block because it's logically incorrect.

Community
  • 1
  • 1
ruben2020
  • 1,549
  • 14
  • 24
0

Use switch it would be the better option.

int main()
{
    int userChoice = 0;

    print(); //printing all of the options.

    cout << "Please enter one of the options listed below" <<endl;
    cin >> userChoice;

    while(1)// 6 = the user wishing the end the program when they press 6.
    {
        if(userChoice == 1) //adding integer to the front of the list
        {
            addValueFront();
        }
        else if(userChoice == 2)//adding integer to the back of the list
        {
            addValueBack();
        }
        else if(userChoice == 3)//removing from the list
        {
            int n = 0;
            cout << "Please enter the integer you wish to remove" << endl;
            cin >> n;
            removeValue(n);
        }
        else if(userChoice == 4)//printing the list
        {
            printList();
        }
        else if(userChoice == 5)//printing the number of items from the list
        {
            printItem();
        }
        else if(userChoice == 6)// 6 = the user wishing the end the program when they press 6.
        {
            return 0;
        }
        else
        {
            cout << "The number you have entered is too high. Please try again" << endl;
            cin >> userChoice;
        }
    }
}
Saurabh B
  • 165
  • 10