Airplane reservations problem

for this program I need to ask the customer if they would like to travel first class or economy by press 1 or 2. After that they must put in a seat number to fill in (the plane has 10 seats total and 1-5 are first class). If one input is put in more than once it will say the seat is taken(I solved that out). The problem I am having is with notifying the customer when first class is full and ask if riding economy is ok. I tried to put in an input counter to where if you put in more than five integer it will notify you,but doesn't work. Also the "do while" loop in the main function isn't working for some reason. I might need a second eye to figure these problems out.

P.S. I didn't do the rest of the coding yet because I want to figure this out first
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
#include <string>
#include <iostream>
#include <stdlib.h>
using namespace std;

void display(int & choice);
void first(int & choice,int seats[],char & anwser,int & counter);

int i, j;
int main()
{
	char anwser;
	int i, j, counter;
	int choice, seats[10];
	do {
		display(choice);
		first(choice, seats,anwser,counter);
	} while (toupper (anwser) == 'Y');//loop not working correctly
	

}

void display(int & choice)
{
	cout << "Welcome! Press 1 for first class or 2 for economic" << endl;
	cin >> choice;
}

void first(int & choice,int seats[],char & anwser,int & counter)
{
	if (choice == 1)
	{
		
		for (j = 0;j < 10;j++)
		{
			cout << "Welcome to first class!" << endl;
			cout << "seats 1-5 are available" << endl;
			
			do
			{
				counter = 0;
				cout << "Pick a seat: ";
				cin >> seats[j];//represents perfered seat 
				counter++;//input counter isn't working 
				for (i = 0;i < j;i++)
					if (seats[j] == seats[i])//seats[i] respresents a taken seat
					{
						cout << "Seat taken try again" << endl;
					}
				
				if (counter >= 5)//input count should display this 
					cout << "No more seats in first class" << endl;

			} while (i != j);

			cout << "Do you want to go again?" << endl;
			cin >> anwser;

		}
		
		
	}
}
Last edited on
Consider writing a utility function to tell you how many first class seats are still available, before prompting for a seat number.

1
2
3
4
5
6
if ( numSeatsAvailable(seats) > 0 ) {
    cout << "Pick a seat: ";
    // etc
} else {
    cout << "No more seats in first class" << endl;
}
Hello Deadweight77,

To use the "std::tolower()" or "std::toupper" you need the header file "cctype".

Just a thought here maybe two arrays, one for first class and one for coach. If you can use a vector it would make it easier.

Line 44 says "counter++; //input counter isn't working" along with the variables "i" and "j". To you "i" and "j" along with "counter" mean something to you, but I have a hard time trying to figure out what they are used for. Especially "counter" What is it counting? Better names would help immensely.

Consider this thinking out loud. How about a counter for first class and a counter for coach. this way when these counters reach five the section is full. And salem c's if statement might look like if (firstClassSeets < MAXFCSEETS). With "MAXFCSEETS" defined as constexpr int MAXFCSEETS{ 5 };. Just some thoughts for now.

I will load up what you have and try to figure out what is what.

Hope that helps,

Andy
Counter is used to keep track of how many times someone puts in an input. So when somebody tries to put in different numbers ranging from 1-5 the 6th integer they put in would prompt that there is no more room in first class,but Ill make some naming adjustments.

Last edited on
UPDATE: So I made some changes to better read it hopefully and tried some of the advice,but I'm still struggling since I'm still trying to find a way to make a counter(the counter name has been changed to fcseats or first class seats). There is also a word called "wanted" meaning the seat you picked or wanted and "taken" are the seats filled or taken already. Also the "do while loop" still isn't working. I did it perfectly fine on previous projects, but it's not working still.


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
#include <string>
#include <iostream>
#include <stdlib.h>
#include<cctype>
using namespace std;

void display(int & choice);
void first(int & choice, int seats[], char & anwser,int & fcseats);

int wanted, taken;
constexpr int MAXFCSEATS{ 5 };//this is first class seats at an constant expression of 5 
int main()
{
	char anwser;
	int wanted,taken, fcseats;
	int choice, seats[10];
	do {
		display(choice);
		first(choice, seats, anwser,fcseats);
	} while (toupper(anwser) == 'Y');


}

void display(int & choice)
{
	cout << "Welcome! Press 1 for first class or 2 for economic" << endl;
	cin >> choice;
}

void first(int & choice, int seats[], char & anwser,int & fcseats)
{
	if (choice == 1)
	{

		for (wanted = 0;wanted < 10;wanted++)
		{
			cout << "Welcome to first class!" << endl;
			cout << "seats 1-5 are available" << endl;

			do
			{
				if (fcseats < MAXFCSEATS)
				{
					cout << "pick a seat" << endl;
					cin >> seats[wanted];//means the seat you wanted initaily 
					
				}
				for (taken = 0;taken < wanted;taken++)//taken means a seat that has been taken or filled already
				{
					
					if (seats[wanted] == seats[taken])
						cout << "Seat already taken" << endl;

				}
				
				 if (fcseats > MAXFCSEATS)
					cout << "All seats are taken" << endl;
				
				

			} while (taken != wanted);

			cout << "Do you want to go again?" << endl;
			cin >> anwser;

		}


	}
}
Hello Deadweight77,

You have made some improvements and some are good.

Before "main" I have:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <stdlib.h>
#include <string>
#include <cctype>

//using namespace std;  // <--- Best not to use.

constexpr int MAXFCSAETS{ 5 };
constexpr int MAXCOACHSEATS{ 5 };
constexpr int MAXSEATS{ 11 };  // <--- Changed.

void display(int & choice);
void first(int & choice, bool seats[], int & counter);  // <--- Changed. 

I put the constants first because while testing I passed the array by reference. Also notice that the prototype for "first" is different. I removed passing the variable "answer". I hope you do not mind, but I corrected the spelling of "answer".

Main is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
	char answer{ 'Y' };
	int counter{}, choice{};
	bool seats[MAXSEATS]{};

	do
	{
		display(choice);

		first(choice, seats, counter);

		std::cout << "\nWould you like to continue entering seats? ";
		std::cin >> answer;

	} while (std::toupper(answer) == 'Y');//loop not working correctly

	return 0;
}

You are passing variables to a function. It is a good idea to initialize those variables to eliminate any problems that might occur.

Your "int" array is making thing difficult. The array only needs to know two things either the seat is open or taken. As a "bool" array you can use "0", i.e., false or "1", i.e., true. It will save a lot of work in the function.

Your use of "answer" in "main" and the function is a nice idea, but does not work correctly. It is better not to pass "answer to the function and define "answer" in the function. By defining "answer in the function it is a local variable to the function and separate from the variable in "main".

"counter" and I believe what you now call "fcSeats" is not really needed.

The function "display" is OK for now. It works, but in a menu function I like to use a do/while and only end the function when a valid choice has been entered. In your setup "choice is an "int" and this is a problem if you enter something other than a number. When the input variable is an "int" it is helpful to follow the formatted "cin" with:
1
2
3
4
5
6
7
8
9
10
while (!std::cin)
{
	std::cout << "\n  Invalid Choice! Try again.\n" << std::endl;

	std::cin.clear();
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

	std::cout << "Welcome! Press 1 for first class or 2 for economy ";
	std::cin >> choice;
}

You can change the message on line 8 if you want.

If you type something other than a number "cin" will be in a failed state and unusable until you reset the state bits.

Passing "choice" by reference is not a problem, although I generally return a valid choice. Either way works.

For the function "first" I hope you find this a better approach.
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
void first(int & choice, bool seats[]/*, int & counter*/)
{
	char answer{ 'Y' };

	if (choice == 1)
	{
		size_t index{};

		for (int j = 0; j < MAXFCSAETS; j++)
		{
			std::cout << "\nWelcome to first class!" << std::endl;
			std::cout << "seats 1-5 are available to first class." << std::endl;

			do
			{
					std::cout << "\nPick a seat: ";
					std::cin >> index;//represents perfered seat

					if (index < 1 || index > 5)
						std::cout << "\n  Wrong seat number. Choose 1 to 5.\n" << std::endl;

			} while (index < 1 || index > 5);

			if (!seats[index])
			{
				seats[index] = true;
				//counter++;
			}
			else
			{
				std::cout << "\n  That seat is taken. Please choose another.\n" << std::endl;
				j--; // <--- To redo the for loop counter and not end early.
			}

			std::cout << "\nDo you want to go again? (Y/N) ";
			std::cin >> answer;

			if (std::toupper(answer) != 'Y')
				break;  // <--- Leave for loop.
		}  //  End for loop.
	}  //  End if statement.
}

I put this together rather quickly. There still may be some refinement that could be done.

To give you a leg up; Take everything from the opening { brace to the closing } brace of the if statement and paste it after an else statement. Then with a few small changes you can use it for the economy seats. It should work, but I have not tried it yet.

As a note: The for loop in "first" is working on wanted < 10, but there are only five seats in first class. Why the extra five? As you can see this is a good place to use "MAXFCSAETS".

I offer this as an alternative and hope you find it much simpler than what you started with.

One last note/suggestion "mainMenu" or "displayMenu" is more descriptive than just "display" which could mean many things.

Hope that helps,

Andy
Thank you very much! For some reason however it says every time I put in a seat it says it is taken. Take a look at what I did so far. I tried with yours exactly the way you did it to see if it was error free,but came up with the same problem.

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

#include <string>
#include <iostream>
#include <stdlib.h>
#include<cctype>
using namespace std;

void display(int & choice);
void first(int & choice, bool seats[], char & anwser,int & fcseats,int & counter);

int wanted, taken;
constexpr int MAXFCSEATS{ 5 };//this is first class seats at an constant expression of 5 
int main()
{
	char anwser;
	int wanted, taken, fcseats, counter;
	int choice;
	bool  seats[10];
	do {
		display(choice);
		first(choice, seats, anwser,fcseats,counter);
	} while (toupper(anwser) == 'Y');


}

void display(int & choice)
{
	cout << "Welcome! Press 1 for first class or 2 for economic" << endl;
	cin >> choice;
}

void first(int & choice, bool seats[], char & anwser,int & fcseats,int & counter)
{
	if (choice == 1)
	{


		for (int j = 0;j < MAXFCSEATS;j++)
		{
			cout << "Welcome to first class!" << endl;
			cout << "seats 1-5 are available" << endl;

			
			do {
				cout << "pick a seat" << endl;
				cin >> wanted;//means the seat you wanted initaily 

				if (wanted < 1 || wanted>5)
					cout << "Wrong number input" << endl;
				
			} while (wanted < 1 || wanted>5);
				
			if (!seats[wanted])// The ! is used to reverse it's logical state if something is true it can be turned to false 
			{
				seats[wanted] = true;
				counter++;
			}
			else
			{
				cout << "Seat is taken. Choose another seat number." << endl;
				j--;//to redo the "for" loop counter 
			}

			

			cout << "Do you want to go again?" << endl;
			cin >> anwser;
			if(toupper(anwser!='Y'))
				break;

		}


	}
}
Last edited on
Hello Deadweight77,

Line 11 as a global variable it should be avoided. Right now you only have one function that uses the variable "wanted" and "taken" is never used.

Line 16 defines "wanted" and "taken" as local variables to "main". If they were used they would overshadow the global variables. It would take awhile and most likely someone else to see that the local variables in main are being used and not the global variables.

For the real problem on line 18. I have a feeling that you have the misconception that when using an array that you have to start at zero. This is not true. A good example is:std::string months[12]{"Jan", ..., "Dec"};. This sets up "Jan" as starting in element zero and there are many built in functions that can use this. But sometimes you have a program where the user (or file) will work with "Jan" as being "1" not "0". So you would write the array as: std::string months[13]{"", "Jan", ..., "Dec"};. Notice that the first element is a blank so that "Jan" starts in element "1".

Since your seat numbers are (1 - 5) and (6 - 10) by making the array one larger than what you did allow you to skip element zero. So when you enter a seat number like "3" the result is stored in element "3" and not element 2 as it is with your array.

I know I talked about initializing your variables, so this is what your code looks line on my computer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// <--- Above "main"
constexpr int MAXSEATS{ 11 };

char anwser = -52; // <--- Which might be the character 'ì'.
int wanted = -858993460,
  taken = -858993460,
  fcseats = -858993460,
  counter = -858993460;
int choice = -858993460;
bool  seats[MAXSEATS]
{
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true,
  true
};

In my IDE it is presented to me as:
 
  [0]  | true (204),
  [1]  | true (204),
  [2]  | true (204),
  [3]  | true (204),
  [4]  | true (204),
  [5]  | true (204),
  [6]  | true (204),
  [7]  | true (204),
  [8]  | true (204),
  [9]  | true (204),
  [10] | true (204)


What happens here is that the compiler will set aside memory space when the variable is defined. At run time, uninitialized, the program tries to use what information is in the space for that variable.

For the "char" the information stored at that memory location is used as a number that is outside the range of a signed "char", (-128 to 127), which in this case becomes (-52).

Most often an uninitialized "int" ends up with the value of "-858993460". In the case of "counter" you pass this variable to the function and add one which would leave you with "-858993459". One less than what you started with. Not what you want.

And for the array the garbage at the memory locations tends to come out with a value of (204). With a bool normally you have two choices "false" or (0) and "true" or (1), but any value greater than zero is considered "true". So what you have here is the entire array is set to "true" before you even get started.

When you initialize the variables when defined you at least have the knowledge that they do not contain garbage.

When I did this:
1
2
3
4
char anwser{};
int fcseats{}, counter{};
int choice{};
bool  seats[MAXSIZE]{};

The {}s or uniform initializer will set the "char" to "\0", the "int"s to zero and the array to (0) or false. In the case of a "double" it is "0.0". Strings and other containers like a vector are empty to start with and do not need initialized unless you need something there to begin with.

Notice in the above code piece I removed the variables "wanted" and "taken". "taken" is never used in the program anywhere and "wanted" is better defined in the function where it is used. "counter" should be replaced with the variables" fcSeats" and "coachSeats" or "fcCounter" and "coachCounter" as you will eventually need both counters. Trying to use just one "counter" for two separate things is difficult to work with. And yes eventually you will have to pass both counters to the function.

When I initialized the variables especially the array it worked as it should. When you reach the function "first" the array has the value of (0) or false for each element and it starts with every seat as available.

For now I have left "counter" alone, but it should be "fcseats".

Some hints for you:

Using "camelCase" with the second or more words capitalized is the more often used method. Some will use the old DOS underscore to denote a space between words. Or you could just use all lower case letters. Whichever method you choose be consistent. Mixing things can be confusing. And the use of the underscore as a prefix or suffix to a variable name should be left to the header files, the ones that come with the compiler not the ones you write.

Most of your code looks like this:
1
2
int main()
{

except the do/while loops which are:
 
do {

Again be consistent. The first bit of code is much easier to read and work with when the {}s are in the same column and the code is properly indented.

It is still a good idea to verify that the input in "display" is valid before the function ends and returns. Waiting until you get to the function "first" is to late and the wrong place to check the value of choice. Also if you enter a letter for "choice" "cin" fill be in a failed state and unusable until it is reset. Going to a different function will not change this.

Loose line 11, define "wanted" in the function "first" where it is used. There is no need to pass this variable to the function and no reason to keep it in "main".

Initialize your variables in "main" and you will have a lot less problems.

Hope that helps,

Andy
Topic archived. No new replies allowed.