2

Here mainopp() is my primary menu function for a program.

Everytime I enter some value other than 1/2/3/4 it shows the error dialog, takes a ch input (due to the getch()) but then instead of going back and re-running the same function, it somehow skips the part where 'cin>>c' is mentioned, and instead goes into an infinite loop with even an incorrect output. It just shows the menu and the error dialog with strange alignment. Keeps repeating clrscr(), the menu, and the statement.

I ensured that that is the line being skipped using the delay() function. I also tried putting the call for mainopp() outside switch case, and that didn't work either.

Then I experimented using char c instead of int c, and put the cases in switch case function in single quotes (') and found it works perfectly as intended. Then I alternated between cin>>c , c=getch() , c=getche() and found it all works correctly.

The only problem I get is when i use int data type for c instead of char. Can anyone please explain why i get an error when i use int data type?

(the prototypes for agentinfo() , update() and credits() are mentioned in my code above this, and work perfectly as intended.)

This is the function :


void mainopp()
{
    int c;
    cout<<endl<<endl<<endl<<"\t \t \t \tTHE AGENCY";
    cout<<endl<<endl<<"\t \t \t    1.AGENT INFORMATION \n";
    cout<<"\t \t \t    2.UPDATE RECORDS \n";
    cout<<"\t \t \t    3.CREDITS \n";
    cout<<"\t \t \t    4.EXIT \n\n";
    cout<<"\t \t \t    Enter your choice: ";
    cin>>c;
    switch(c)
    {   case 1 :agentinfo();
            break;
        case 2 :update();
            break;
        case 3 :credits();
            break;
        case 4 :exit(1);
        default:cout<<"\t \t \tWrong selection !! Enter Again";
            getch();
            clrscr();
            mainopp();
    }
}
NSachdeva
  • 51
  • 5

1 Answers1

1

You cant really call you main like that... you will have a recursive error. You should try this :

void mainopp()
{
int c;
bool Valid = false;
while(!Valid)
{
cout<<endl<<endl<<endl<<"\t \t \t \tTHE AGENCY";
cout<<endl<<endl<<"\t \t \t    1.AGENT INFORMATION \n";
cout<<"\t \t \t    2.UPDATE RECORDS \n";
cout<<"\t \t \t    3.CREDITS \n";
cout<<"\t \t \t    4.EXIT \n\n";
cout<<"\t \t \t    Enter your choice: ";
cin>>c;
Valid = true;
switch(c)
{   case 1 :agentinfo();
        break;
    case 2 :update();
        break;
    case 3 :credits();
        break;
    case 4 :exit(1);
    default:cout<<"\t \t \tWrong selection !! Enter Again";
        clrscr();
        Valid = false;
}
}
}

This example will loop until the user entered a valid choice. If you want run it until the user choose to quit, add another choice (another case) and change the bool in this option. For infinite loop, simply use while(1).

MacGruber
  • 162
  • 2
  • 13
  • @Michael yes I was recursively calling the function, which somehow works for a character object but not an integer object for some reason, I wanted to know abot this anomaly :/ Anyway, Thank you. :D – NSachdeva Nov 06 '15 at 16:54
  • thank you for the code. but my code does work, only coming up short with the integer object c somehow. otherwise it works perfectly as I needed. Could you also please mention why i am getting an error only with integer data type? :/ Anyway, Thank you. :D – NSachdeva Nov 06 '15 at 16:54
  • Doing a long time i havent done c++ tbh. Maybe cin>> register as string or char, so you try to force a string (or char) into a int and that might cause some error? Try with a float and see if the error happen again. – MacGruber Nov 06 '15 at 18:30