Small issue with output message in object code

Write your question here.
The program is suppose to take number of passenger from int main(),and then check the number of first class,eco class and business class.However on the first iteration the program is showing the same message(line9) twice.
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
 #include <iostream>

using namespace std;

void total(int passenger){
int bui=0,eco=0,first=0;
string type;
for (int i=1;i=passenger;i++){
        cout <<"Enter class:";
getline(cin,type);
if(type== "first class"){
    first++;
}else if(type== "buisiness class"){
bui++;
}else if(type=="economic class"){
eco++;
}
}
cout <<"Total number of first class:"<< first<<endl;
cout <<"Total number of buisiness class:"<<bui<< endl;
cout <<"Total number of economic class:"<<eco<<endl;
}

int main(){
int passenger;
cout << "Enter the number of passenger:";
cin >>passenger;
total(passenger);
return 0;
}
When does this loop end?
1
2
for ( int i=1; i=passenger; i++)
{
The loops end when it has finished iterating the number of passenger.For example if there is 10 passenger it will iterate 10 times.This syntax for(int i =1;i=passenger;i++) is actually the same as
for (int i=0;i<passenger;i++) i was just trying to figure if the issue was in the loop.
This syntax for(int i =1;i=passenger;i++) is actually the same as
for (int i=0;i<passenger;i++)
No, those are not the same thing. Try it yourself to see the difference the behavior.

i was just trying to figure if the issue was in the loop.
What keskiverto just told you is the problem with the loop.

= is doing assignment, in case that isn't clear. But == wouldn't be correct here, either.
Just use <, it's what is the most readable.
Last edited on
Assignment returns value of the left operand after the assignment has completed.
Both operands here are int. Therefore,
1
2
3
i = passenger
// returns same as
passenger

The condition requires a bool value. We have int.
There is implicit conversion from int to bool, but we can be explicit:
1
2
3
if ( passenger )
// is same as
if ( passenger != 0 )

The passenger != 0 is not same as i < passenger

Lets run the loop. Say passenger==42.
* Loop init sets i=1
* Loop condition sets i=42
* 42 is true and body of the loop runs once
* Increment sets i=43
* Loop condition sets i=42
* 42 is true and body of the loop runs again
* Increment sets i=43
...
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
#include <iostream>
#include <string>

using namespace std;

void total(int passenger) {
	int bui = 0, eco = 0, first = 0;
	string type;
	for (int i = 1; i <= passenger; i++) {
		cout << i << " Enter class:";
		getline(cin, type);
		if (type == "first class") {
			first++;
		}
		else if (type == "business class") {
			bui++;
		}
		else if (type == "economic class") {
			eco++;
		}
	}
	cout << "Total number of first class:" << first << endl;
	cout << "Total number of business class:" << bui << endl;
	cout << "Total number of economic class:" << eco << endl;
}

int main() {
	int passenger;
	cout << "Enter the number of passengers:";
	cin >> passenger;
	cin.ignore();
	total(passenger);
	return 0;
}
Thank you so much all,for your time and help it works finally :)
Topic archived. No new replies allowed.