OK I just started my very first C++ class and I am working on a lab. Here is the code I wrote and a break down of what I'm trying to do.
My code is doing what I want it to do. However what I am trying to figure out is if the users enters in anything other then a number I want it to spit out an error that say's "invalid input"
I've tried doing it with an else statemnt but can't get it it to work. Here is my code.
Code:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
//declare variables
int Temp1 = 0;
int Temp2 = 0;
int AverageTemp = 0;
cout << "Follow the steps to find the average temp between two cities." <<endl;
cout <<endl;
cout << "Enter temp for 1st city: ";
cin >> Temp1;
cout << "Enter temp for 2nd city: ";
cin >> Temp2;
if(Temp1 >= 120)
{
cout << "Error Temp1 is WAY TO HOT! Temp can not exceed or be equal to 120." <<endl;
cout << "Please re-enter temp for 1st city: ";
cin >> Temp1;
}
if(Temp2 >= 120)
{
cout << "Error Temp2 is WAY TO HOT! Temp can not exceed or be equal to 120." <<endl;
cout << "Please re-enter temp for 2nd city: ";
cin >> Temp2;
}
if (Temp1 < 120 || Temp2 < 120)
{
AverageTemp = (Temp1 + Temp2) / 2;
cout << "Average temp: " << AverageTemp <<endl;
}
if (Temp1 > Temp2)
cout <<"City 1 is warmer then city 2." <<endl;
if (Temp1 < Temp2)
cout <<"City 2 is warmer then City 1." <<endl;
if (Temp1 == Temp2)
cout <<"Both temps are equal." <<endl;
system("pause");
return 0;
}
Thanks in advance.