I have a program due shortly and I can't get this to work. I have many errors that show up and can't figure them out. Can I get some help to get me on the correct path.

// This program shows a structure with two nested structure members.
#include <iostream>			// standard IO routines 
#include <iomanip>			// for output formatting
#include <fstream>			// standard file stream
#include <cmath>			// standard math functions 
#include <string>			// standard C++ library 
using namespace std;			// standard C++ namespace
 
struct Weather
 
const int RAINFALL_LENGTH = 2;
const int HIGH TEMP_LENGTH = 3;
const int LOW TEMP_LENGTH = 3;
string month[11];
month[0] = January;
month[1] = February;
month[2] = March;
month[3] = April;
month[4] = May;
month[5] = June;
month[6] = July;
month[7] = August;
month[8] = September;
month[9] = October;
month[10] = November;
month[11] = December;
 
int main()
{
RainFall;
 
// Ask for the user for the amount of rainfall
cout << "Enter the amount of rainfall: ";
cin.getline(rainfall.Inches, RAINFALL_LENGTH);
 
// Get the high and low temperatures
cout << "Now enter the high temperature:\n";
cout << "Temperature (up to 3 digits): ";
cin >> High.Temperature.month;
cout << "Enter the low temperature: \n";
cin >> Low.Temperature.month;
cin.get(); // Remove the remaining newline character// This <strong class="highlight">program</strong> shows a structure with two nested structure members.

return(0);
}
struct month
{
   int rainfall;
   int high_temp;
   int low_temp;
};

Recommended Answers

All 7 Replies

Always post your errors!

struct Weather

What is this? The syntax is improper and I'm not sure what you're trying to do.

month[0] = January;
month[1] = February;
month[2] = March;
month[3] = April;
month[4] = May;
month[5] = June;
month[6] = July;
month[7] = August;
month[8] = September;
month[9] = October;
month[10] = November;
month[11] = December;

This is executable code and cannot exist outside of a function. Put it at the beginning of main (or in some other function called at the beginning of main)

RainFall;

What's this?

cin.getline(rainfall.Inches, RAINFALL_LENGTH);

What is rainfall? Where is it declared?

These are just a few.

I am trying to write a program that uses a structure to store the following weather data for a particular month:

Total Rainfall
High Temperature
Low Temperature
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. (The average temperature should be calculated.) Once the data is entered for all the months, the program should caluclate the 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.

Inpuit Validation: ONly accept temperatures within the range between -100 and +140 degrees Fahrenheit.

The above post could have been a place to post your errors - but no matter.

Get rid of this line:

struct Weather;

Move the definition of the month struct to where the struct Weather line was.

Change month from an array of strings to an array of months. you might also want to name it months, since the name month is taken by a struct, and the plural is more correct. Also, you only have enough space for 11 elements in the array, meaning the maximum index is 11-1 = 10. you want enough room for 12 months.
i.e:
change this:

string month[11];

to

month months[12];

Remove the 12 lines after that. (the month[0] = January lines)

That's all the precise code I'm giving up. Try to fix the rest on your own. Then post updated code with more specific questions.

Hint: you will need a loop in your program

ok I got this far with the code and still end of with 16 errors.

#include <iostream>					// standard IO routines 
#include <iomanip>					// for output formatting
#include <fstream>					// standard file stream
#include <cmath>					// standard math functions 
#include <string>					// standard C++ library 
using namespace std;				// standard C++ namespace

//define the structure
struct WEATHER
{
double totalRainfall;
double highTemp;
double lowTemp;
double avgTemp;
};

// Function Prototypes
void getdata(WEATHER&);    // Argument passed by reference
//month months[12];

int main()
{

//declare array of structures
WEATHER annualWeather[12];

getdata(annualWeather[]);   //call the function pass the argument to the function

double AnnualRainfall;  // declare variable

for(int x = 0; x < 11; x++)
AnnualRainfall += annualWeather.totalRainfall[i];

cout << " Total Rainfall is"  AnnualRainfall << endl;
return 0;
}

void getdata(WEATHER annualWeather[i]&)   //annual weather is the reference parameter  
{  
	int i;

	double MaxHighTemp = 0;
	double MaxLowTemp = 10000;

	for(i=0;i<12;i++)
	{
		cout << "enter low tempature for month " i + 1 << ":" << endl;	
		cin >> annualWeather.lowTemp[i];
		while(annualWeather.lowTemp[i] < -100)
		{
			cout << " must be gretaer than -100 " << endl;
			cin >> annualWeather.lowTemp[i];
		}
		if (annualWeather.highTemp[i] < lowHighTemp)  		
			MaxLowTemp = annualWeather.lowTemp[i];

		cout << "enter high tempature for month " i + 1 << ":" << endl;
		cin >> annualWeather.highTemp[i];
		while(annualWeather.highTemp[i] < -100)
		{
			cout << " must be less than 140 " << endl;
			cin >> annualWeather.highTemp[i];
		}
		if (annualWeather.highTemp[i] > MaxHighTemp)  		
			MaxHighTemp = annualWeather.highTemp[i];

		cout << "enter total rainfall for month " i + 1 << ":" << endl;
		cin >> annualWeather.totalRainfall[i];

		annualWeather[i].avgTemp = (annualWeather[i].lowTemp + annualWeather[i].highTemp) / 2;

	}
}

below is the error log that i received.

1>------ Build started: Project: lab9, Configuration: Debug Win32 ------
1>Compiling...
1>lab9.cpp
1>c:\users\arron\desktop\lab9\lab9\lab9.cpp(36) : error C2059: syntax error : ']'
1>c:\users\arron\desktop\lab9\lab9\lab9.cpp(41) : error C2228: left of '.totalRainfall' must have class/struct/union
1> type is 'WEATHER [12]'
1>c:\users\arron\desktop\lab9\lab9\lab9.cpp(41) : error C2065: 'i' : undeclared identifier
1>c:\users\arron\desktop\lab9\lab9\lab9.cpp(43) : error C2146: syntax error : missing ';' before identifier 'AnnualRainfall'
1>c:\users\arron\desktop\lab9\lab9\lab9.cpp(43) : error C2563: mismatch in formal parameter list
1>c:\users\arron\desktop\lab9\lab9\lab9.cpp(43) : error C2568: '<<' : unable to resolve function overload
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(974): could be 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=wchar_t,
1> _Traits=std::char_traits<wchar_t>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(966): or 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(940): or 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
1>c:\users\arron\desktop\lab9\lab9\lab9.cpp(47) : error C2065: 'i' : undeclared identifier
1>c:\users\arron\desktop\lab9\lab9\lab9.cpp(47) : error C2143: syntax error : missing ',' before '&'
1>c:\users\arron\desktop\lab9\lab9\lab9.cpp(56) : error C2146: syntax error : missing ';' before identifier 'i'
1>c:\users\arron\desktop\lab9\lab9\lab9.cpp(56) : warning C4554: '<<' : check operator precedence for possible error; use parentheses to clarify precedence
1>c:\users\arron\desktop\lab9\lab9\lab9.cpp(56) : error C2297: '<<' : illegal, right operand has type 'const char [2]'
1>c:\users\arron\desktop\lab9\lab9\lab9.cpp(56) : error C2563: mismatch in formal parameter list
1>c:\users\arron\desktop\lab9\lab9\lab9.cpp(56) : error C2568: '<<' : unable to resolve function overload
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(974): could be 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=wchar_t,
1> _Traits=std::char_traits<wchar_t>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(966): or 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(940): or 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
1>c:\users\arron\desktop\lab9\lab9\lab9.cpp(57) : error C2228: left of '.lowTemp' must have class/struct/union
1> type is 'WEATHER []'
1> did you intend to use '->' instead?
1>c:\users\arron\desktop\lab9\lab9\lab9.cpp(58) : error C2228: left of '.lowTemp' must have class/struct/union
1> type is 'WEATHER []'
1> did you intend to use '->' instead?
1>c:\users\arron\desktop\lab9\lab9\lab9.cpp(58) : fatal error C1903: unable to recover from previous error(s); stopping compilation
1>Build log was saved at "file://c:\Users\Arron\Desktop\lab9\lab9\Debug\BuildLog.htm"
1>lab9 - 15 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

You've got the basic idea right I think - just some minor typos and mistakes. I don't want to point out all of them - try to figure them out - read over the program (some lines in particular you may want to look over are the lines that are mentioned in the errors).

Few things I'll help you with:

annualWeather.totalRainfall[i];

That is incorrect. It should be:

annualWeather[i].Rainfall

Also, in the definition of the getdata() function: you have this:

void getdata(WEATHER annualWeather[i]&)

First of all, what is i? It hasn't been declared in this scope. Get rid of it. And the & should be between WEATHER and annualWeather[].

had to step back in the program to see if I could simplify the program and this is what it looks like. I need help getting the program to calculate the total rainfall.

#include <iostream>					// standard IO routines 
#include <iomanip>					// for output formatting
#include <fstream>					// standard file stream
#include <cmath>					// standard math functions 
#include <string>					// standard C++ library 
using namespace std;				// standard C++ namespace

//struct months
const int months = 12;
double totalRainfall;
double Rainfall[months];
double highTemperature;
double lowTemperature;
double avgTemp;
double avgRainfall;
double lowest;
double highest;

double sumArray(double[],int);
void GetRain (double Rain);
void GetTempH (double Temp);
void GetTempl (double Temp2);
int main()
{
	cout << " Welcome to the Year's Weather Data Analyzer " << endl;
	cout << " ====================================================== " << endl;
	cout << endl << " Enter the following information: " << endl << endl;
	for (int count = 0; count < months; count++)
	{
	
	cout << " Month " << (count + 1) << endl;
	GetRain(Rainfall[months]);
	GetTempH(highTemperature); 
	GetTempl(lowTemperature);
	
	}
	cout << " ==================================================== " << endl;
	cout << " Final Results " <<endl <<endl;
	// Display the result
	cout << fixed << showpoint << setprecision(2);
	totalRainfall = sumArray(Rainfall,months);
	cout << " The total Rainfall: " << totalRainfall << endl;
	cout << " Average monthly Rain: " << avgRainfall << endl;
	cout << " Average monthly Average Temperature: " << avgTemp << endl;
	cout << " Highest Temperature: " << highest << " months " 
	<< months << endl;
	cout << " Lowest Temperature: " << lowest << " months " << months << endl;
	cout << " ==================================================== " << endl;

	return 0;
}
 //Function to get high temperatures
void GetTempH (double Temp)
{
	cout << setw(10) << " High Temperature ";
	cin >> Temp;
	while (cin && (Temp < -101 || Temp > 140))
	{
		cout << setw(10) << " Temperature must be  -100 through 140. " << endl;
		cout << setw(10) << " Enter the High Temperature for this month: ";
		cin >> Temp;
	}
}
//Function to get low temperatures
void GetTempl (double Temp2)
{
	cout << setw(10) << " Low Temperature: ";
	cin >> Temp2;
	while (cin && (Temp2 < -101 || Temp2 > 140))
	{
		cout << setw(10) << " Temperature must be  -100 through 140. " << endl;
		cout << setw(10) << " Enter the Low Temperature for this month: ";
		cin >> Temp2;
	}
	
}
void GetRain (double Rain)
{
	cout << setw(5) << " Rainfall: ";
	cin >> Rain;
	while (cin && (Rain <= 0))
	{
		cout << setw(10) << " Rain fall must be  greater than or equal to 0. " << endl;
		cout << setw(10) << " Enter the Rain Fall for this month: ";
		cin >> Rain;
		
		
	}
}

double sumArray(double array[], int size)
{
	double totalRainfall = 0; // Accumulator
	for (int count = 0; count < size; count++)
		totalRainfall += array[count];
	return totalRainfall;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.