You, as a programmer are required to develop a C++ program that checks the status of Air Pollution Index (API) based on the table below:
API Air Quality Status
0 – 50 Good
51 – 100 Average
101 – 200 Not healthy
201 – 300 Very unhealthy
301 – 500 Danger
over 500 Declare critical condition
If the user input API value less than zero, display a message “API not valid“. You must use a loop so that the program will be looping until the user chooses not to continue. Your program should have a function named IPU_CATEGORY that will check the API category by using if-else statement and then display the result of the air quality status.
#include<iostream>
#include<conio.h>
usingnamespace std;
char IPU_CATEGORY(int API) //Function Prototype
void main()
{
int API;
int a;
char again;
cout<<"Enter the API value: ";
cin>>API;
a = IPU_CATEGORY(API); // Calling Function
cout<<"\nDo you want to continue? (Y/N) : ";
cin>>again;
cout<<endl;
while(again=='Y'||again=='y');
cout<<endl;
_getch();
}
char IPU_CATEGORY(int API) //Function Defination
{
int IPU_CATEGORY;
IPU_CATEGORY = API;
{
if (API<0)
cout<<"API not valid"<<endl;
elseif (API>=0 && API<=50)
cout<<"The air quality status is Good"<<endl;
elseif (API>=51 && API<=100)
cout<<"The air quality status is Average"<<endl;
elseif (API>=101 && API<=200)
cout<<"The air quality status is Not healthy"<<endl;
elseif (API>=201 && API<=300)
cout<<"The air quality status is Very unhealthy"<<endl;
elseif (API>=301 && API<=500)
cout<<"The air quality status is Danger"<<endl;
elseif (API>500)
cout<<"The air quality status is Declare critical condition"<<endl;
}
return IPU_CATEGORY;
}
My confusion:
does i need to use for or while loop?? my understanding of both loop;
–If you know or can determine in advance the number of repetitions needed, the forloop is the correct choice
–If you do not know and cannot determine in advance the number of repetitions needed, and it could be zero, use a whileloop
your understanding is correct (even though these loops are interchangeable)
you need a while loop.
I suggest a while(true) loop. break from it if input is valid.