problem with code for assigning flight seats

hi, im new to programming and i desperately need help, help of any sort is welcome. we were given this problem:
You are the developer for taxiflite.com, a company which charters a light aircraft to
Capetown from Johannesburg. Write a program to assign seats on each flight of the
airline's only plane (capacity: 50 seats (10 rows (A-J) of 5 seats each)). The program
should display the following menu of alternatives: Please type 1 for "First class" and
Please type 2 for "Economy". If the person types 1 your program should ask for the
person’s name and assign a seat in the first class section (rows 1-3 (15 seats)). If the
person types 2, the program should ask for the person’s name and assign a seat in
economy section (rows 4-10 (35 seats)). Menu Option 3 displays the seating
allocation. The program should print a boarding pass indicating the person’s name
and seat number and weather it is in the first class or economy section of the plane
along with the price (R10 000 for first class and R5 000 for economy). Isle seats (A-J
seat number 2 and 3 are subject to a 10% discount) Use a 2-Dimensional array to
represent the seating chart of the plane. Initialize all the elements of the array to 0 to
indicate that all seats are empty. As each seat is assigned, set the corresponding
elements of the array to X to indicate that the seat is no longer available. When the
first class section is full, the program should ask the person if it is acceptable to be
placed in the economy section (and vice versa). If yes then make the appropriate
seat assignment. All seats in each section are assigned randomly unless the person
flying stipulates a specific seat. In the display view, show the percentage occupied of
the total flight (seats taken vs seats available) else if no seats are available then print
the message "Next Flight leaves in 3 hours." All flight information must be saved to a
file.
use Function for initialize, menu, checkSeat, printTicket, displaySeating

so far i have come up wit the following code but I'm stuck and unsure of what I'm doing:

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
//program assigns seats to passengers in a flight from CTN to JBG
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{

const int firstClassPrice = 10000;
const int economyPrice = 5000;
int name, answer; 
double price, discount;

      function init()
      {
       const int rows = 9;
       const int columns = 5;
       int seat[rows][columns];
       for (int rows = 0, rows < 9; rows++)
            for (int columns = 0; columns < 5; columns++)
               char seat[rows][columns] = "X";
      }

      function menu()
      {
        cout << "Welcome to taxiflite.com\n";
        << "Please choose one of the following: \n";
        << "1. First class\n";
        << "2. Economy\n";
        << "3. Seating allocation\n";
        << "4. Ticket holder\n";

       cin >> choice;
       switch (choice)
      
      }  
         case 1: 

         while(rows < 2)
         {
           function init()

           cout << "Enter name: ";
   	   cin >> name;
   	   function checkseat()

		   if ( column[2] || column[3])
		    {        
			discount = 10 / 100 * firstClassPrice;
		        price = firstClassPrice - discount;
		    }  
		  else
 			price = firstClassPrice;
    

	 }
	//check to see if first class is full
	if (seat[2][5] == "X" )
	cout << "Would you like for us to book a seat for you in economy(y/n): ";
	cin >> answer;
	if ( answer == "y" )
	   function init()
	   
       else
	  "Next flight leaves in 3 hours";


	case 2:
	while ( rows > 2 )
	{
   	  function init()

    	  cout << "Enter name: ";
          cin >> name;

          	if ( column [2] || column[3] )
             	{   
		  discount = 10 / 100 * economyPrice;
   		  price = economyPrice - discount;
   		}
   		else
 		  price = economyPrice;
		}
	case 3:

		function printTicket()

	case 4:



	default:
	cout << "You chose incorrectly\n"
}

	function checkSeat()
	{
	  function init()

	  cout << "Do you want a specific seat?(yes/no: "
	  cin >> answer;
		if( answer == yes )
		  if( seat[rows][columns] = "0" )
		  {			 
			 char seat[rows][columns] = "X";
			 name[rows][columns] = name;
		  }
		else
		{
//im not sure if this is how to choose a seat randomly
		   function init()
		   if(0)
		   random(0,5)
		}
	}

	function printTicket(string name, type; double price; int seat;)
	{
		cout <<"Name: " << name << endl;
     		     << "Seat No: " << seat << endl;
                     << "Ticket type: " << type << endl;
                     << "price: " << price << endl;

	}

	function displaySeating()
	{

	}
return 0;
}

Some of the stuff is left out because im unsure. :-)
i think my main problem is understanding what is really required
Last edited on
closed account (o3hC5Di1)
Hey there,

Please do following things, then we'll try and help you along:

1. Put your code into a [code ]{code goes here}[/code] block, it will make it easier for us to read.
2. Indent your code, again for readability: http://en.wikipedia.org/wiki/Programming_style#Indentation
3. describe clearly what problems you are having, what is not working or what unexpected behaviour the program is giving you.

Sorry to be so "demanding", but your assignment is rather large and without these steps it makes it really a lot more tedious for us to help you.

Do get back to us as soon as you have managed those things though.

All the best,
NwN
Last edited on
i tried to do what you required me to do...if the is still something i should change please let me know
closed account (o3hC5Di1)
Hello there,

Thanks for doing that.

Now then, I've made some alterations to your code, however it is still full of incorrect syntax - I haven't looked at the logic yet as these syntax errors need to be resolved first.

First of all, every instruction in c++ must end with a semicolon ";".
You have a very large number of instructions without those.
Instructions mean things like "cout << endl;", assignments "int a=0;", etc. not control structures such as if/else, loops, ...
So I would advise you to fix those first, it will make your compiler a lot happier.

Second, you should remember that "else" statements require curly braces else { /*code goes here*/ } , I've fixed those for you this time.

Third, functions in c++ are not declared, nor called by the word "function". You need to specify the type of data which the function returns, or if it only executes code and doesn't return anything, use void, as such:
1
2
3
4
5
6
7
8
9
//notice where the semicolons are
//function returns an int, so declared as int
int calculateTotal( int ticketprice, int ticketamount) { return ticketprice*ticketamount; }
//function only does something without returning a value, so void
void printTicket() { /*printing ticket here*/ }

//calling (using) the function is like this:
total = calculateTotal(ticketprice, ticketamount);
printTicket();


Fourth, you shouldn't be declaring function within your main function. The whole point of functions is that you take the logic out of main() so it can be reused or isolated easily.

So:

1. Fix your syntax, particularly semicolons; and the way you call functions.
2. Put your functions out of the main() {} code block.

Then come back to us and we'll have a look at the actual logic, you're randomising function for instance will not work the way it is now, but let's take it step by step.

All the best,
NwN

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

//program assigns seats to passengers in a flight from CTN to JBG
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

//function declarations should go here for now

int main()
{

const int firstClassPrice = 10000;
const int economyPrice = 5000;
int name, answer; 
double price, discount;

      function init()
      {
       const int rows = 9;
       const int columns = 5;
       int seat[rows][columns];
       for (int rows = 0, rows < 9; rows++)
            for (int columns = 0; columns < 5; columns++)
               char seat[rows][columns] = "X";
      }

      function menu()
      {
        cout << "Welcome to taxiflite.com\n";
        << "Please choose one of the following: \n";
        << "1. First class\n";
        << "2. Economy\n";
        << "3. Seating allocation\n";
        << "4. Ticket holder\n";

       cin >> choice;

       //changed switch to if statement - better for large code blocks
       if (choice == "1")
       {
            while(rows < 2)
            {
                function init()
                cout << "Enter name: ";
   	            cin >> name;
   	            function checkseat()

		        if ( column[2] || column[3])
		        {        
			        discount = 10 / 100 * firstClassPrice;
		            price = firstClassPrice - discount;
		        }  
		        else
 			    price = firstClassPrice;
            }
	
            //check to see if first class is full
	        if (seat[2][5] == "X" )
	        {
                cout << "Would you like for us to book a seat for you in economy(y/n): ";
	            cin >> answer;
            }
	        if ( answer == "y" )
            {
                function init();
            }
	        else
            {
	            "Next flight leaves in 3 hours";
            }
        }

	    else if (choice == "2")
        {
        	while ( rows > 2 )
	        {
   	            function init()

    	        cout << "Enter name: ";
                cin >> name;

          	    if ( column [2] || column[3] )
             	{   
		            discount = 10 / 100 * economyPrice;
   		            price = economyPrice - discount;
   		        }
   		        else
                {
 		            price = economyPrice;
                }
             }
		}
	
        else if (choice == "3")
        {
            function printTicket()
        }

	    else if (choice =="4")
        {
            //ticket holder code goes here
        }

        else
        {
		    cout << "You chose incorrectly\n"
        }


	function checkSeat()
	{
	  function init()

	  cout << "Do you want a specific seat?(yes/no: "
	  cin >> answer;
		if( answer == yes )
		  if( seat[rows][columns] = "0" )
		  {			 
			 char seat[rows][columns] = "X";
			 name[rows][columns] = name;
		  }
		else
		{
//im not sure if this is how to choose a seat randomly
		   function init()
		   if(0)
		   random(0,5)
		}
	}

	function printTicket(string name, type; double price; int seat;)
	{
		cout <<"Name: " << name << endl;
     		     << "Seat No: " << seat << endl;
                     << "Ticket type: " << type << endl;
                     << "price: " << price << endl;

	}

	function displaySeating()
	{

	}
    
    return 0;
}
Last edited on
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

//program assigns seats to passengers in a flight from CTN to JBG
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

//function declarations should go here for now
//i saw this in our c++ book called function prototypes/function declarations
void init();
void menu();
void checkSeat();
void printTicket();
void displaySeating();

int main()
{

const int firstClassPrice = 10000;
const int economyPrice = 5000;
int name, answer,seat; 
double price, discount;
string type;

//since im going to mainly use the menu function, i only call that function.
menu()


return 0;
}
      void init()
      {
       const int rows = 9;
       const int columns = 5;
       int seat[rows][columns];
       for (int rows = 0, rows < 9; rows++)
            for (int columns = 0; columns < 5; columns++)
               char seat[rows][columns] = "X";
      }

      void menu()
      {
        cout << "Welcome to taxiflite.com\n";
        << "Please choose one of the following: \n";
        << "1. First class\n";
        << "2. Economy\n";
        << "3. Seating allocation\n";
        << "4. Ticket holder\n";

       cin >> choice;

       //changed switch to if statement - better for large code blocks
       if (choice == "1")
       {
         type = "First class";
            while(rows < 2)
            {
                init();
                cout << "Enter name: ";
   	            cin >> name;
   	             checkseat();

		        if ( column[2] || column[3])
		        {        
			    discount = 10 / 100 * firstClassPrice;
		            price = firstClassPrice - discount;
		        }  
		        else
 			    price = firstClassPrice;
            }
	
            //check to see if first class is full
	        if (seat[2][5] == "X" )
	        {
                cout << "Would you like a seat for you in economy(y/n): ";
	            cin >> answer;
            }
	        if ( answer == "y" )
            {
                 init();
            }
	        else
            {
	        cout << "Next flight leaves in 3 hours" << endl;
            }
        }

      else if (choice == "2")
        {
            type = "Economy"
        	while ( rows > 2 )
	        {
   	             init();

    	        cout << "Enter name: ";
                cin >> name;

          	    if ( column [2] || column[3] )
             	       {   
		            discount = 10 / 100 * economyPrice;
   		            price = economyPrice - discount;
   		       }
   		    else
                       {
 		            price = economyPrice;
                       }
                }
        }
	
        else if (choice == "3")
         {
             printTicket();
         }

        else if (choice =="4")
          {
            //ticket holder code goes here
          }

        else
          {
		    cout << "You chose incorrectly\n";
          }

      }
	void checkSeat()
       {
	   init();
         {
	  cout << "Do you want a specific seat?(yes/no: ";
	  cin >> answer;
		if( answer == yes )
		  if( seat[rows][columns] = "0" )
		  {			 
			 char seat[rows][columns] = "X";
			 name[rows][columns] = name;
		  }
		else
		{
//im not sure if this is how to choose a seat randomly
		   init();
		   if(0)
		   random(0,5);
		}
	}

	void printTicket()
	{
		cout <<"Name: " << name << endl;
     		     << "Seat No: " << seat << endl;
                     << "Ticket type: " << type << endl;
                     << "price: " << price << endl;

	}

	void displaySeating()
	{

	}
    


I tried to do what you asked for, the calling a function thing, im not sure about it though.
closed account (o3hC5Di1)
Hi,

Alright, I've spent A LOT of work on making your code compile and I've added comments on where your logic is flawed or where I made significant changes.

Please take some time to read through it, test it and see if you can solve any problems yourself now.

On a sidenote, you should really read up on some basic c++ syntax, things like semicolons and putting braces around if/else blocks is absolutely basic material.

All the best,
NwN

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
190
191
192

//program assigns seats to passengers in a flight from CTN to JBG
#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
#include <cstdlib>
using namespace std;

//function declarations should go here for now
//i saw this in our c++ book called function prototypes/function declarations
void init();
void menu();
void checkSeat();
void printTicket();
void displaySeating();

//we'll make global variables to provide easiest fix for now:
const int firstClassPrice = 10000;
const int economyPrice = 5000;
const int rows = 9;
const int columns = 5;
char seat[rows][columns];
int answer; 
double price, discount;
string type, sname, name[rows][columns];

int main()
{
    srand(time(NULL)); //need this for random numbers
    //since im going to mainly use the menu function, i only call that function.
    menu();
    return 0;
}
      
void init()
{
    for (int rows = 0; rows < 9; rows++)
        for (int columns = 0; columns < 5; columns++)
             seat[rows][columns] = 'X';
}

void menu()
{
    //choice was not declared
    string choice;
    //missing cout's here
    cout << "Welcome to taxiflite.com\n";
    cout << "Please choose one of the following: \n";
    cout << "1. First class\n";
    cout << "2. Economy\n";
    cout << "3. Seating allocation\n";
    cout << "4. Ticket holder\n";

    cin >> choice;

    if (choice == "1")
    {
        type = "First class";
        while(rows < 2)
        {
            init();
            cout << "Enter name: ";
            cin >> sname;
            checkSeat();
            
            //this is not the expression you want to use here
            //if ( column[2] || column[3])
            //you want to specify this from the seating plan
            //you will need checkSeat() to return the positions of the seat so you can do
            //seat == isleseat => discount
            //inserted temporary condition so it will compile
            if (bool isleseat=true )
            {        
                discount = 10 / 100 * firstClassPrice;
                price = firstClassPrice - discount;
            }  
            else
            {
                price = firstClassPrice;
            }
        }

        //check to see if first class is full
        //i doubt this checks if entire first class is full, it only checks if seat[2][5] is taken
        if (seat[2][5] == 'X' )
        {
            cout << "Would you like a seat for you in economy(y/n): ";
            cin >> answer;
        }
        if ( answer == 'y' )
        {
             init();
        }
        else
        {
            cout << "Next flight leaves in 3 hours" << endl;
        }
    }

    else if (choice == "2")
    {
        type = "Economy";
        while ( rows > 2 )
        {
            init();

            cout << "Enter name: ";
            cin >> sname;
            
            //this is not the expression you want to use here
            //if ( column[2] || column[3])
            //you want to specify this from the seating plan
            //you will need checkSeat() to return the positions of the seat so you can do
            //seat == isleseat => discount
            //inserted temporary condition so it will compile
            if (bool isleseat=true )
            {   
                discount = 10 / 100 * economyPrice;
                price = economyPrice - discount;
            }
            else
            {
                price = economyPrice;
            }
        }
    }

    else if (choice == "3")
    {
        printTicket();
    }

    else if (choice =="4")
    {
        //ticket holder code goes here
    }

    else
    {
        cout << "You chose incorrectly\n";
    }
}

void checkSeat()
{
    init();

    //answer is a string in this function, so need to give it another name
    string reply;
    
    cout << "Do you want a specific seat?(yes/no: ";
    cin >> reply;

//this block of "if" is not really correct, a seat should be chosen randomly if answer is no.
    if( reply == "yes" )
    {
        //again, this is not a good conditional expression
        //if (seat[rows][columns] = '0')
        //you will need to check here if theseat the user specified is taken or not.
        //inserted temporary condition to make it compile
        if( bool seattaken = false )
        {			 
            seat[rows][columns] = 'X';
            name[rows][columns] = sname;
        }
        else
        {
            //choosing a seat randomly
            int x = rand()%rows;
            int y = rand()%columns;

            //you should do a check here if the random seat is taken or not
            //if it is you need to do it again
            //seat[x][y] to use.
        }
   }
}

void printTicket()
{
    //you were missing some cout's here:
    cout <<"Name: " << name << endl;
    cout << "Seat No: " << seat << endl;
    cout << "Ticket type: " << type << endl;
    cout << "price: " << price << endl;
}

void displaySeating()
{

}
Last edited on
Topic archived. No new replies allowed.