I ran into a weird issue causes infinite loop

Hi I'm pretty new to programming I'm taking a c++ curse at university.
will now i'm working on my final project for a while now ,after I finished i started testing for a bit.
everything is working fine except when I enter a character instead of a number (at the end of the do loop) the program panics and goes for infinite loop.

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
#include <iostream> 
#include <fstream>
using namespace std;
double conv_o(double a);
double conv_co(double b);
int main()
{ int choice,dec;
double o,co,no,ppm,nco;
string a;
ofstream fin;
fin.open("AQEP.txt");
cout<<"\t\t welcome to the AQEP"<<endl<<endl; 
fin<<"\t\t welcome to the AQEP"<<endl<<endl;
do
{ 
cout<<"enter the name of the location"<<endl;
cin>>a;
	cout<<"to evaluate ozone levels please enter 1\n to evaluate co levels please enter 2 \n";
	cin>>choice;
	switch (choice){
       case 1 : cout<<"enter the calculated o3 levels in ppm"<<endl;
       cout<<"if the calculated levels are in mg/m3 press 0 to convert it to ppm \n";
       cin>>o;
       if(o==0){
       	cout<<"please enter the value\n";
       	cin>>no;
       	o=conv_o(no);
       	cout<<"the value in ppm is "<<o<<endl;
	   }
	   fin<<"the ozone value for "<<a<<" is "<<o<<endl;
	 
        if (0<o&&o<0.064){
		
       cout<<"the air quality is healthy"<<endl;
       fin<<"the air quality is healthy"<<endl;}
        else if (0.064<o&&o<0.084){
		
       cout<<"the air quality is moderate"<<endl;
       cout<<"the air quality is moderate"<<endl;}
        else if (0.084<o&&o<0.104){
		
       cout<<"the air quality is unhealthy for sensetive"<<endl;
       fin<<"the air quality is unhealthy for sensetive"<<endl;}
        else if (0.104<o&&o<0.124){
		
       cout<<"the air quality is unhealthy"<<endl;
       fin<<"the air quality is unhealthy"<<endl;}
        else if (0.124<o&&o<0.374){
	
       cout<<"the air quality is very unhealthy"<<endl;
       fin<<"the air quality is very unhealthy"<<endl;}
        else if (0.374<o){
		
       cout<<"the air quality is hazardous"<<endl;
       fin<<"the air quality is hazardous"<<endl;}
       else
       cout<<"error"<<endl;
       break;
       case 2 : cout<<"enter the calculated co levels in ppm"<<endl;
       cout<<"if the calculated levels are in mg/m3 press 0 to convert it to ppm \n";
       cin>>co;
       if(co==0){
       	cout<<"please enter the value\n";
       	cin>>nco;
       	co=conv_co(nco);
       	cout<<"the value in ppm is "<<co<<endl;
	   }
	   fin<<"the co value for "<<a<<" is "<<co<<endl<<"and ";
	   
        if (0<co&&co<4.4){
		
       cout<<"the air quality is healthy"<<endl;
       fin<<"the air quality is healthy"<<endl;}
        else if (4.4<co&&co<9.4){
		
       cout<<"the air quality is moderate"<<endl;
        fin<<"the air quality is moderate"<<endl;}
        else if (9.4<co&&co<12.4){
		
       cout<<"the air quality is unhealthy for sensetive"<<endl;
       fin<<"the air quality is unhealthy for sensetive"<<endl;}
        else if (12.4<co&&co<15.4){
		
       cout<<"the air quality is unhealthy"<<endl;
       fin<<"the air quality is unhealthy"<<endl;}
        else if (15.4<co&&co<30.4){
		
       cout<<"the air quality is very unhealthy"<<endl;
       fin<<"the air quality is very unhealthy"<<endl;}
        else if (30.4<co){
		
       cout<<"the air quality is hazardous"<<endl;
       fin<<"the air quality is hazardous"<<endl;}
       else{
	   
       cout<<"error"<<endl;}
       break;
        
       
}
cout<<"to enter a new value please enter 0\n";
       cin>>dec;
       
} while (dec==0);

cout<<endl<<endl<<"thank you for using AQEP";

return 0;
}

double conv_o(double a){
	double oo;
	oo=a*22.2/48;
	return oo;
}
double conv_co(double b){
	double coo;
	coo=b*22.2/28;
	return coo;
}


the code is messy I know but I will highlight the problem.

1
2
cout<<"to enter a new value please enter 0\n";
       cin>>dec;

I tried adding dec=0 at the start of the while loop and it looped only once is there a way to make it not loop at all?
you can do it a couple of ways. you can read in a string, convert it to an integer, and if that is not good make the user re-enter until you get what you need.

or you can read in a character, just 1, and change the switch on choice against '1' '2' instead of 1,2 etc (char '1' not value integer 1).

general cleanup:
consider doing this format
case
function1()
case
function2()
instead of the big wads of code in the case statements.

cout<<"the air quality is moderate"<<endl;
cout<<"the air quality is moderate"<<endl;
looks like a bug, did you mean fin?

you can probably find a way to make the redundant string stuff more compact and cleaner looking.

you can chain this type of comparison. say you want values for an unsigned integer 0-5, 6-10, and 15+
then you can write logic
if x < 6
else //its greater than 5 now, known!
if x < 11
else //its greater than 10 now, known...
...
instead of double ended comparisons on each term.
if(time == money && comparisons.take_time())
cout << "extra comparisons cost money!"

Last edited on
thank you for your input i'll try to do it.
and yes i meant fin thanks for that too.
and
if x < 6
else //its greater than 5 now, known!
if x < 11
else //its greater than 10 now, known...
...
this does make much more sense I don't how I missed it.
i'll try to clean up the code a bit more .
and I think i'm ready to present it .
thanks again .
best of luck..
Topic archived. No new replies allowed.