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();
}
}
You cant really call you main like that... you will have a recursive error. You should try this :
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)
.