mardi 26 mars 2019

issue with validating user input

I'm having problems with the user input of my system. the user is only allowed to enter a number between 0-6, anything else show warrant an error message, but for some reason it doesn't.

I first tried adding if statements within the switch cases but that didn't work.

I expect the user to be greeted with an error messaged telling them this input (then what they typed) is wrong, when they type any thing expect 1 or 2 but all it does is crash the system or continue looping the menu.

snippet of code.

    void Homescreen(void) { 

    string CString;

    string improvedString; 

    int menuOp; 

    do {

      cout << "\n enter an option (1 - 2): ";

     if (menuop== "1" ||  menuOp == "2")

    {
     cin >> menuOp;

      } 
     else if { (!(menuop== "1" ||  menuOp == "2")){

      cout << "error your input << menu op << "is not valid try again" 
     <<endl; 
       }
       switch(menuOp) {

      case 1: 

      cString = retrieveword();

       break;

           case 2:
            improvedString = ReverseWord(cString);

        break;

        while(true);
          }}

I expect the user to be greeted with an error message showing them what what they inputted along with the message "is wrong " but it either loops back to the menu, crashes the system or just fails.

Aucun commentaire:

Enregistrer un commentaire