menu return problem

haii..straight to the point..im using Quincy 2005 and DevC++ as my compiler...i want to implement elevator simulator...the problem here is i can't go to next menu after key in my choice...below is the menu code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
void Screen_Output::Header() // Menu header (SECOND MENU)
{
	cout<<"\n\n\n"<<endl;
	cout << "\t\t--------------------------------------------"<< endl;
	cout << "\t\t*****\t\tWelcome to\t\t*****"<< endl;
	cout << "\t\t*****   Elevator Simulation Program    *****" << endl;
	cout << "\t\t--------------------------------------------" << endl;
}

//implementation of menu() method (THIRD MENU)
int Screen_Output::menu()
{
		cout<<"\n\n"<<endl;
		//the kbhit() function detects if there is input from the keyboard
	if(kbhit())  	//TRUE if the there is input from keyboard

		{
	cout<<"\t\t----------------------------------------------"<<endl;
	cout<<"\t\t*****\t\t1. Call Elevator\t*****"<<endl;
	cout<<"\t\t*****\t\t2. Continued\t\t*****"<<endl;
	cout<<"\t\t*****\t\t3. Exit\t\t\t*****"<<endl;
	cout<<"\t\t----------------------------------------------"<<endl;
	cout<<"Your choice: ";
	cin>>choice;
	return choice;
		}
}

//main driver program (FIRST MENU TO CHOOSE THE CONDITION)
//after choosing, it should go to SECOND MENU and then THIRD MENU (above)
int main()
{
	Screen_Output display;
	srand(time(0));//initialize the random number
	Elevator lift1(rand()%4+1), lift2(rand()%4+1), lift3(rand()%4+1), lift4(rand()%4+1);//initialize the random floor of elevator
	int option, counter=0;
	
		while(1)
		{	
		system("cls");//clear the screen
		display.Header();//call Header method
		option=display.menu();//call menu method to get user's option
		if (option==1)//decide which condition is being used
		{
		People Passenger;
		Passenger.SetPassengers();//call SetPassengers method
		Passenger.Run(lift1, lift2, lift3, lift4);//call base class run method by using derived class object
		
		}
		else if (option==3)//if user exit program
			break;

		system("cls");//clear the screen	

		cout << "\n\n\n\t\t\tPress ENTER to access MENU";
		cout << "\n\n\t\t\t---------------------------\n" ;
		cout << "\t\t\t      ELEVATOR SYSTEM      \n";
		cout << "\t\t\t---------------------------\n";
		cout << "\t\t\t 1. 4 Elevator \n";
		cout << "\t\t\t 2. 3 Elevator \n";
		cout << "\t\t\t 3. 2 Elevator \n";
		cout << "\t\t\t 4. 1 Elevator \n";
		cout << "\t\t\t Choose condition between 1-4 : \n";
		cin >> decision;

                system("cls");
		
                //after decide, it show the output based on the user choose.
                // ELEVATOR OUTPUT
		if(decision==1)//decide which condition is being used
		{
		cout << "\n\t\t\tElevator 1\n";	lift1.CurrentStatus();
		cout << "\n\t\t\tElevator 2\n";	lift2.CurrentStatus();
		cout << "\n\t\t\tElevator 3\n";	lift3.CurrentStatus();
		cout << "\n\t\t\tElevator 4\n";	lift4.CurrentStatus();		
		}
		if(decision==2)
		{	
		cout << "\n\t\t\tElevator 1\n";	lift1.CurrentStatus();
		cout << "\n\t\t\tElevator 2\n";	lift2.CurrentStatus();
		cout << "\n\t\t\tElevator 3\n";	lift3.CurrentStatus();
		cout << "\n\t\t\tElevator 4\n\t\t\t>> Not Operating.";
		}
		if(decision==3)
		{
		cout << "\n\t\t\tElevator 1\n";	lift1.CurrentStatus();
		cout << "\n\t\t\tElevator 2\n";	lift2.CurrentStatus();
		cout << "\n\t\t\tElevator 3\n\t\t\t>> Not Operating.";
		cout << "\n\t\t\tElevator 4\n\t\t\t>> Not Operating.";
		}
		if(decision==4)
		{
		cout << "\n\t\t\tElevator 1\n";	lift1.CurrentStatus();
		cout << "\n\t\t\tElevator 2\n\t\t\t>> Not Operating.";
		cout << "\n\t\t\tElevator 3\n\t\t\t>> Not Operating.";
		cout << "\n\t\t\tElevator 4\n\t\t\t>> Not Operating.";
		}
		cout << "\n\t\t\t===========================\n";
		cout << "\t\t\tTimer :" << counter << endl;	//show timer
	counter++;  //counter for timer	
	Sleep(1000);//to delay the timing
		}
}


the output should go like this :
FIRST MENU

 Press ENTER to access MENU

 ---------------------------
       ELEVATOR SYSTEM
 ---------------------------
  1. 4 Elevator
  2. 3 Elevator
  3. 2 Elevator
  4. 1 Elevator
  Choose condition between 1-4 :


ELEVATOR OUTPUT :

  ---------------------------
        ELEVATOR SYSTEM
  ---------------------------

  Elevator 1
  >> Resting at floor: 4
  Elevator 2
  >> Not Operating.
  Elevator 3
  >> Not Operating.
  Elevator 4
  >> Not Operating.
  ===========================
  Timer :14



SECOND MENU and THIRD MENU


                --------------------------------------------
                *****           Welcome to              *****
                *****   Elevator Simulation Program    *****
                --------------------------------------------



                ----------------------------------------------
                *****           1. Call Elevator        *****
                *****           2. Continued            *****
                *****           3. Exit                 *****
                ----------------------------------------------
Your choice:


Problem here is that i only can go to FIRST MENU and then the program show the ELEVATOR OUPUT for a second and then loop the FIRST MENU... What should i add in the program code so that it will show the FIRST MENU, i choose the condition, it show the ELEVATOR OUTPUT, then it go to the SECOND MENU after i press the ENTER button, and then go back to FIRST MENU where it supposed to show which elevator is moving ?
1
2
cin>>choice;
return choice;


I don't see where you have the variable choice defined. Can you post the whole program?

edit: I think this statement is the problem:
if(kbhit())

You should get rid of that if statement and just leave what's inside of it because that if statement is not inside a loop. So that's causing your program to return to main and continue your while(1) loop.
Last edited on
the variable choice defined in the Screen_Output class...here the class declaration...

1
2
3
4
5
6
7
8
9
10
11
12
13
//declaration of Screen_Output class
class Screen_Output
{
public:
	Screen_Output()://constructor initialize private data
	  choice(0)
	{};
	  ~Screen_Output(){};//destructor
	void Header();//access method to display the header
	int menu();//access method to display menu
private:
	int choice;
};
cant post the whole program...got some error because it too long...
See my edit, I believe that is your problem.
okay...i try it first...hope it would work...get back to you later...
okay...i have remove the if statement...now the problem has change...

new problem (based on the output that i get) :
1. it show the SECOND MENU first.
2. then, it shows the FIRST MENU after i select 1 and 2 in the SECOND MENU.
3. after i decide on the FIRST MENU, it shows the SECOND MENU again.

i start to get confuse...
It shows SECOND MENU first because that's how you have it coded

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
	while(1)
		{	
		system("cls");
		display.Header();//<----SECOND MENU
		option=display.menu();//<----THIRD MENU
		if (option==1)
		{
		People Passenger;
		Passenger.SetPassengers();
		Passenger.Run(lift1, lift2, lift3, lift4);
		
		}
		else if (option==3)
			break;

		system("cls");	

		cout << "\n\n\n\t\t\tPress ENTER to access MENU";//<---FIRST MENU
		cout << "\n\n\t\t\t---------------------------\n" ;
		cout << "\t\t\t      ELEVATOR SYSTEM      \n";
		cout << "\t\t\t---------------------------\n";
		cout << "\t\t\t 1. 4 Elevator \n";
		cout << "\t\t\t 2. 3 Elevator \n";
		cout << "\t\t\t 3. 2 Elevator \n";
		cout << "\t\t\t 4. 1 Elevator \n";
		cout << "\t\t\t Choose condition between 1-4 : \n";
		cin >> decision;


So put your FIRST MENU in a separate function and call that one before you call the SECOND MENU.
Last edited on
okey...now i have the picture of it...i edit my code first and get back to you later...
i already change the code...but the output not as i expected to be...it keep looping to the FIRST MENU...would you like to see the code for me...i made a change a little bit...i remove the FIRST MENU and make the decision choice in the SECOND MENU...before this, i use randomize to randomly select the condition...and it work like i expected it should...but i want the user to make the decision...and i remove the randomize function...if you want to see the whole program including the one that i use randomize, give me your email...i send it to you as soon as possible so that you can see how the output should be and show/tell me what i have to change in the code...
Topic archived. No new replies allowed.