how im gonna loop menu if option not 1 or 2 ???

void display( )
{  
	int option;
	system("cls"); //clear screen
	cout<<endl<<endl;
	cout<<"\t  * Display Menu *\n";
	cout<<"\n\t1. Display For Certein Date"<<endl; 
	cout<<"\t2. Display All Appointments"<<endl;
	cout<<"\nEnter Option Number: ";  //Enter option
	cin>>option;
	cin.get();

	switch ( option ) 
	  {
		 case 1: displayCerteinDate ( );
			     break;
		 case 2: displayAll ( );
		             break;
	     default: cout<<endl<<"Wrong option number!! Try again\n";
	              cin.get();         //give the user a chance to read the output data
	  }
}

Recommended Answers

All 19 Replies

Put

cout<<endl<<"Wrong option number!! Try again\n";
cin.get(); //give the user a chance to read the output data

in a do-while -loop and change cin.get(); to cin>>option; like this:

do {
    cout<<endl<<"Wrong option number!! Try again\n";
    cin>>option;
} while(option<1 || option>2);
while ( cin>>option ) {
    switch ( option )
    {
    }
}
while ( cin>>option ) {
    switch ( option )
    {
    }
}

You have to take into account that the loop won't end automatically except if the user enters some text ...

actually u can use a flag......(its just a variable but commonly know as flag when used in such purposes )

void display( )
{  
	int option, int flag =1;
	system("cls"); //clear screen
	cout<<endl<<endl;
	cout<<"\t  * Display Menu *\n";
	cout<<"\n\t1. Display For Certein Date"<<endl; 
	cout<<"\t2. Display All Appointments"<<endl;
	cout<<"\t3.  exit"<<endl;
	cout<<"\nEnter Option Number: ";  //Enter option
	cin>>option;
	cin.get();
    while(flag)
	{   
        switch ( option ) 
        {
            case 1: displayCerteinDate ( );
                    break;
            case 2: displayAll ( );
		             break;
            case 3: flag =0;
                     break;
            default: cout<<endl<<"Wrong option number!! Try again\n";
                    cin.get();         //give the user a chance to read the output data
	  }
	}

}

i have intialised a variable flag to 1 . and put the whole swicth inside a while loop which check for the status of the flag.
i have included an exit option in the menu . so as soon as the user presses 3. the flag =0 and the while condition fails , eventually u will come out of the loop and program ends

commented: That's not real programming +0

actually u can use a flag......(its just a variable but commonly know as flag when used in such purposes )

Why the h*ll would you use an integer flag? Isn't boolean good enough here ?

flag is a variable name .. u can also write ur name instead of that.. its just that i am a bit convenient with that name that's its.....
and i am also giving an option in switch case if the user wants to exit.
both the way it works.....

flag is a variable name .. u can also write ur name instead of that.. its just that i am a bit convenient with that name that's its.....

I know flag is a variable name, but two things, if you would have to use a flag in this case you should have used a boolean instead of an integer and here it's just overkill, why would you use an integer flag here?

i guess u didnt understand my code..... why dont you execute this code and see how exactly it works.......
and variable can also be put inside instead of boolean, any way 1= TRUE and and 0 or lesser implies false.......

and if you have observed the original code.. it doesnt have ne room for allowing the user to exit..
if he./she presses the wrong number , its just wants to accept the new number .. so where the h@#l the user will exit............

i guess u didnt understand my code..... why dont you execute this code and see how exactly it works.......
and variable can also be put inside instead of boolean, any way 1= TRUE and and 0 or lesser implies false.......

I know, you don't have to teach me C++, but a boolean takes up less memory :)

while ( cin>>option ) {
    switch ( option )
    {
    }
}

i need

default: cout<<endl<<"Wrong option number!! Try again\n";

as well!!!

What about the following:

do {
    cin>>option;
    switch ( option ) 
    {
	case 1:
            displayCerteinDate();
	    break;
	case 2:
            displayAll();
	    break;
	default:
            cout<<endl<<"Wrong option number!! Try again\n";
    }
} while (!(1<= option && option<=2));

actually u can use a flag......(its just a variable but commonly know as flag when used in such purposes )

void display( )
{  
	int option, int flag =1;
	system("cls"); //clear screen
	cout<<endl<<endl;
	cout<<"\t  * Display Menu *\n";
	cout<<"\n\t1. Display For Certein Date"<<endl; 
	cout<<"\t2. Display All Appointments"<<endl;
	cout<<"\t3.  exit"<<endl;
	cout<<"\nEnter Option Number: ";  //Enter option
	cin>>option;
	cin.get();
    while(flag)
	{   
        switch ( option ) 
        {
            case 1: displayCerteinDate ( );
                    break;
            case 2: displayAll ( );
		             break;
            case 3: flag =0;
                     break;
            default: cout<<endl<<"Wrong option number!! Try again\n";
                    cin.get();         //give the user a chance to read the output data
	  }
	}

}

i have intialised a variable flag to 1 . and put the whole swicth inside a while loop which check for the status of the flag.
i have included an exit option in the menu . so as soon as the user presses 3. the flag =0 and the while condition fails , eventually u will come out of the loop and program ends

i couldent get it why you made liket his but any way i tried its looping but only

"Wrong option number!! Try again\n";

even if i enter 1 or 2 after one wrong option its still looping same. and it effected option 1 and 2 as well after they display result not returning main. looping forever

Did you even read my previous post ?

Did you even read my previous post ?

yes i was trying your codes but ist giving error

Error 1 error C2059: syntax error : '}' c:\visual studio 2005\projects\wer\wer\wer.cpp 278

looks fine to me but giging this error.

yes i was trying your codes but ist giving error
looks fine to me but giging this error.

Then you must have done something wrong, I tested it and it was working perfectly :)

Could you please post the whole code which was causing this error?

void display( )
{          nt option;
	system("cls"); //clear screen
	cout<<endl<<endl;
	cout<<"\t  * Display Menu *\n";
	cout<<"\n\t1. Display For Certein Date"<<endl; 
	cout<<"\t2. Display All Appointments"<<endl;
	cout<<"\nEnter Option Number: ";  //Enter option
	cin>>option;
	cin.get();
	do { 
	          cin>>option;
	        switch ( option ) 
	         {
		 case 1: displayCerteinDate ( );
			     break;
		 case 2: displayAll ( );
		              break;
	         default: cout<<endl<<"Wrong option number!! Try again\n";
	                  cin.get();         //give the user a chance to read the output data
	          }
	while (!(1<=option && option<=2));
	  }
}

Change

while (!(1<=option && option<=2));
	  [B]}[/B]

to

[B]}[/B]
while (!(1<=option && option<=2));

nt option; has to be int option; :)

the following:

....
        cin>>option;
	cin.get();
	do { 
....

should be changed to:

....
	do { 
....

and...

.....
default: cout<<endl<<"Wrong option number!! Try again\n";
	                  cin.get(); //give the user a chance to read the output data
	          }
.....

has to be:

.....
default: cout<<endl<<"Wrong option number!! Try again\n";
	          }
.....

:P

Thanks tux4life you are the best
its done.

It was actually good old Salem who brought me on the idea :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.