Build a C++ program to read the temperature in centigrade and display a suitable message according to the temperature state below: (nested-if else/switch case)
Temp <0 then freezing weather
Temp 0-10 then very cold weather
Temp 10-20 then cold weather
Temp 20-30 then normal in Temp
Temp 30-40 then it is hot
Temp >=40 then it is very hot
Example Input: 42
Expected Output: It is very hot.
#include <iostream>
using namespace std;
int main() {
int Temp;
cout<<"Enter the temperature in centigrade: ";
cin>> Temp;
if(!cin){ // validation
cout<<"Invalid input. Must be int number"<<endl;
cin.clear(); // clears the error flag
exit(1);
}
if(Temp<0)
cout << "freezing weather";
else if(Temp<10)
cout << "very cold weather";
else if(Temp<20)
cout<<"cold weather";
else if(Temp<30)
cout<<"normal in Temp";
else if(Temp<40)
cout<<"it is hot";
else
cout<<"it is very hot";
cout<<endl;
return 0;
}
Comments
Leave a comment