Nested Loops Help

Oct 2, 2018 at 7:20am
My code is supposed to allow you to input all the inches of rain for the year, then adds them up at the end. when i run the code, i can only input januarys rain amount, then it just goes through the code, not allowing further inputs.
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
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    double rain, total=0, average;
    int years, inputRainAmount;
    bool inputFail;
    const string MONTHS_NAMES [] = {"Jan" , "Feb", "Mar", "Apr","May",
    "Jun","Jul","Aug", "Sep", "Oct", "Nov", "Dec"};
    
    do {
    cout << "Please enter the amount of years: ";
    cin >> years;
    inputFail = cin.fail() || years <=0;
    
    if (inputFail==true)
    {
              cout << "Error: number of years must be >0" << endl;
              cin.clear();
              cin.ignore(INT_MAX, '\n');
    }
} while (inputFail);


for (int i = 1; i <=years; i++)
{
         cout << "\nStarting year number " << i << endl;

         for (int j = 0; j <= 11; j++)
         {
             cout << "\nEnter amount of rain for " << MONTHS_NAMES[j]<< ":" ;
             //input validation
             cin >> inputRainAmount;
             
             total += rain; 
             }
             }
    system("PAUSE");
    return EXIT_SUCCESS;
}
Last edited on Oct 2, 2018 at 7:22am
Oct 2, 2018 at 7:49am
So you input a variable called inputRainAmount:
cin >> inputRainAmount;

Then you increment the total by a variable called rain:
total += rain;

Spot anything odd?
Oct 2, 2018 at 8:08am
okay. got that. now it goes to february then cycles through. (it ran once but now is doing the same ordeal after january.)
Last edited on Oct 2, 2018 at 8:09am
Oct 2, 2018 at 8:10am
holy crap i got it. i had the inputRainAmount as an int and was typing in decimals. thank you very much.
Topic archived. No new replies allowed.