Combination of Function, Loop And If-Else Statement

My question is as below:

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.

My source code that have been done:
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
#include<iostream>
#include<conio.h>

using namespace 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;
	else
		if	(API>=0 && API<=50)
		cout<<"The air quality status is Good"<<endl;
	else 
		if (API>=51 && API<=100)
			cout<<"The air quality status is Average"<<endl;
	else 
		if (API>=101 && API<=200)
			cout<<"The air quality status is Not healthy"<<endl;
	else 
		if (API>=201 && API<=300)
			cout<<"The air quality status is Very unhealthy"<<endl;
	else 
		if (API>=301 && API<=500)
			cout<<"The air quality status is Danger"<<endl;
	else
		if (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.
where should i put the while loop?? in function or main body??
Topic archived. No new replies allowed.