problem with arrays/loops

My program compiles but does not do what I want it to do. It is supposed to display a seating chart to a user. The user can pick to select a seat or to select a price. Once they are assigned a seat (from one of the prior two methods) the seat they chose should have the price changed to 0, thus having it not able to be chosen.

Here is 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
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
//Purpose: Displays a ticket pricing 2D array

#include <iostream>
#include <iomanip>

using namespace std;
const int row = 9;		//sets a global variable for number of rows
const int col = 10;		//sets a global variable for number of columns

//function displays the current seating chart
void seat_display(int seats[row][col])
{
	
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			cout<< setw(7) << seats[i][j];
		}
		cout<< endl;
		cout<< endl;
	}
}
//function allows user to pick a seat number, enter a price, or quit the program
int select_seat()
{
	int select;
	cout << "Press 1 to enter seat number, 2 to enter a price, or 3 to quit: ";
	cin >> select;
	return select;
}


int main()
{
	int select;
	int seats[row][col] = //sets the seating chart
	{
		{10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
		{10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
		{10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
		{10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
		{10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
		{10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
		{20, 20, 30, 30, 40, 40, 30, 30, 20, 20 },
		{20, 30, 30, 40, 50, 50, 40, 30, 30, 20 },
		{30, 40, 50, 50, 50, 50, 50, 50, 40, 30 },
	};

	do
	{
		do
		{

	 


		seat_display(seats); //displays the seating chart to user
		select = select_seat();

			if (select == 1)//user enters a seat number
			{
				int i = 0;
				int j = 0;

				int rownumber; //for row
				int colnumber; //for column

				cout << "Please enter the row you would like to sit in: ";
				cin >> rownumber;
				while (rownumber >= 0 && rownumber < 9)
				{
					cout << "Please enter the seat you would like to sit in: ";
					cin >> colnumber;
					while (colnumber >= 0 && colnumber < 9)
					{
						do
						{
							if (seats[rownumber][colnumber] != 0)
							{

								cout << "You have selected seat "<< seats[i][j]<< "."<<endl;
								seats[i][j] = 0;
							}
							else 
							{
								cout << "That seat is taken."<<endl;
							}

						}while (colnumber >= 0 || colnumber < 10);
					}
				}
			}
			else if (select == 2)  //user enters a price
			{
				int seatprice; //variable that stores the price of the seat
				cout << "Please enter a seat price: ";
				cin >> seatprice;

				int i = 9;
				int j = 10;
				bool found = true;

				while (i >= 0 && !found)
				{
					while (j >= 0 && !found)
					{
						if (seats[i][j] == seatprice)
						{
							cout << "you are sitting in row " << i << ", column " << j << "." <<endl;
							seats[i][j] = 0;
							found = true;
						}
						j--;
					}
					i--;
				}
				if (!found)
				{
					cout << "All seats at this price are taken.";
				}
			}
			else if (select == 3) //exits program
			{
				exit(0);
			}
			else if (select > 3 || select < 1)
			{
				cout<<"Please enter 1, 2, or 3."<<endl; //if user enters an invalid input
			}//select = -1;								//they are reprompted
		} while (select!= 3);
	} while (select > 3 || select < 1);
}


When I run the program and press 1, this is what happens:

     10     10     10     10     10     10     10     10     10     10

     10     10     10     10     10     10     10     10     10     10

     10     10     10     10     10     10     10     10     10     10

     10     10     20     20     20     20     20     20     10     10

     10     10     20     20     20     20     20     20     10     10

     10     10     20     20     20     20     20     20     10     10

     20     20     30     30     40     40     30     30     20     20

     20     30     30     40     50     50     40     30     30     20

     30     40     50     50     50     50     50     50     40     30

Press 1 to enter seat number, 2 to enter a price, or 3 to quit: 1
Please enter the row you would like to sit in: 4
Please enter the seat you would like to sit in: 3
You have selected seat 10.
You have selected seat 0.
You have selected seat 0.
You have selected seat 0.
You have selected seat 0.
You have selected seat 0.
You have selected seat 0.
//endless loop
You have^CPress any key to continue . . .


What I would like to happen is that the seat number (ie. 0,0 or 4,3 in this case) is displayed to the user and then the seat that was chosen is set to 0.




When I press 2, the program looks like this:

     10     10     10     10     10     10     10     10     10     10

     10     10     10     10     10     10     10     10     10     10

     10     10     10     10     10     10     10     10     10     10

     10     10     20     20     20     20     20     20     10     10

     10     10     20     20     20     20     20     20     10     10

     10     10     20     20     20     20     20     20     10     10

     20     20     30     30     40     40     30     30     20     20

     20     30     30     40     50     50     40     30     30     20

     30     40     50     50     50     50     50     50     40     30

Press 1 to enter seat number, 2 to enter a price, or 3 to quit: 2
Please enter a seat price: 30
     10     10     10     10     10     10     10     10     10     10

     10     10     10     10     10     10     10     10     10     10

     10     10     10     10     10     10     10     10     10     10

     10     10     20     20     20     20     20     20     10     10

     10     10     20     20     20     20     20     20     10     10

     10     10     20     20     20     20     20     20     10     10

     20     20     30     30     40     40     30     30     20     20

     20     30     30     40     50     50     40     30     30     20

     30     40     50     50     50     50     50     50     40     30

Press 1 to enter seat number, 2 to enter a price, or 3 to quit:


I would like it to pick an available seat for the user, starting from the bottom right and working its way left/up the seats. The seat is then assigned to 0 and cannot be chosen again.




Thanks for reading and more thanks for any help provided.
The do while loop on line 77 will run forever, as well as the while loop on 75, since you never modify colnumber while inside either loop.

And for the second problem, you use i and j when setting the seat price, not the col/row number.
Last edited on
We can start with this error:
[EDIT FD got there before me]

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
            if (select == 1)//user enters a seat number
            {
                int i = 0; 
                int j = 0;


                int rownumber; //for row
                int colnumber; //for column

                cout << "Please enter the row you would like to sit in: ";
                cin >> rownumber;
 

                while (rownumber >= 0 && rownumber < 9)
                {
                    cout << "Please enter the seat you would like to sit in: ";
                    cin >> colnumber;
                    while (colnumber >= 0 && colnumber < 9) //****** There are 10 columns NOT 9 
                    {
                        do
                        {
                            if (seats[rownumber][colnumber] != 0)
                            {
                                
                            /**************************************************
                            You are getting the  seat value from the user int the  
                            rownumber,columnnumber variables
                             BUT you are  using  i, and j (which are 0) to set the seat 
                             ****************************************************/
                                           
                                cout << "You have selected seat "<< seats[i][j]<< "."<<endl;
                                seats[i][j] = 0;
                            }
                            else
                            {
                                cout << "That seat is taken."<<endl;
                            }

                        }
                        while (colnumber >= 0 || colnumber < 10);
                    }
                }
Last edited on
Thanks, however I don't know how to change the do while loops do make it work. I'm new to c++ and struggling in my class hehe.

I did look at the second problem and changed it to

1
2
3
4
5
6
if (seats[i][j] != 0)
							{

								cout << "You have selected seat "<< seats[i][j]<< "."<<endl;
								seats[i][j] = 0;
							}	


however the program doesn't change the initial seat display to reflect that the seat is taken. It also doesn't display the cout statement 'you have selected seat' etc.
thank you gulkan, changed to:

1
2
3
4
5
6
7
8
9
while (colnumber >= 0 && colnumber < 10)
					{
						do
						{
							if (seats[i][j] != 0)
							{

								cout << "You have selected seat "<< seats[i][j]<< "."<<endl;
								seats[i][j] = 0;
You didn't fix it correctly, re-check what guestgulkan said on lines 27-29 in his code.

For the first one, try stepping through the code on a sheet of paper and see what happens. You should be able to spot the error soon enough.
Ok, I think this is correct now, I carefully read over it.

1
2
3
4
5
6
7
8
do
						{
							if (seats[rownumber][colnumber] != 0)
							{

								cout << "You have selected seat "<< seats[rownumber][colnumber]<< "."<<endl;
								seats[rownumber][colnumber] = 0;
							}	


and for the first problem, would it work if i changed the while loop to an if loop?

if (//rownumber is valid)

//code

else

cout << "please enter valid input"<<endl;
Here is the finished product for anyone else that may be having troubles.

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
//Purpose: Displays a ticket pricing 2D array

#include <iostream>
#include <iomanip>

using namespace std;
const int row = 9;		//sets a global variable for number of rows
const int col = 10;		//sets a global variable for number of columns

//function displays the current seating chart
void seat_display(int seats[row][col])
{
	
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			cout<< setw(7) << seats[i][j];
		}
		cout<< endl;
		cout<< endl;
	}
}
//function allows user to pick a seat number, enter a price, or quit the program
int select_seat()
{
	int select;
	cout << "Press 1 to enter seat number, 2 to enter a price, or 3 to quit: ";
	cin >> select;
	return select;
}


int main()
{
	int select;
	int seats[row][col] = //sets the seating chart
	{
		{10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
		{10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
		{10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
		{10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
		{10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
		{10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
		{20, 20, 30, 30, 40, 40, 30, 30, 20, 20 },
		{20, 30, 30, 40, 50, 50, 40, 30, 30, 20 },
		{30, 40, 50, 50, 50, 50, 50, 50, 40, 30 },
	};

	do
	{
		do
		{

	 


		seat_display(seats); //displays the seating chart to user
		select = select_seat();

			if (select == 1)//user enters a seat number
			{
				int i = 0;
				int j = 0;

				int rownumber; //for row
				int colnumber; //for column

				cout << "Please enter the row you would like to sit in: ";
				cin >> rownumber;
				if (rownumber >= 0 && rownumber < 9)
				{
					cout << "Please enter the seat you would like to sit in: ";
					cin >> colnumber;
					system("cls");
					if (colnumber >= 0 && colnumber < 10)
					{

							if (seats[rownumber][colnumber] != 0)
							{

								cout << "You have selected seat "<<rownumber<< ", " <<colnumber<< "."<<endl;
								cout << "The cost of that seat is "<<seats[rownumber][colnumber] <<endl;
								seats[rownumber][colnumber] = 0;
							}	
							else 
							{
								cout << "That seat is taken."<<endl;
							}
					}
				}
			}
			else if (select == 2)  //user enters a price
			{
				int seatprice; //variable that stores the price of the seat
				int i = 8;
				int j = 9;
				bool found = false;
				cout << "Please enter a seat price: ";
				cin >> seatprice;
				system("cls");

				while (i >= 0 && !found)
				{
					j = 9;
					while (j >= 0 && !found)
					{
						if (seats[i][j] == seatprice)
						{
							cout << "you are sitting in row " << i << ", column " << j << "." <<endl;
							seats[i][j] = 0;
							found = true;
						}
						j--;
					}
					i--;
				}
				if (!found)
				{
					cout << "All seats at this price are taken."<<endl;
				}
			}
			else if (select == 3) //exits program
			{
				exit(0);
			}
			else if (select > 3 || select < 1)
			{
				cout<<"Please enter 1, 2, or 3."<<endl; //if user enters an invalid input
			}//select = -1;								//they are reprompted
		} while (select!= 3);
	} while (select > 3 || select < 1);
}



thanks all for the help.
Topic archived. No new replies allowed.