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.
#include <iostream>
usingnamespace 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++;
}elseif(type== "buisiness class"){
bui++;
}elseif(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;
}
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.
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
...