need help ! How to update the function ??

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

char ray[10][3] = { 0 };

//void printForm(char ray[][], int row, char column);
void getData(int& row, int& column);
void seatmap(int row, int column);

int main()
{
char choice;
int numSeatsFC, numSeatsBC ;
int totalfc = 9, totalbc = 21;
int row, column;
cout << "The airplane has 10 rows, with 3 seats in each row.\n";
cout << "0 indicates that the seat is still available,\n";
cout << "1 indicates that the seat has already occupied.\n";

cout << endl;
cout << setw(17)<<"Column";
cout << endl;
cout << setw(11) << "#1" << setw(4) << "#2" << setw(4) << "#3";

for (int i = 0; i < 10; i++)
{
cout << endl;
cout <<"Row"<<setw(2)<<setprecision(4)<<"#"<<(i + 1)<<setw(3);
for (int j = 0; j < 3; j++)
{
cout<<setw(4)<< ray[i][j];
}
}

cout << endl;

do
{
cout << "Enter the class of the ticket.\n";
cout << "1/F - First class\n";
cout << "2/B - Business class\n";
cin >> choice;

if (choice == '1' || choice == 'F')
{
cout << "There are 9 seats available.\n";
cout << "How many you want to purchase?";
cin >> numSeatsFC;
cout << "Number of tickets to be purchase:\n" << "First class" << setw(4) << numSeatsFC << endl;
totalfc -= numSeatsFC;
break;
}
else if (choice == '2' || choice == 'B')
{
cout << "There are 21 business class seats available.\n";
cout << "How many you want to purchase?";
cin >> numSeatsBC;
cout << "Number of tickets to be purchase:\n" << "Business class" << setw(4) << numSeatsBC << endl;
totalbc -= numSeatsBC;
break;
}
} while (choice != '1' || choice != 'F' || choice != '2' || choice != 'B');

cout << "Number of tickets left:\n";
cout << "First class"<< totalfc<< endl;
cout << "Business class"<< totalbc << endl;

cout << "Select one of the options.\n";
cout << "0/X - Exit Program\n";
cout << "1/F - Plane Depart\n";
cout << "2/M - Show Seat Map";
cout << "Any other key - next customer\n";
cin >> choice;

if (choice == '2' || choice == 'M')
{
seatmap(row, column);
}
return 0;
}


void getData(int& row, int& column)
{
cout << "Enter the row you want to sit<From row 4 to 10> " << endl;
cin >> row;
cout << "Enter the seat number (from A to F). " << endl;
cin >> column;
if (row < 4 && row >10)
{
cout << "Wrong input" << endl;
cout << "Enter the row you want to sit<From row 4 to 10> " << endl;
cin >> row;
}
}

void seatmap(int row, int column)
{
int i, j;

if (ray[row - 1][(column - 65)] == '1')
{
cout << "This seat already assigned. Choose another seat: " << endl;
cin >> column;

}
ray[row - 1][(column)-65] = '1';

cout << "0 indicates that the seat is available; " << endl;
cout << "1 indicates that the seat is occupied. " << endl;

for (i = 0; i < 10; i++)
{
cout << left << setw(3) << "Row " << setw(2) << i + 1;
for (j = 0; j < 3; j++)
{
cout << right << setw(6) << ray[i][j];
}
cout << endl;
}

}
Last edited on
closed account (E0p9LyTq)
You have lots of problems with your code, number one being you need to use code tags to make it easier to read.

http://www.cplusplus.com/articles/jEywvCM9/

HINT, you can go back and edit your post to add code tags.



Thanks for your reply FurryGuy !!!
i saw you replied a lot of question with very useful advice and I am still wondering will you lend me your hand !
I will go and check again and can you give me some advice if i have any other question because i am really weak in this
1
2
3
4
5
6
7
8
9
10
11
if (choice == '2' || choice == 'M')
	{
		seatmap( ray[10][3] , row , column);
	}


This is what i actually put inside the () but the error list show me 3 errors
Error	1	error C2660: 'seatmap' : function does not take 3 arguments
	2	IntelliSense: argument of type "char" is incompatible with parameter of type "char(*)[3]"	
	3	IntelliSense: too many arguments in function call	
	
closed account (E0p9LyTq)
You can't define user-created functions inside main(), you need to move seatmap() and getData() outside main(). Either place the functions above main() so you don't need function declarations, or below main().

Remove the ; from your function definitions, you are declaring functions twice without defining the actual code.

Where is the function definition for printForm()?

Your parameters do not match in the declarations/definitions of seatmap() or getData().

After bashing your code around a lot I was able to get it to compile, I did NOT test it to see if it actually works as intended.

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

char ray[10][3] = { 0 };

//void printForm(char ray[][], int row, char column);
void getData(int& row, int& column);
void seatmap(int row, int column);

int main()
{
   char choice;
   int numSeatsFC, numSeatsBC ;
   int totalfc = 9, totalbc = 21;
   int row, column;
   cout << "The airplane has 10 rows, with 3 seats in each row.\n";
   cout << "0 indicates that the seat is still available,\n";
   cout << "1 indicates that the seat has already occupied.\n";

   cout << endl;
   cout << setw(17)<<"Column";
   cout << endl;
   cout << setw(11) << "#1" << setw(4) << "#2" << setw(4) << "#3";

   for (int i = 0; i < 10; i++)
   {	
      cout << endl;
      cout <<"Row"<<setw(2)<<setprecision(4)<<"#"<<(i + 1)<<setw(3);
      for (int j = 0; j < 3; j++)
      {
         cout<<setw(4)<< ray[i][j];
      }
   }

   cout << endl;

   do
   {
      cout << "Enter the class of the ticket.\n";
      cout << "1/F - First class\n";
      cout << "2/B - Business class\n";
      cin >> choice;

      if (choice == '1' || choice == 'F')
      {
         cout << "There are 9 seats available.\n";
         cout << "How many you want to purchase?";
         cin >> numSeatsFC;
         cout << "Number of tickets to be purchase:\n" << "First class" << setw(4) << numSeatsFC << endl;
         totalfc -= numSeatsFC;
         break;
      }
      else if (choice == '2' || choice == 'B')
      {
         cout << "There are 21 business class seats available.\n";
         cout << "How many you want to purchase?";
         cin >> numSeatsBC;
         cout << "Number of tickets to be purchase:\n" << "Business class" << setw(4) << numSeatsBC << endl;
         totalbc -= numSeatsBC;
         break;
      }
   } while (choice != '1' || choice != 'F' || choice != '2' || choice != 'B');

   cout << "Number of tickets left:\n";
   cout << "First class"<< totalfc<< endl;
   cout << "Business class"<< totalbc << endl;

   cout << "Select one of the options.\n";
   cout << "0/X - Exit Program\n";
   cout << "1/F - Plane Depart\n";
   cout << "2/M - Show Seat Map";
   cout << "Any other key - next customer\n";
   cin >> choice;

   if (choice == '2' || choice == 'M')
   {
      seatmap(row, column);
   }
   return 0;
}


void getData(int& row, int& column)
{
   cout << "Enter the row you want to sit<From row 4 to 10> " << endl;
   cin >> row;
   cout << "Enter the seat number (from A to F). " << endl;
   cin >> column;
   if (row < 4 && row >10)
   {
      cout << "Wrong input" << endl;
      cout << "Enter the row you want to sit<From row 4 to 10> " << endl;
      cin >> row;
   }
}

void seatmap(int row, int column)
{
   int i, j;

   if (ray[row - 1][(column - 65)] == '1')
   {
      cout << "This seat already assigned. Choose another seat: " << endl;
      cin >> column;

   }
   ray[row - 1][(column)-65] = '1';

   cout << "0 indicates that the seat is available; " << endl;
   cout << "1 indicates that the seat is occupied. " << endl;

   for (i = 0; i < 10; i++)
   {
      cout << left << setw(3) << "Row " << setw(2) << i + 1;
      for (j = 0; j < 3; j++)
      {
         cout << right << setw(6) << ray[i][j];
      }
      cout << endl;
   }

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



//void printForm(char ray[][], int row, char column);
void getBC(int& row, int& column);
void getFC(int& row, int& column);
void seatmap();

int main()
{	
	int ray[10][3] = { 0 };
	char choice;
	int numSeatsFC, numSeatsBC;
	int totalfc = 9, totalbc = 21;
	int row, column;
	cout << "The airplane has 10 rows, with 3 seats in each row.\n";
	cout << "0 indicates that the seat is still available,\n";
	cout << "1 indicates that the seat has already occupied.\n";

	cout << endl;
	cout << setw(17) << "Column";
	cout << endl;
	cout << setw(11) << "#1" << setw(4) << "#2" << setw(4) << "#3";

	for (int i = 0; i < 10; i++)
	{
		cout << endl;
		cout << "Row" << setw(2) << setprecision(4) << "#" << (i + 1) << setw(3);
		for (int j = 0; j < 3; j++)
		{
			cout << setw(4) << ray[i][j];
		}
	}

	cout << endl;

	do
	{
		cout << "Enter the class of the ticket.\n";
		cout << "1/F - First class<Row 1 to 3>\n";
		cout << "2/B - Business class<Row 4 to 10>\n";
		cin >> choice;

		if (choice == '1' || choice == 'F')
		{
			cout << "There are 9 seats available.\n";
			cout << "How many you want to purchase?";
			cin >> numSeatsFC;
			for (int k = 0; k < numSeatsFC; k++)
			{
					getFC(row, column);
			}
			
			cout << "Number of tickets to be purchase:\n" << "First class" << setw(4) << numSeatsFC << endl;
			totalfc -= numSeatsFC;
			break;
		}
		else if (choice == '2' || choice == 'B')
		{
			cout << "There are 21 business class seats available.\n";
			cout << "How many you want to purchase?";
			cin >> numSeatsBC;
			for (int k = 0; k < numSeatsBC; k++)
			{
				getBC(row, column);
			}
			
			cout << "Number of tickets to be purchase:\n" << "Business class" << setw(4) << numSeatsBC << endl;
			totalbc -= numSeatsBC;
			break;
		}
	} while (choice != '1' || choice != 'F' || choice != '2' || choice != 'B');

	cout << "Number of tickets left:\n";
	cout << "First class" << totalfc << endl;
	cout << "Business class" << totalbc << endl;

	cout << "Select one of the options.\n";
	cout << "0/X - Exit Program\n";
	cout << "1/F - Plane Depart\n";
	cout << "2/M - Show Seat Map";
	cout << "Any other key - next customer\n";
	cin >> choice;

		if (choice == '2' || choice == 'M')
		{
			seatmap();
		}
		return 0;
	}


void getBC(int& row, int& column)
{

	cout << "Enter the row you want to sit<From row 4 to 10> " << endl;
	cin >> row;

	while (row < 4 || row > 10)
	{
		cout << "Wrong input" << endl;
		cout << "Enter the row you want to sit<From row 4 to 10> " << endl;
		cin >> row;
	}
	cout << "Enter the column number (from 1 to 3). " << endl;
	cin >> column;
	while (column < 1 || column > 3)
	{
		cout << "Wrong input" << endl;
		cout << "Enter the column you want to sit<From column 1 to 3> " << endl;
		cin >> column;
	}
}


void getFC(int& row, int& column)
{	
	cout << "Enter the row you want to sit<From row 1 to 3> " << endl;
	cin >> row;

	while (row < 1 || row > 3)
	{
		cout << "Wrong input" << endl;
		cout << "Enter the row you want to sit<From row 1 to 3> " << endl;
		cin >> row;
	}
	cout << "Enter the column number (from 1 to 3). " << endl;
	cin >> column;
	while (column < 1 || column > 3)
	{
		cout << "Wrong input" << endl;
		cout << "Enter the column you want to sit<From column 1 to 3> " << endl;
		cin >> column;
	}
}

void seatmap(int&row , int&column)
{
	
	for (int i = 0; i < 10; i++)
	{
		cout << endl;
		cout << "Row" << setw(2) << setprecision(4) << "#" << (i + 1) << setw(3);
		for (int j = 0; j < 3; j++)
		{
			cout << setw(4) << ray[i][j];
		}
	}
	}


I have modified my code and all the things go smoothly except the function of seatmap
what should i change if i want to update the seat taken by others from 0 to 1 ?
all the things go smoothly

I don't know how you can say that when the code you posted doesn't even compile.

Line 11,91,141: Your function declaration, function call and function definition do not agree.

Line 29-37: Why don't you call seatmap() here?

Line 50,64: The couts reflect the total number of seats in the section, not the number of seats currently available in the section.

Line 53,67: What if the number of seats to purchase is > than the number of seats available?

Line 76: Your while condition is faulty. if the choice is 'F', then != '1' is going to be true. You want:
 
  while ( ! (choice == '1' || choice == 'F' || choice == '2' || choice == 'B'));


Lines 97-117,120-139: These two functions are nearly identical. You should consider consolidating them, or using common sub functions (get_col, get_row).

Line 141: row and col do not need to be passed to seatmap(). They are not used. seatmap().

Line 150: ray is not defined. It needs to be passed in as an argument.

except the function of seatmap what should i change if i want to update the seat taken by others from 0 to 1 ?

You should not change the contents of ray inside seatmap(). seatmap() should do one thing - Display the contents of ray.
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
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

const int MAX_ROWS = 10;
const int MAX_COLS = 3;

//void printForm(char ray[][], int row, char column);
void getBC (int ray[MAX_ROWS][MAX_COLS]);
void getFC (int ray[MAX_ROWS][MAX_COLS]); 
void seatmap(int ray[MAX_ROWS][MAX_COLS]);

int get_int (int min, int max) 
{   int     num;

    while (true)
    {   cin >> num;
        if (num >= min && num <= max)
            return num;
        cout << "Invalid number" << endl;            
    }
}
    
int main()
{   int ray[MAX_ROWS][MAX_COLS] = { 0 };
	char choice;
	int numSeatsFC, numSeatsBC;
	int totalfc = 9, totalbc = 21;
	cout << "The airplane has 10 rows, with 3 seats in each row.\n";
	cout << "0 indicates that the seat is still available,\n";
	cout << "1 indicates that the seat has already occupied.\n";

	seatmap (ray);
	do
	{   cout << "Enter the class of the ticket.\n";
		cout << "1/F - First class<Row 1 to 3>\n";
		cout << "2/B - Business class<Row 4 to 10>\n";
		cin >> choice;

		if (choice == '1' || choice == 'F')
		{   cout << "There are " << totalfc << " seats available.\n";
			cout << "How many you want to purchase?";
			numSeatsFC = get_int (1, totalfc);
			for (int k = 0; k < numSeatsFC; k++)
			{   getFC (ray);
			}			
			totalfc -= numSeatsFC;
			break;
		}
		else if (choice == '2' || choice == 'B')
		{   cout << "There are " << totalbc << " business class seats available.\n";
			cout << "How many you want to purchase?";
			numSeatsBC = get_int (1, totalbc);
			for (int k = 0; k < numSeatsBC; k++)
			{   getBC (ray);
			}		
			totalbc -= numSeatsBC;
			break;
		}
	} while (! (choice == '1' || choice == 'F' || choice == '2' || choice == 'B'));

	cout << "Number of tickets left:\n";
	cout << "First class" << totalfc << endl;
	cout << "Business class" << totalbc << endl;

	cout << "Select one of the options.\n";
	cout << "0/X - Exit Program\n";
	cout << "1/F - Plane Depart\n";
	cout << "2/M - Show Seat Map";
	cout << "Any other key - next customer\n";
	cin >> choice;

	if (choice == '2' || choice == 'M')
	{   seatmap(ray);
	}
	system ("pause");
	return 0;
}

bool reserve_seat (int ray[MAX_ROWS][MAX_COLS], int row, int col)
{   if (ray[row-1][col-1])
    {   cout << "Seat is occupied" << endl;
        return false;
    }
    ray[row-1][col-1] = 1;
    return true;
}
            
void getBC(int ray[MAX_ROWS][MAX_COLS])
{   int     row, col;

    do
    {   cout << "Enter the row you want to sit<From row 4 to 10> " << endl;
	    row = get_int (4, 10); 
	    cout << "Enter the column number (from 1 to 3). " << endl;
	    col = get_int (1,3);
    } while (! reserve_seat (ray, row, col));
}

void getFC (int ray[MAX_ROWS][MAX_COLS])
{   int     row, col;

    do
    {	cout << "Enter the row you want to sit<From row 1 to 3> " << endl;
	    row = get_int (1, 3);
	    cout << "Enter the column number (from 1 to 3). " << endl;
	    col = get_int (1, 3);
    } while (! reserve_seat (ray, row, col));	    
}

void seatmap(int ray[MAX_ROWS][MAX_COLS])
{   for (int i = 0; i < MAX_ROWS; i++)
	{   cout << endl;
		cout << "Row" << setw(2) << setprecision(4) << "#" << (i + 1) << setw(3);
		for (int j = 0; j < MAX_COLS; j++)
		{   cout << setw(4) << ray[i][j];
		}
	}
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Thanks for your help !! I read all your advice and use few hours to google it one by one. I understand what are you trying to help me but one thing I dont really understand is why we need to put 


int get_int (int min, int max) 
{   int     num;

    while (true)
    {   cin >> num;
        if (num >= min && num <= max)
            return num;
        cout << "Invalid number" << endl;            
    }
}

before int main () ?
I try to put it inside the main program and the whole pragram are not running .
I try to put it inside the main program and the whole pragram are not running .

You can't define one function inside another. Put it before or after. If you put it after, you need a function declaration for it.

I get it ! Thanks for your help AbstractionAnon !
Topic archived. No new replies allowed.