having some trouble with this one


Hello guys I an having some issues with this program. It will compile but will but keeps giving me errors. (invalid temparture value) I will appreciate if somebody can give it a look over. I put the lab instructions to give you an idea of what the objective is.

**************************************************************************
Structured Data and Abstract Data Types Lab

Your Mission: Write a program that uses a structure to store the following data for each month:

Member Name: Description:
* * * * * * * * * * * * * * *
rain total rainfall
high high temperature
low low temperature
averageTem average temperature

The program should have an array of 12 structures to hold weather data for an entire year. When the program runs, it should ask the user to enter data for each month. Once the data are entered for all the months, the program should calculate and display the average monthly rainfall, the total rainfall for the year, the highest and lowest temperatures for the year (and the months they occurred in), and the average of all the monthly average temperatures.



Input validation should include checking for temperatures within the range between -100 and +140 degrees fahrenheit.

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
 
#include<iostream>
#include<iomanip>
#include<cmath>

using namespace std;

// Constant for the number of months
const int num_months = 12;

// Declaration of the WeatherInfo structure
struct weatherInfo
{


double rain[num_months];
double high[num_months];
double low[num_months];
double averageTemp[num_months];

double yearlyMonthlyRainfall(double weather[])

{
	int count{0};
	double sum{0};
	while(count < 12)
	{
	 sum += weather[count];
	 count++;
	}
	
	double average = sum / 12;
	return average;
}

};

int main(){
	
	double temp;
	double low1;
	double average2;
	weatherInfo Info;
	
// Create an array of WeatherInfo structures
// Get the weather data for each month.
cout << "Enter the following information:\n";
for (size_t count = 0; count <= 11; count++)


{
		cout << " Enter total rainfall for month " << count + 1 << ":" << endl;
		cin >> Info.rain[count];
		cout << " enter the highest temperature reading for month " << count + 1 << ":" << endl;
		cin >> temp;
		if (temp >= -100 && temp <= 140)
		


{
		Info.high[count] = temp;
}

	else
	{
		Info.low[count] = temp;
	}
		cout << "Invalid temperature value" << endl;
		exit (0);
	{
		cout << "enter the lowest temperature reading for month " << count + 1 << ":" << endl;
		cin >> low1;
	}	
		if (low1 >= -100 && low1<= 140)
	{
		Info.low[count] = low1;
	}
		else
	{
		cout << "Invalid temperature value" << endl;
		exit(0);
	}	
		cout << "enter the average temperature for month " << count + 1 << ":" << endl;
		cin >> average2;
	
	
		if (average2 >= -100 && average2<= 140)
	{
		Info.averageTemp[count]	= average2;
	}
	else
	{
		cout << "Invalid temperature value" << endl;
		exit(0);
	
		
}

	

	return 0;
}

}


Last edited on
a trick to reading code is to mentally block unimportant bits when scanning for something.

here, if you read it that way..
1
2
3
4
5
6
7
8
9
10
11
12
for something
{
if something
  {
   something
  }
else 
{
  something
}
exit 0

it really helps if you keep a consistent brace style. Either misaligned books style (done to avoid wasteful blank lines in printed books) (main(){ ) or column-aligned (
1
2
3
4
main()
{
  stuff
}

but mixing those is erratic and I can only assume the total out the window brackets on 60 are a copy/paste error or something like that.
Last edited on
Having a consistent indentation style goes a long way to help reading comprehension as well. The code as shown in the first post is hard to decipher and understand.

Indentation is to help us hoo-mans read and understand the code, the compiler will happily process code that is unreadable for people as long as it is syntactically correct.

If I were grading the code I'd take a point or two off for mangling the indentation.
Indentation may be helpful, but blindly relying on indentation by itself can lead to hard-to-figure-out bugs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
   std::cout << "Enter a number less than 10 or greater than 100: ";
   int x { };
   std::cin >> x;
   std::cout << '\n';

   if (x >= 10)
      if (x > 100)
         std::cout << "More than 100, Thanks!\n";
   else // not the else intended!
      std::cout << "Less than 10, Thanks!\n";
}

Proper use of braces makes the intent of the program flow much clearer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int main()
{
   std::cout << "Enter a number less than 10 or greater than 100: ";
   int x { };
   std::cin >> x;
   std::cout << '\n';

   if (x >= 10)
   {
      if (x > 100)
      {
         std::cout << "More than 100, Thanks!\n";
      }
   }
   else // fixed!
   {
      std::cout << "Less than 10, Thanks!\n";
   }
}
With some reformatting, removing excess whitespace (blank lines) and proper indentation:
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
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

// Constant for the number of months
const int num_months = 12;

// Declaration of the WeatherInfo structure
struct weatherInfo
{
   double rain[num_months];
   double high[num_months];
   double low[num_months];
   double averageTemp[num_months];

   double yearlyMonthlyRainfall(double weather[])
   {
      int count { 0 };
      double sum { 0 };

      while (count < 12)
      {
         sum += weather[count];
         count++;
      }

      double average = sum / 12;
      return average;
   }
};

int main()
{
   double temp;
   double low1;
   double average2;
   weatherInfo Info;

   // Create an array of WeatherInfo structures
   // Get the weather data for each month.
   cout << "Enter the following information:\n";

   for (size_t count { }; count <= 11; count++)
   {
      cout << " Enter total rainfall for month " << count + 1 << ":" << endl;
      cin >> Info.rain[count];
      cout << " enter the highest temperature reading for month " << count + 1 << ":" << endl;
      cin >> temp;
      if (temp >= -100 && temp <= 140)
      {
         Info.high[count] = temp;
      }
      else
      {
         Info.low[count] = temp;
      }
      cout << "Invalid temperature value" << endl;
      exit(0);
      {
         cout << "enter the lowest temperature reading for month " << count + 1 << ":" << endl;
         cin >> low1;
      }
      if (low1 >= -100 && low1 <= 140)
      {
         Info.low[count] = low1;
      }
      else
      {
         cout << "Invalid temperature value" << endl;
         exit(0);
      }
      cout << "enter the average temperature for month " << count + 1 << ":" << endl;
      cin >> average2;

      if (average2 >= -100 && average2 <= 140)
      {
         Info.averageTemp[count] = average2;
      }
      else
      {
         cout << "Invalid temperature value" << endl;
         exit(0);
      }
      return 0;
   }
}

Lines 59-60 will always be executed within the loop, doesn't matter if the data being entered is valid or not. The program will terminate after the first value, the highest value, is entered.
Something else to consider, calling exit() can cause problems with cleanup vs. using return:
https://stackoverflow.com/questions/461449/return-statement-vs-exit-in-main

Consider using return -1; (or some other non-zero value) to indicate the program didn't terminate normally, but will still allow for proper cleanup of used resources.
Taking off just a couple of marks for presentation is very generous :(

A simple struct with a pile of functions outside to record/input/process an array of objects process results in the following:

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
#include<iostream>

using namespace std;

const int MONTHS_PER_YEAR = 12;

struct weatherInfo
{
    double rain{0};
    double high{0};
    double low{0};
    double average{0};
};

double averageRainfall(weatherInfo W[])
{
    int count{0};
    double sum{0};
    while(count < MONTHS_PER_YEAR)
    {
        sum += W[count].rain;
        count++;
    }
    return sum / MONTHS_PER_YEAR;
}

int main()
{
    weatherInfo Info[MONTHS_PER_YEAR]{0};
    
    
    
    cout << "Enter the following information:\n";
    for (size_t count = 0; count < MONTHS_PER_YEAR; count++)
    {
        cout << " Enter total rainfall for month " << count + 1 << ": ";
        cin >> Info[count].rain;
        
        
        cout
        << " Enter the highest temperature reading for month "
        << count + 1 << ": ";
        cin >> Info[count].high;
        
        if (Info[count].high >= -100 and Info[count].high <= 140)
        {
            cout << "Valid temperature value\n";
        }
        else
        {
            cout << "Invalid temperature value\n";
            exit(0); // allow user to correct and try again?
        }
        
        // blah blah for the other factors - use a function eliminate repetition
    }
    // then ... process the years worth of data
    
    cout << "Average rainfall for year: " << averageRainfall(Info) << '\n';
    // etc etc
    
    return 0;
}
Possibly:

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
#include <iostream>

constexpr size_t MONTHS {12};

struct weatherInfo {
	double rain {};
	double high {};
	double low {};
	double average {};
};

int main() {
	weatherInfo Info[MONTHS];

	std::cout << "Enter the following information for each month:\n";

	double totalRain {}, totalAvg {};
	double highTemp {-110}, lowTemp {150};
	size_t highMon {}, lowMon {};

	for (size_t count {}; count < MONTHS; ++count) {
		std::cout << "Enter total rainfall for month " << count + 1 << ": ";
		std::cin >> Info[count].rain;
		totalRain += Info[count].rain;

		do {
			std::cout << "Enter the highest temperature reading for month " << count + 1 << "(-100 - 140): ";
			std::cin >> Info[count].high;
		} while (!(Info[count].high >= -100 && Info[count].high <= 140) && (std::cout << "Invalid temperature value\n"));

		if (Info[count].high > highTemp) {
			highTemp = Info[count].high;
			highMon = count;
		}

		do {
			std::cout << "Enter the lowest temperature reading for month " << count + 1 << "(-100 - 140): ";
			std::cin >> Info[count].low;
		} while (!(Info[count].low >= -100 && Info[count].low <= 140) && (std::cout << "Invalid temperature value\n"));

		if (Info[count].low < lowTemp) {
			lowTemp = Info[count].low;
			lowMon = count;
		}

		do {
			std::cout << "Enter the average temperature reading for month " << count + 1 << "(-100 - 140): ";
			std::cin >> Info[count].average;
		} while (!(Info[count].average >= -100 && Info[count].average <= 140) && (std::cout << "Invalid temperature value\n"));

		totalAvg += Info[count].average;
	}

	std::cout << "Average monthly rainfall is " << totalRain / MONTHS << '\n';
	std::cout << "Total rainfall is " << totalRain << '\n';
	std::cout << "Highest temperature is " << highTemp << " in month " << highMon + 1 << '\n';
	std::cout << "Lowest temperature is " << lowTemp << " in month " << lowMon + 1 << '\n';
	std::cout << "Overall average temperature is " << totalAvg / MONTHS << '\n';
}

Topic archived. No new replies allowed.