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.
#include <iostream>
#include <fstream>
usingnamespace 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;}
elseif (0.064<o&&o<0.084){
cout<<"the air quality is moderate"<<endl;
cout<<"the air quality is moderate"<<endl;}
elseif (0.084<o&&o<0.104){
cout<<"the air quality is unhealthy for sensetive"<<endl;
fin<<"the air quality is unhealthy for sensetive"<<endl;}
elseif (0.104<o&&o<0.124){
cout<<"the air quality is unhealthy"<<endl;
fin<<"the air quality is unhealthy"<<endl;}
elseif (0.124<o&&o<0.374){
cout<<"the air quality is very unhealthy"<<endl;
fin<<"the air quality is very unhealthy"<<endl;}
elseif (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;}
elseif (4.4<co&&co<9.4){
cout<<"the air quality is moderate"<<endl;
fin<<"the air quality is moderate"<<endl;}
elseif (9.4<co&&co<12.4){
cout<<"the air quality is unhealthy for sensetive"<<endl;
fin<<"the air quality is unhealthy for sensetive"<<endl;}
elseif (12.4<co&&co<15.4){
cout<<"the air quality is unhealthy"<<endl;
fin<<"the air quality is unhealthy"<<endl;}
elseif (15.4<co&&co<30.4){
cout<<"the air quality is very unhealthy"<<endl;
fin<<"the air quality is very unhealthy"<<endl;}
elseif (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!"
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..