intro to c++, looking for some help

Just finish a semester of intro to c++ and I understand the basics of coding now. I'm making a menu program and I want to make the program loop to previous menus. I guess I'm trying to be able to navigate between menu options. got some advice? thanks.
(Also i know some other forums i use that have "spoilers" where you can put a lot of code in and it wont expand in the area unless you click an expand button. maybe helpful to read and differ between the question and the 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
#include <cstdlib>
#include <iostream>
#define tax 7.5     //tax globally defined

using namespace std;

int main(int argc, char *argv[])
{
    int choice1, choice2;
    // A customer wants to stay at a hotel and know:
    // What rooms are available and how much the rooms are
    
    cout << "Thank you for choosing Global Hotels, How may I help you?\n\nSelect a menu option.\n";
    cout << " 1. Hotel Room Pricing.\n";
    cout << " 2. Hotel Room Availability.\n";
    cout << " 3. EXIT.\n";
    cin >> choice1;
    
    if (choice1 == 1)
    {
        cout << "Where would you like to stay?\n";
        cout << "1. Conceirge Boarding.\n";
        cout << "2. Common Coach Boarding.\n";
        cout << "3. Kings and Queens Luxary Boarding.\n";
        cout << "4. Return to Previous menu.\n";
        cin >> choice2;
        
            if (choice2 == 1)
                cout << "Pricing is $59.99+tax\n";
            else if (choice2 == 2)
                cout << "Pricing is $72.99+tax\n";
            else if (choice2 == 3)
                cout << "Pricing is $129.00+tax\n";
            else if (choice2 == 4)
            {
                cout << "Where would you like to stay?\n";
                cout << "1. Conceirge Boarding.\n";
                cout << "2. Common Coach Boarding.\n";
                cout << "3. Kings and Queens Luxary Boarding.\n";
                cout << "4. Return to Previous menu.\n";
                cin >> choice2;
            }
            else 
                cout << "Invalid Answer.";
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
Last edited on
Everything looks fine in your menu.... it doesn't look complete yet though. If statements are fine in a situation like this, but I think you should switch statements instead of if's for making menus.
Thanks for the swift reply. I need to find a way to loop it back to the previous menus so the program doesn't come to an end. How do i need to do this? (Im still working on the code as its not complete yet, but i wanted to show the generalization of what im doing to make sure im in the right direction.)
Haha.

Right. What you should do is place your main in a do-while loop. If the person enters an invalid answer, you can make it loop the program. To make it loop, make sure you make the condition true when the user enters something wrong. If the user does, it'll repeat the whole program.
Thanks sounds like an easy to do fix. ill have at it for a while and reply if i get stuck and such, ok where do i input the do and while part? lol sry i dont know how to manipulate the main function.
Last edited on
Someone helped me by using this but it doesnt compile with c++.

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
#include <cstdlib>
#include <iostream>
#define tax 7.5     //tax globally defined

using namespace std;

int main(int argc, char *argv[]){
    int choice;
    // A customer wants to stay at a hotel and know:
    // What rooms are available and how much the rooms are
    
    while(choice != 3){
	choice = mainMenu();
	// this means only exit if they choose 3...
        if (choice == 1){
           // then display the next menu
	   choice = menu2();
	   if (choice == 1)
                cout << "Pricing is $59.99+tax\n";
            else if (choice == 2)
                cout << "Pricing is $72.99+tax\n";
            else if (choice == 3)
                cout << "Pricing is $129.00+tax\n";
            else if (choice == 4){
                choice = menu2();
            }
            else 
                cout << "Invalid Answer.";
        }
	// TODO Code for your other menu option...
	if(choice ==2){
	  cout << "Must code other menu...blablabla...";
	}
    } // END LOOP	
    // got here because they choose 3..
    return EXIT_SUCCESS;
}
int mainMenu(){
    int choice;
    cout << "Thank you for choosing Global Hotels, How may I help you?\n\nSelect a menu option.\n";
    cout << " 1. Hotel Room Pricing.\n";
    cout << " 2. Hotel Room Availability.\n";
    cout << " 3. EXIT.\n";
    cin >> choice;
    return choice;
}
int menu2(){
   int choice;
   cout << "Where would you like to stay?\n";
   cout << "1. Conceirge Boarding.\n";
   cout << "2. Common Coach Boarding.\n";
   cout << "3. Kings and Queens Luxary Boarding.\n";
   cout << "4. Return to Previous menu.\n";
   cin >> choice;
   return choice;
}
It's missing the function declarations. Stick

1
2
int mainMenu();
int menu2();


at the top.

Haha.

Right. What you should do is place your main in a do-while loop. If the person enters an invalid answer, you can make it loop the program. To make it loop, make sure you make the condition true when the user enters something wrong. If the user does, it'll repeat the whole program.


So i did this, its not 100% but if anyone has an idea of what i should code to make it better im up for the challenge.

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
#include <cstdlib>
#include <iostream>
#define tax 7.5     //tax globally defined

using namespace std;

int main(int argc, char *argv[])
{
   
    int choice, choice2;
    // A customer wants to stay at a hotel and know:
    // What rooms are available and how much the rooms are
     do {
    cout << "Thank you for choosing Global Hotels, How may I help you?\n\nSelect a menu option.\n";
    cout << " 1. Hotel Room Pricing.\n";
    cout << " 2. Hotel Room Availability.\n";
    cout << " 3. EXIT.\n";
    cin >> choice;
    
    if (choice == 1)
    {
        cout << "Where would you like to stay?\n";
        cout << "1. Conceirge Boarding.\n";
        cout << "2. Common Coach Boarding.\n";
        cout << "3. Kings and Queens Luxary Boarding.\n";
        cout << "4. Return to Previous menu.\n";
        cin >> choice2;
        
            if (choice2 == 1)
                cout << "Pricing is $59.99+tax\n";
            else if (choice2 == 2)
                cout << "Pricing is $72.99+tax\n";
            else if (choice2 == 3)
                cout << "Pricing is $129.00+tax\n";
            else if (choice2 == 4)
            {
                cout << "Where would you like to stay?\n";
                cout << "1. Conceirge Boarding.\n";
                cout << "2. Common Coach Boarding.\n";
                cout << "3. Kings and Queens Luxary Boarding.\n";
                cout << "4. Return to Previous menu.\n";
                cin >> choice2;
            }
            else 
                cout << "Invalid Answer.";
    }
    
    if (choice == 2)
    {
        cout << "Currently there are rooms available in the Concierge Boarding\n";
        cout << "and Kings and Queens Luxary Boarding.\n";
        
        cout << "Select a menu option.\n";
        cout << " 1. Hotel Room Pricing.\n";
        cout << " 2. Hotel Room Availability.\n";
        cout << " 3. EXIT.\n";
        cin >> choice;
    }
    
        }while (choice != 3);

    system("PAUSE");
    return EXIT_SUCCESS;
}
You're definitely on the right track.

Your compilation errors can be fixed be changing int choice; to int choice = 0;. You always have to initialize values before using them. You also need to add the following lines before int main():
1
2
int mainMenu();
int menu2();

This tells the functions below to expect them. Otherwise when you call it the compiler will have no idea what you are talking about.

If you are only using mainMenu and menu2, this is pretty good. Since you are implementing more menus, I'd assign each menu with a unique number. Otherwise It'll start to get tough to tell if you want Hotel Room Pricing or Conceirge Boarding.

I'd use enum, but you can also just use defines or declare a bunch of global variables.

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
namespace menu
{
  enum menuList
  {
    main = 1,           //main menu
    mHotelPricing,      //menu - hotel pricing
    mHotelAvailability, //menu - hotel availablility
    //Add more as nessesary
  }
}

int main()
{
  int choice = menu::main;

  while (choice > 0)
  {
    switch (choice)
    {
    case menu::mHotelPricing:
      choice = HotelPriceMenu();
      break;
    case menu::mHotelAvailability:
      choice = HotelAvailMenu();
      break;
    case menu::main:
    case default:
      choice = mainMenu();
      break;
    }    
  }

  return 0;
}

int mainMenu()
{
  int choice;
  cout << "Thank you for choosing Global Hotels, How may I help you?\n\nSelect a menu option.\n";
  cout << " 1. Hotel Room Pricing.\n";
  cout << " 2. Hotel Room Availability.\n";
  cout << " 3. EXIT.\n";
  cin >> choice;

  switch (choice)
  {
    case 1:
      return menu::mHotelPricing;
    case 2:
      return menu::mHotelAvailability;
    case 3:
    default:
      return -1;
  }    
}


This will let you add tons of menus and will let you call any menu from any other menu without difficulty.
Last edited on
Topic archived. No new replies allowed.