| | |
Weather Program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 40
Reputation:
Solved Threads: 0
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.
c++ Syntax (Toggle Plain Text)
// 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; };
Always post your errors!
What is this? The syntax is improper and I'm not sure what you're trying to do.
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)
What's this?
What is rainfall? Where is it declared?
These are just a few.
C++ Syntax (Toggle Plain Text)
struct Weather
C++ Syntax (Toggle Plain Text)
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;
C++ Syntax (Toggle Plain Text)
RainFall;
C++ Syntax (Toggle Plain Text)
cin.getline(rainfall.Inches, RAINFALL_LENGTH);
These are just a few.
Last edited by CoolGamer48; Aug 5th, 2008 at 12:12 am.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
•
•
Join Date: Oct 2007
Posts: 40
Reputation:
Solved Threads: 0
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.
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:
Move the definition of the
Change
i.e:
change this:
to
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
Get rid of this line:
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
string month[11];
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
Last edited by CoolGamer48; Aug 5th, 2008 at 12:32 am.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
•
•
Join Date: Oct 2007
Posts: 40
Reputation:
Solved Threads: 0
ok I got this far with the code and still end of with 16 errors.
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 ==========
c++ Syntax (Toggle Plain Text)
#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; } }
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 ==========
In passing look at the recent thread of your colleague - young meteorologist:
http://www.daniweb.com/forums/thread137898.html
May be it helps
...
http://www.daniweb.com/forums/thread137898.html
May be it helps
... 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:
That is incorrect. It should be:
Also, in the definition of the getdata() function: you have this:
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[].
Few things I'll help you with:
C++ Syntax (Toggle Plain Text)
annualWeather.totalRainfall[i];
C++ Syntax (Toggle Plain Text)
annualWeather[i].Rainfall
Also, in the definition of the getdata() function: you have this:
C++ Syntax (Toggle Plain Text)
void getdata(WEATHER annualWeather[i]&)
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
•
•
Join Date: Oct 2007
Posts: 40
Reputation:
Solved Threads: 0
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.
c++ Syntax (Toggle Plain Text)
#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; }
![]() |
Similar Threads
- IE won't let site show pictures. (Web Browsers)
- have you ever abused ur powers? (Geeks' Lounge)
- "WINDOWS\ SYSTEM\ BRIDGE.DLL" (Viruses, Spyware and other Nasties)
- I think I have a virus (Windows NT / 2000 / XP)
- c++ windows program to demonstrate encryption (C++)
- pegged CPU, possible peper? (Web Browsers)
- Please Help, Junk on machine (Windows NT / 2000 / XP)
- Know of a good program? (IT Professionals' Lounge)
Other Threads in the C++ Forum
- Previous Thread: Setting array in contructor
- Next Thread: Columns in txt
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






