Loop Problems

Having trouble making this while statement work. It should end the loop when I enter Z but instead it keeps looping. Any suggestions? Thanks.

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

const char SENTINEL = 'Z';
char zone;
float age;
int total_residents;
int E;
int M;
int P;
int C;
float show_percent(float a, float b);
int main()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);


cout<<"Enter the time zone: E, C, M or P. Press "<<SENTINEL<< " to stop" <<endl;

while(zone != SENTINEL)
{
cin>> zone;
if(zone = 'E'){
E++;
total_residents;

}
else if(zone = 'C'){
C++;
total_residents;
} 
else if(zone = 'M'){
M++;
total_residents;
} 
else if(zone = 'P'){
P++;
total_residents;
}
else{
cout<< "invalid input"<<endl;
}
}
cout<<""<<endl;
cout<<"    US RESIDENCY REPORT"<<endl;
cout<<""<<endl;
cout << "Eastern\tCentral\tMountain Pacific" << endl;
cout<< E<<"\t"<< C<<"\t"<< M<<"\t "<< P<< endl;
cout<< show_percent(E,total_residents) <<"\t"<<show_percent(C,total_residents) <<"\t"<< show_percent(M,total_residents) <<"\t"<<show_percent(P,total_residents) <<endl;
}
float show_percent(float a, float b)
{
	
	float c;
	c = a/b;
	return c;
}
For the comparison between two or more values, is used '==' and not '=' because single is an assignment. Then just replace in the if.statement into the while-statement the condition if(zone == 'E')the others too.
Your if statements should be using ==, not =. A single = is for assignment, and this mistake ensures zone is always set to 'E'.
Thanks so much. I forgot about that and now it is working.
Topic archived. No new replies allowed.