Help my code stops at the end!

total rainfall
Average Monthly Rain: The average monthly rain is total rain of 12 months then divided by 12.
Average Monthly Average Temperature:
The monthly average Temp is (monthly high temp + monthly low temp)/2.

The average monthly average temp is to add all 12 months' monthly average Temp and then divided by 12.

Highest Temp and Lowest Temp during the year and the months they occurred in.

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

struct weather
{
double totalRainfall;
double highTemp;
double lowTemp;
double avgTemp;
};

int main()
{
weather months[12];
double total = 0, highest, lowest, avgsum;
int highmonth, lowmonth;
string month[12] = { "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November",
"December" };

for (int i = 0; i<12; i++)
{
cout << "Enter total rainfall for month " << month[i] << ": ";
cin >> months[i].totalRainfall;

cout << "Enter high temperature: ";
cin >> months[i].highTemp;

while (months[i].highTemp < -100 || months[i].highTemp > 140)
{
cout << "ERROR: Temperature must be in the range of "
<< "-100 through 140.\n";
cout << "\tHigh Temperature: ";
cin >> months[i].highTemp;
}
cout << "Enter low temperature: ";
cin >> months[i].lowTemp;

while (months[i].lowTemp < -100 || months[i].lowTemp > 140)
{
cout << "ERROR: Temperature must be in the range of "
<< "-100 through 40.\n";
cout << "\tLow Temperature: ";
cin >> months[i].lowTemp;
}
}
//Calculate the monthly average temperature and total rainfall.

for (int i = 0; i<12; i++)
{
total = total + months[i].totalRainfall;
months[i].avgTemp= (months[i].highTemp + months[i].lowTemp)/2;
}

highest = months[0].highTemp;
lowest = months[0].lowTemp;

for (int i=1; i<12; i++)
{
if (months[i].highTemp>highest)
{
highest = months[i].highTemp;
highmonth = i;
}
if (months[i].lowTemp<lowest)
{
lowest = months[i].lowTemp;
lowmonth = i;
}
}
avgsum = 0;

//Calculate the average.
for (int i = 0; i<12; i++)
{
avgsum = avgsum + months[i].avgTemp;
}

//Display the calculated data.
cout << "Average monthly rainfall: " << total / 12 << endl;
cout << "Total monthly rainfall: " << total << endl;
cout << "Highest rainfall in " << month[highmonth] << "is: " << highest << endl;
cout << "Lowest rainfall in " << month[lowmonth] << "is: " << lowest << endl;
cout << "Average of average temperatures is: " <<avgsum /12 << endl;

system("pause");
}
system("pause"); Will do that
What I mean is it won't tell me what the highest and lowest months are it gives me a break error!
There’s a typo in your third for-loop:
for (int i=1; i<12; i++)
The index should range from 0 to 11 as in the others.

I haven’t tested your program because I’m too lazy to insert 36 numeric values, but perhaps you need to add
#include <cstdlib>
to use “system()”.
Anyway it compiles without issues.

When you want to test a program that requires so many data, you could consider moving the loop which asks the user for the data inside a function, then adding a new one which reads the data from a file.
When you are satisfied with your code, you just need to substitute the call for the function which asks the user for the one which reads from file.

Just to give you an idea or what I mean, imagine to write a file like the following:
rain_temp.txt
1
2
3
4
5
6
7
8
9
10
11
12
 1.0 21.0 11.0
 2.0 22.0 12.0
 3.0 23.0 13.0
 4.0 24.0 14.0
 5.0 25.0 15.0
 6.0 26.0 16.0
 7.0 27.0 17.0
 8.0 28.0 18.0
 9.0 29.0 19.0
10.0 30.0 20.0
11.0 31.0 21.0
12.0 32.0 22.0


Than you can use it to test your code as many times as you want:
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>

struct weather
{
    double totalRainfall;
    double highTemp;
    double lowTemp;
    double avgTemp;
};

void waitForEnter();

int main()
{
    // Uncomment the following lines to get data from the user
    // weather months[12];
    // std::string month[] = { "January", "February", "March", "April", 
                            // "May", "June", "July", "August", 
                            // "September", "October", "November", "December" };
    // for (int i = 0; i<12; i++)
    // {
        // std::cout << "Enter total rainfall for month " << month[i] << ": ";
        // std::cin >> months[i].totalRainfall;

        // do {
            // std::cout << "Enter highest temperature: ";
            // std::cin >> months[i].highTemp;
            // cin.ignore(1);
            // if(months[i].highTemp < -100 || 140 < months[i].highTemp) {
                // std::cout << "ERROR: temperature must be in the range of "
                             // "-100 through 140.\n";
            // }
        // } while (months[i].highTemp < -100 || 140 < months[i].highTemp)

        // do {
            // std::cout << "Enter low temperature: ";
            // std::cin >> months[i].lowTemp;
            // cin.ignore(1);
            // if(months[i].lowTemp < -100 || 140 < months[i].lowTemp) {
                // std::cout << "ERROR: temperature must be in the range of "
                            // "-100 through 40.\n";
            // }
        // } while (months[i].lowTemp < -100 || 140 < months[i].lowTemp)
    // }

    // Comment the following block to avoid reading data from test file
    // START READING FROM FILE
    std::ifstream test_data("rain_temp.txt");
    weather months[12];
    std::string month[] = { "January", "February", "March", "April", 
                            "May", "June", "July", "August", 
                            "September", "October", "November", "December" };
    for(int i{}; i<12; i++) {
        test_data >> months[i].totalRainfall >> months[i].highTemp 
                  >> months[i].lowTemp;
    }
    test_data.close();
    // END READING FROM FILE

    //Calculate the monthly average temperature and total rainfall.
    double total = 0;
    for (int i = 0; i<12; i++)
    {
        total += months[i].totalRainfall;
        months[i].avgTemp = (months[i].highTemp + months[i].lowTemp)/2;
    }

    // Find the highest and lowest temperatures ever.
    // Bookmark that month.
    double highest = 0, 
           lowest = 141,
           avgsum = 0;
    int highmonth = 0, lowmonth = 0;
    for (int i=0; i<12; i++)
    {
        if (months[i].highTemp > highest)
        {
            highest = months[i].highTemp;
            highmonth = i;
        }
        if (months[i].lowTemp < lowest)
        {
            lowest = months[i].lowTemp;
            lowmonth = i;
        }
        // Since later we want the average year temperature, we get ahead of
        // that calculation.
        avgsum += months[i].avgTemp;
    }

    // Calculate the average.   <-- of what? Bad comment.
    avgsum /= 12;

    //Display the calculated data.
    std::cout << "Total rainfall: " << total
              << "\nAverage monthly rainfall: " << total / 12
              << "\nHighest rainfall in " << month[highmonth] << " has been: " << highest
              << "\nLowest rainfall in " << month[lowmonth] << " has been: " << lowest
              << "\nAverage of average temperatures is: " << avgsum << '\n';
    waitForEnter();
    return 0;
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

Last edited on
Enoizat wrote:

There’s a typo in your third for-loop:
for (int i=1; i<12; i++)
The index should range from 0 to 11 as in the others.

I don't see what's wrong with that, he already calculated the [0] index as the preliminary values for the min/max calculations.

Note that your print statement says "rainfall" but you're actually displaying the highest and lowest temperatures.

Highest rainfall in Septemberis: 101.1 (just my output example)
Lowest rainfall in Augustis: 20.3


________

EDIT: Found a possible problem: what happens if the highest or lowest temperatures occur in January? You never initialize highmonth or lowmonth. Initialize them to 0! (Enoizat did this in his code that he made for you)

________

EDIT 2:
 
    double highest = 0, 

should be
 
    double highest = -100, 

since it's possible for all of OP's temperature values to theoretically be negative (down to -100).
Last edited on
Thank you guys for all your help I finally got it to working correctly. Now I need to remove this part << "\nAverage of average temperatures is: " << avgsum << '\n'; When I do I get a couple of errors!
Last edited on
The issues is when I get to Highest rainfall and Lowest it stops and I get an error.

I think the question has already been answered. But in case you missed it, int highmonth, lowmonth; are not initialised.

to keep things cosistent, set both to zero.
1
2
3
4
    highest = months[0].highTemp;  // using index 0 here
    lowest  = months[0].lowTemp;   // and here

    lowmonth = highmonth = 0;   // so set each of these to zero too. 
Ganado wrote:
I don't see what's wrong with that
Actually nothing :-(
I was so focused on suggesting a possible strategy for testing the code that I just skimmed through it, so I’ve been taken in by an apparent typo (which wasn’t a typo). Thank you for your help.
Thanks everyone for you help: Can someone please run my code? I want to add space before the output on //Display the calculated data! I also need help in putting space between the highest and lowest temp at the bottom so the month and the temp isn't so close together!

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

struct weather
{
double totalRainfall;
double highTemp;
double lowTemp;
double avgTemp;
};

int main()
{

weather months[12];
std::string month[] = { "January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December" };
for (int i = 0; i < 12; i++)
{
std::cout << "Enter total rainfall for month " << month[i] << ": ";
std::cin >> months[i].totalRainfall;

do {
std::cout << "Highest Temperature: ";
std::cin >> months[i].highTemp;
cin.ignore(1);
if (months[i].highTemp < -100 || 140 < months[i].highTemp) {
std::cout << "ERROR: temperature must be in the range of "
"-100 through 140.\n";
}
} while (months[i].highTemp < -100 || 140 < months[i].highTemp);

do {
std::cout << "Low Temperature: ";
std::cin >> months[i].lowTemp;
cin.ignore(1);
if (months[i].lowTemp < -100 || 140 < months[i].lowTemp) {
std::cout << "ERROR: temperature must be in the range of "
"-100 through 40.\n";
}
} while (months[i].lowTemp < -100 || 140 < months[i].lowTemp);
}
//Calculate the monthly average temperature and total rainfall.
double total = 0;
for (int i = 0; i < 12; i++)
{
total += months[i].totalRainfall;
months[i].avgTemp = (months[i].highTemp + months[i].lowTemp) / 2;
}

// Find the highest and lowest temperatures ever.
// Bookmark that month.
double highest = 0,
lowest = 141,
avgsum = 0;
int highmonth = 0, lowmonth = 0;
for (int i = 0; i < 12; i++)
{
if (months[i].highTemp > highest)
{
highest = months[i].highTemp;
highmonth = i;
}
if (months[i].lowTemp < lowest)
{
lowest = months[i].lowTemp;
lowmonth = i;
}
// Since later we want the average year temperature, we get ahead of
// that calculation.
avgsum += months[i].avgTemp;
}


//Display the calculated data.

cout << "Total Rainfall: " << total <<endl;
cout << "Average Monthly Rain: " << total / 12 <<endl;
cout << "Average Monthly Average Temperatures : " << avgsum / 12 <<endl;
cout << "Highest Temperature " << month[highmonth] << highest <<endl;
cout << "Lowest Temperature" << month[lowmonth] << lowest << endl;

system("pause");
}
Last edited on
Let me know if it's what you were looking for:
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iomanip>
#include <iostream>
#include <string>

struct weather
{
    double totalRainfall;
    double highTemp;
    double lowTemp;
    double avgTemp;
};

int main()
{
    weather months[12] = {}; // initialize all fields to 'empty' (let's say 0)
    std::string month[] = { "January",   "February", "March",    "April",
                            "May",       "June",     "July",     "August",
                            "September", "October",  "November", "December" };
    for (int i = 0; i < 12; i++)
    {
        std::cout << "\nEnter total rainfall for month " << month[i] << ": ";
        std::cin >> months[i].totalRainfall;

        do {
            std::cout << "Highest temperature: ";
            std::cin >> months[i].highTemp;
            if (months[i].highTemp < -100 || 140 < months[i].highTemp) {
                std::cout << "ERROR: temperature must be in the range of "
                "-100 through 140.\n";
            }
        } while (months[i].highTemp < -100 || 140 < months[i].highTemp);

        do {
            std::cout << "Lowest temperature: ";
            std::cin >> months[i].lowTemp;
            if (months[i].lowTemp < -100 || 140 < months[i].lowTemp) {
                std::cout << "ERROR: temperature must be in the range of "
                "-100 through 40.\n";
            }
        } while (months[i].lowTemp < -100 || 140 < months[i].lowTemp);
    }
    //Calculate the monthly average temperature and total rainfall.
    double total = 0;
    for (int i = 0; i < 12; i++)
    {
        total += months[i].totalRainfall;
        months[i].avgTemp = (months[i].highTemp + months[i].lowTemp) / 2;
    }

    // Find the highest and lowest temperatures ever.
    // Bookmark that month.
    double highest = -101,
           lowest  =  141,
           avgsum  =    0;
    int highmonth = 0, lowmonth = 0;
    for (int i = 0; i < 12; i++)
    {
        if (months[i].highTemp > highest)
        {
            highest = months[i].highTemp;
            highmonth = i;
        }
        if (months[i].lowTemp < lowest)
        {
            lowest = months[i].lowTemp;
            lowmonth = i;
        }
        // Since later we want the average year temperature, we get ahead of
        // that calculation.
        avgsum += months[i].avgTemp;
    }

    // Can someone please run my code? I want to add space before the output on 
    // Display the calculated data! I also need help in putting space between 
    // the highest and lowest temp at the bottom so the month and the temp 
    // isn't so close together!

    //Display the calculated data.
    std::cout << "\nTotal Rainfall: " << total
              << "\nAverage Monthly Rain: " << total / 12
              << "\nAverage Monthly Temperatures: " << avgsum / 12
              << "\n\nHighest Temperature: " << month[highmonth] << ' ' << highest
              << "\n\nLowest Temperature: " << month[lowmonth]  << ' ' << lowest 
              << "\n\n";

    system("pause");
}

closed account (48T7M4Gy)
http://www.chegg.com/homework-help/questions-and-answers/need-help-write-program-uses-structure-store-following-weather-data-particular-month-total-q20128495
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/general/220758/
Topic archived. No new replies allowed.