anyone know why this isnt working?
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double totalRain = 0.0;// total rainfall inches
double count = 0.0;
int numData;
char answer;
cout <<"For how many years have you collected rainfall data? ";
cin >> numData;
if (numData < 1)
{
cout <<"Invalid: number of years must be at least one." << endl;
cout <<"Do you want to try again (y/n)?";
cin >> answer;
{
if (answer == 'Y' || answer == 'y')
cout <<"For how many years have you collected rainfall data? ";
cin >> numData;
if (answer != 'Y' || answer != 'y')
cout <<"Goodbye";
}
}
for(int month = 0; month < numData*12; month++)
{
float inches;
cout << "Enter rainfall (in inches) for month " << (month%12) + 1 << " of year "
<< (month/12) + 1 << ": ";
cin >> inches;
count++;
totalRain += inches;
}
cout <<"The total number of months of rainfall data:" << numData * 12 << endl;
cout <<"The total inches of rainfall for that period:" << totalRain << endl;
cout <<"The average rainfall per month for that period:" << totalRain / (float)count << endl;
system("pause");
return 0;
}
the part where you enter
would you like to try again y/n)?
it works fine if i put y or Y but if i dont want to continue it wont terminate the program.
ex
For how many years have you collected rainfall data? -4
Invalid: number of years must be at least one.
Do you want to try again (y/n)? y
For how many years have you collected rainfall data? 0
Invalid: number of years must be at least one.
Do you want to try again (y/n)? y
For how many years have you collected rainfall data? 2
//Program continues as in Part 1