quick question regarding input counters

For some reason I can't find out why my input counter isn't working. It is supposed to keep track of how many times I enter in a seat and after the 5th input for a seat is should prompt if the customer wants to fly economy class instead of first class since first class only has 5 seats. This is trivial I know, but I don't understand why it doesn't work.
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 <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 & scseats);
void second(int & choice, bool seats[], char & anwser, int & scseats, int & counter, int & fcseats);

const int T_seats = 10;
const int MAXFCSEATS{ 5 };

int main()
{
	char anwser;
	int  fcseats, counter, scseats;
	int choice;
	bool  seats[T_seats] = { false };
	do {
		display(choice);
		first(choice, seats, anwser, fcseats, counter, scseats);
		second(choice, seats, anwser, scseats, counter, 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, bool seats[], char & anwser, int & fcseats, int & counter, int & scseats)
{

	int wanted;

	if (choice == 1)
	{

		counter = 0;//setting it to zero
		cout << "Welcome to first class!" << endl;
		cout << "seats 1-5 are available" << endl;


		do {
			cout << "pick a seat" << endl;
			cin >> wanted;
			counter++;//adding one to everytime I put in a seat or a least it is supposed to 


			if (wanted < 1 || wanted>5)
				cout << "Wrong number input" << endl;

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

		if (!seats[wanted])
		{
			seats[wanted] = true;
		}
		else
		{
			cout << "Seat is taken. Choose another seat number." << endl;

		}
		while (counter > 5)//The following is supposed to happen after the 5th input for a seat
		{

			cout << "First class full would you like to go in economy? Press Y or N" << endl;
			cin >> anwser;
			if (toupper(anwser) == 'Y')
			{
				second(choice, seats, anwser, scseats, counter, fcseats);
			}
			else
			{
				cout << "Next flight is in 3 hours" << endl;
				exit(1);
			}
		}


		cout << "Do you want to go again? Press Y for yes or N for no" << endl;
		cin >> anwser;

	}
}

void second(int & choice, bool seats[], char & anwser, int & scseats, int & counter, int & fcseats)
{
	int wanted;
	if (choice == 2)
	{
		cout << "Welcome to Economy!" << endl;
		cout << "seats 6-10 available" << endl;

		do {
			cout << "pick a seat" << endl;
			cin >> wanted;

			if (wanted < 6 || wanted>10)
				cout << "Wrong number input" << endl;

		} while (wanted < 6 || wanted>10);

		if (!seats[wanted])
		{
			seats[wanted] = true;

		}
		else
		{
			cout << "Seat is taken. Choose another seat number." << endl;

		}

		cout << "Do you want to go again? Press Y for yes or N for no" << endl;
		cin >> anwser;

	}

}
Line 43 is the issue. You set counter to 0 EVERY time the function is called. By the end of the function, it'll equal 1, but once the function is called again it'll be reset to 0. You should set counter to 0 inside int main when it's declared and remove line 43.
ok so I removed line 43 and moved it up to int main,but still doesn't work.
what do you plan to do with count?
if you wanted to count first and second separately you could do this..

int first(everything except count)
{static int count = 0;
count ++;
...
return count;
}

and the same in second.
It looks like you should keep them distinct, as they represent 2 different things?
Just tried it,but didn't work for me
I even tested it out myself, the counter works just fine after removing line 43. Cout the counter after counter++ and you'll see that it's "1" every time. Remove line 43, and you'll see the counter progress past 1.

EDIT: Here's 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
#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 & scseats);
void second(int & choice, bool seats[], char & anwser, int & scseats, int & counter, int & fcseats);

const int T_seats = 10;
const int MAXFCSEATS{ 5 };

int main()
{
	char anwser;
	int  fcseats, counter, scseats;
	counter = 0; // SET COUNTER TO 0 HERE!
	int choice;
	bool  seats[T_seats] = { false };
	do {
		display(choice);
		first(choice, seats, anwser, fcseats, counter, scseats);
		second(choice, seats, anwser, scseats, counter, 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, bool seats[], char & anwser, int & fcseats, int & counter, int & scseats)
{

	int wanted;

	if (choice == 1)
	{

		//counter = 0;//setting it to zero COMMENTED OUT THIS LINE
		cout << "Welcome to first class!" << endl;
		cout << "seats 1-5 are available" << endl;


		do {
			cout << "pick a seat" << endl;
			cin >> wanted;
			counter++;//adding one to everytime I put in a seat or a least it is supposed to 
			std::cout << '\n' << counter << '\n';

			if (wanted < 1 || wanted>5)
				cout << "Wrong number input" << endl;

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

		if (!seats[wanted])
		{
			seats[wanted] = true;
		}
		else
		{
			cout << "Seat is taken. Choose another seat number." << endl;

		}
		while (counter > 5)//The following is supposed to happen after the 5th input for a seat
		{

			cout << "First class full would you like to go in economy? Press Y or N" << endl;
			cin >> anwser;
			if (toupper(anwser) == 'Y')
			{
				second(choice, seats, anwser, scseats, counter, fcseats);
			}
			else
			{
				cout << "Next flight is in 3 hours" << endl;
				exit(1);
			}
		}


		cout << "Do you want to go again? Press Y for yes or N for no" << endl;
		cin >> anwser;

	}
}

void second(int & choice, bool seats[], char & anwser, int & scseats, int & counter, int & fcseats)
{
	int wanted;
	if (choice == 2)
	{
		cout << "Welcome to Economy!" << endl;
		cout << "seats 6-10 available" << endl;

		do {
			cout << "pick a seat" << endl;
			cin >> wanted;

			if (wanted < 6 || wanted>10)
				cout << "Wrong number input" << endl;

		} while (wanted < 6 || wanted>10);

		if (!seats[wanted])
		{
			seats[wanted] = true;

		}
		else
		{
			cout << "Seat is taken. Choose another seat number." << endl;

		}

		cout << "Do you want to go again? Press Y for yes or N for no" << endl;
		cin >> anwser;

	}

}



ALSO: Don't make it a while loop if counter > 5, use an if statement. You've just made an infinite loop!
Last edited on
Topic archived. No new replies allowed.