Menu Help

So I need help with a menu for an airline program. (yes i am obviously a newbie :p) The problem I have is that i have to have a menu. one option is to enter an id. the second option is to enter a seat for that specific user ID. Im really confused here. I had a program but i cant use if statements.
ex.
1
2
3
4
5
if (choice ==1)
{
cout << "please enter an id"
cin >> user[num].id; 
}


because the struct is only available for that function.

how would you go about doing this?
i just need a push in a good direction.
im not looking for saving memory or anthing. this is for homework obviously.
i thought about using classes but im not too proficient with them yet.

i also have to output the id along with the seat to a file because an option is to read in a previous file with previous users and seats.

I'm sorry if this is too confusing. ill try to explain it more if i need to.

thanks in advanced for any help.

please post all your code because we wont be able to understand unless we see atleast the struct. are you getting compiler errors or runtime errors or what? also i personally recommend against using structs cause i find they lead to trouble more then they help ;)
Its a good idea to show us all of your code, if not this might help.


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
using namespace std;
#include <iostream>

int main()
{
    string choice;
    cout << "----Menu----\n";
    cout << "1. This\n";
    cout << "2. That\n";
    cout << "3. Who\n";
    while (choice != "done")
    {
        cout << "Your choice: ";
        cin >> choice;
        if (choice == "1")
        {
            //Do function
        }
        else if (choice == "2")
        {
            //Do
        }
        else if (choice == "3")
        {
            //do
        }
    }
}



or


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
using namespace std;
#include <iostream>

void menu();

int main()
{
    menu();
}

void menu()
{
    string choice;
    cout << "----Menu----\n";
    cout << "1. This\n";
    cout << "2. That\n";
    cout << "3. Who\n";
    while (choice != "done")
    {
        cout << "Your choice: ";
        cin >> choice;
        if (choice == "1")
        {
            //Do function
        }
        else if (choice == "2")
        {
            //Do
        }
        else if (choice == "3")
        {
            //do
        }
    }
}


Last edited on
ok so here is what i have so far. I saw that you used else if's mobat.
would that be a lot better than just using if statements? would i be able to use cin's from one of those else if functions in another function?

im not getting any errors except for the fact that i cannot use an id to match the flight seat.
so when i enter a seat it erases from memory when i go back to the main menu. when i print out the seating chart the seat is now not occupied with the "X".

by the way here is my 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*CSC 160

This is a program where a user picks a customer ID and picks his or her seat on 
an airplane flight. It is menu driven and does a series of tasks regarding 
airline seating. */

#include <iostream>
#include <fstream>
#include <string>
using namespace std; 


//declare struct called user
struct user
{
       string pin; 
       string seat; 
      
       };
//declare mad amounts of struct users       
const int maxsize = 21; 
int main()
{  
    
   //declare file to read from and to save to
    ofstream outdata("manifest.dat");
    ifstream indata("manifest.dat"); 
   
   //declare two dimensional array of seats
    char seats[7][4] =
    
    {   {'1', 'A', 'B', 'C'},
        {'2', 'A', 'B', 'C'},
        {'3', 'A', 'B', 'C'},
        {'4', 'A', 'B', 'C'},
        {'5', 'A', 'B', 'C'},
        {'6', 'A', 'B', 'C'},
        {'7', 'A', 'B', 'C'}
        
        
      
        };      
       

        //declare ints and struct called users. 
    int row = 0; 
    int column = 0; 
    int choice; 
    int num = 1; 
    user you, users[maxsize]; 
    
  //clear screen when i return to main from choices. 
  
    system("cls"); 
    
    //menu of the airline program.
    cout <<"1. Enter Customer ID" << endl; 
    cout <<"2. Enter Requested Seat" <<endl; 
    cout <<"3. View Assigned Seats" <<endl; 
    cout <<"4. Print Manifest" <<endl; 
    cout <<"5. Save to File" <<endl; 
    cout <<"6. Read Saved file" <<endl; 
    cout <<"7. Exit" <<endl; 
    cout <<"Please enter a choice 1-7: " <<endl; 

    //choice from 1-7
    cin >> choice; 
    
    
    //choice number 1
    if (choice == 1)
    {
               //clear screen first
               system("cls"); 
               
               //enter id of 4 numbers 
     cout << "Please enter a customer id of 4 numbers: "<<endl; 
          cin >> users[num].pin; 
          //send pin number to the file
          outdata << users[num].pin;
          
          
          cout<< "Returning to main menu...."<<endl; 
          
          
          system ("pause");
          return main();
          }
          
          //for choice 2
          if (choice == 2)
          { 
                     
                     system("cls");
                  
                     
                   
                    // enter seat you wish to sit in
          cout << "Please enter the seat you want to be placed in, starting with"<<endl; 
          cout << "the row. (For example for seat 1 B, enter: 1 2)"<<endl; 
          cout <<"Enter choice now: "<<endl; 
          //out put row and column to file. 
          cin >> row>>column; 
          outdata << row << " " << column; 
          
          // printing out seating chart for varification
         row = row - 1; 
   
          seats[row][column] = 'X'; 
          
           for (int i = 0; i < 7; i++)
          {
               for (int j = 0; j < 4; j++)
               
             cout <<  " " << seats[i][j]<< " "; 
             cout <<endl; 
          
          
        
         } 
         //change user to the next one
          num++;     
          system("pause");
          return main(); 
          }
          //print out seats
    if (choice == 3)
    { 
               system ("cls"); 
       cout << "Seats taken are marked with an 'X'" <<endl; 
          for (int i = 0; i < 7; i++)
          {
               for (int j = 0; j < 4; j++)
               
             cout <<  " " << seats[i][j]<< " "; 
             cout <<endl; 
         }    
         cout << "Returning to main screen"<<endl; 
            system ("pause"); 
            return main(); 
     
     }
     /* print out manifest
     for ex. 
     
     Manifest for flight. 
     1111 1B
     2321 3C
     etc. */
     
    if (choice == 4)
    { 
               system("cls"); 
   
    cout << users[0].pin<<endl; 
               
               cout << "Manifest for Flight 13 - F" << endl; 
               for (int i =1; i < num; i++)
                  {
                        cout << users[i].pin<<endl; 
                             cout << " " << users[i].seat << endl; 
                             }
               cout << "returning to home screen.." << endl; 
               
               system("pause");
               
               return main();
               }
               
    if (choice == 5)\
    { //save to file
               outdata << users[num].pin<<" " <<users[num].seat<<endl; 
               }
    if (choice == 6) 
    {
               //read from file which i do not know how to do
               }
               
               //exit
    if (choice == 7)
    {
               return 0; 
               }
               
system("pause");
return 0;   
 
}
Last edited on
You could make a function for each operation in the menu and then use a switch statement to implement the menu.
Yea thats what i eventually had to do cristi. Thanks a lot you guys for your help. =]
Topic archived. No new replies allowed.