943,582 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2028
  • C++ RSS
Aug 5th, 2008
0

Weather Program

Expand Post »
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)
  1. // This program shows a structure with two nested structure members.
  2. #include <iostream> // standard IO routines
  3. #include <iomanip> // for output formatting
  4. #include <fstream> // standard file stream
  5. #include <cmath> // standard math functions
  6. #include <string> // standard C++ library
  7. using namespace std; // standard C++ namespace
  8.  
  9. struct Weather
  10.  
  11. const int RAINFALL_LENGTH = 2;
  12. const int HIGH TEMP_LENGTH = 3;
  13. const int LOW TEMP_LENGTH = 3;
  14. string month[11];
  15. month[0] = January;
  16. month[1] = February;
  17. month[2] = March;
  18. month[3] = April;
  19. month[4] = May;
  20. month[5] = June;
  21. month[6] = July;
  22. month[7] = August;
  23. month[8] = September;
  24. month[9] = October;
  25. month[10] = November;
  26. month[11] = December;
  27.  
  28. int main()
  29. {
  30. RainFall;
  31.  
  32. // Ask for the user for the amount of rainfall
  33. cout << "Enter the amount of rainfall: ";
  34. cin.getline(rainfall.Inches, RAINFALL_LENGTH);
  35.  
  36. // Get the high and low temperatures
  37. cout << "Now enter the high temperature:\n";
  38. cout << "Temperature (up to 3 digits): ";
  39. cin >> High.Temperature.month;
  40. cout << "Enter the low temperature: \n";
  41. cin >> Low.Temperature.month;
  42. cin.get(); // Remove the remaining newline character// This <strong class="highlight">program</strong> shows a structure with two nested structure members.
  43.  
  44. return(0);
  45. }
  46. struct month
  47. {
  48. int rainfall;
  49. int high_temp;
  50. int low_temp;
  51. };
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
student4lyfe is offline Offline
40 posts
since Oct 2007
Aug 5th, 2008
0

Re: Weather Program

Always post your errors!

C++ Syntax (Toggle Plain Text)
  1. struct Weather
What is this? The syntax is improper and I'm not sure what you're trying to do.

C++ Syntax (Toggle Plain Text)
  1. month[0] = January;
  2. month[1] = February;
  3. month[2] = March;
  4. month[3] = April;
  5. month[4] = May;
  6. month[5] = June;
  7. month[6] = July;
  8. month[7] = August;
  9. month[8] = September;
  10. month[9] = October;
  11. month[10] = November;
  12. 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)

C++ Syntax (Toggle Plain Text)
  1. RainFall;
What's this?

C++ Syntax (Toggle Plain Text)
  1. cin.getline(rainfall.Inches, RAINFALL_LENGTH);
What is rainfall? Where is it declared?

These are just a few.
Last edited by CoolGamer48; Aug 5th, 2008 at 12:12 am.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Aug 5th, 2008
0

Re: Weather Program

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
student4lyfe is offline Offline
40 posts
since Oct 2007
Aug 5th, 2008
0

Re: Weather Program

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

Get rid of this line:
C++ Syntax (Toggle Plain Text)
  1. 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)
  1. 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
Last edited by CoolGamer48; Aug 5th, 2008 at 12:32 am.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Aug 5th, 2008
0

Re: Weather Program

ok I got this far with the code and still end of with 16 errors.
c++ Syntax (Toggle Plain Text)
  1. #include <iostream> // standard IO routines
  2. #include <iomanip> // for output formatting
  3. #include <fstream> // standard file stream
  4. #include <cmath> // standard math functions
  5. #include <string> // standard C++ library
  6. using namespace std; // standard C++ namespace
  7.  
  8. //define the structure
  9. struct WEATHER
  10. {
  11. double totalRainfall;
  12. double highTemp;
  13. double lowTemp;
  14. double avgTemp;
  15. };
  16.  
  17. // Function Prototypes
  18. void getdata(WEATHER&); // Argument passed by reference
  19. //month months[12];
  20.  
  21. int main()
  22. {
  23.  
  24. //declare array of structures
  25. WEATHER annualWeather[12];
  26.  
  27. getdata(annualWeather[]); //call the function pass the argument to the function
  28.  
  29. double AnnualRainfall; // declare variable
  30.  
  31. for(int x = 0; x < 11; x++)
  32. AnnualRainfall += annualWeather.totalRainfall[i];
  33.  
  34. cout << " Total Rainfall is" AnnualRainfall << endl;
  35. return 0;
  36. }
  37.  
  38. void getdata(WEATHER annualWeather[i]&) //annual weather is the reference parameter
  39. {
  40. int i;
  41.  
  42. double MaxHighTemp = 0;
  43. double MaxLowTemp = 10000;
  44.  
  45. for(i=0;i<12;i++)
  46. {
  47. cout << "enter low tempature for month " i + 1 << ":" << endl;
  48. cin >> annualWeather.lowTemp[i];
  49. while(annualWeather.lowTemp[i] < -100)
  50. {
  51. cout << " must be gretaer than -100 " << endl;
  52. cin >> annualWeather.lowTemp[i];
  53. }
  54. if (annualWeather.highTemp[i] < lowHighTemp)
  55. MaxLowTemp = annualWeather.lowTemp[i];
  56.  
  57. cout << "enter high tempature for month " i + 1 << ":" << endl;
  58. cin >> annualWeather.highTemp[i];
  59. while(annualWeather.highTemp[i] < -100)
  60. {
  61. cout << " must be less than 140 " << endl;
  62. cin >> annualWeather.highTemp[i];
  63. }
  64. if (annualWeather.highTemp[i] > MaxHighTemp)
  65. MaxHighTemp = annualWeather.highTemp[i];
  66.  
  67. cout << "enter total rainfall for month " i + 1 << ":" << endl;
  68. cin >> annualWeather.totalRainfall[i];
  69.  
  70. annualWeather[i].avgTemp = (annualWeather[i].lowTemp + annualWeather[i].highTemp) / 2;
  71.  
  72. }
  73. }
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 ==========
Reputation Points: 10
Solved Threads: 0
Light Poster
student4lyfe is offline Offline
40 posts
since Oct 2007
Aug 5th, 2008
0

Re: Weather Program

In passing look at the recent thread of your colleague - young meteorologist:
http://www.daniweb.com/forums/thread137898.html
May be it helps ...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Aug 5th, 2008
0

Re: Weather Program

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:
C++ Syntax (Toggle Plain Text)
  1. annualWeather.totalRainfall[i];
That is incorrect. It should be:
C++ Syntax (Toggle Plain Text)
  1. annualWeather[i].Rainfall

Also, in the definition of the getdata() function: you have this:
C++ Syntax (Toggle Plain Text)
  1. 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[].
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Aug 5th, 2008
0

Re: Weather Program

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)
  1. #include <iostream> // standard IO routines
  2. #include <iomanip> // for output formatting
  3. #include <fstream> // standard file stream
  4. #include <cmath> // standard math functions
  5. #include <string> // standard C++ library
  6. using namespace std; // standard C++ namespace
  7.  
  8. //struct months
  9. const int months = 12;
  10. double totalRainfall;
  11. double Rainfall[months];
  12. double highTemperature;
  13. double lowTemperature;
  14. double avgTemp;
  15. double avgRainfall;
  16. double lowest;
  17. double highest;
  18.  
  19. double sumArray(double[],int);
  20. void GetRain (double Rain);
  21. void GetTempH (double Temp);
  22. void GetTempl (double Temp2);
  23. int main()
  24. {
  25. cout << " Welcome to the Year's Weather Data Analyzer " << endl;
  26. cout << " ====================================================== " << endl;
  27. cout << endl << " Enter the following information: " << endl << endl;
  28. for (int count = 0; count < months; count++)
  29. {
  30.  
  31. cout << " Month " << (count + 1) << endl;
  32. GetRain(Rainfall[months]);
  33. GetTempH(highTemperature);
  34. GetTempl(lowTemperature);
  35.  
  36. }
  37. cout << " ==================================================== " << endl;
  38. cout << " Final Results " <<endl <<endl;
  39. // Display the result
  40. cout << fixed << showpoint << setprecision(2);
  41. totalRainfall = sumArray(Rainfall,months);
  42. cout << " The total Rainfall: " << totalRainfall << endl;
  43. cout << " Average monthly Rain: " << avgRainfall << endl;
  44. cout << " Average monthly Average Temperature: " << avgTemp << endl;
  45. cout << " Highest Temperature: " << highest << " months "
  46. << months << endl;
  47. cout << " Lowest Temperature: " << lowest << " months " << months << endl;
  48. cout << " ==================================================== " << endl;
  49.  
  50. return 0;
  51. }
  52. //Function to get high temperatures
  53. void GetTempH (double Temp)
  54. {
  55. cout << setw(10) << " High Temperature ";
  56. cin >> Temp;
  57. while (cin && (Temp < -101 || Temp > 140))
  58. {
  59. cout << setw(10) << " Temperature must be -100 through 140. " << endl;
  60. cout << setw(10) << " Enter the High Temperature for this month: ";
  61. cin >> Temp;
  62. }
  63. }
  64. //Function to get low temperatures
  65. void GetTempl (double Temp2)
  66. {
  67. cout << setw(10) << " Low Temperature: ";
  68. cin >> Temp2;
  69. while (cin && (Temp2 < -101 || Temp2 > 140))
  70. {
  71. cout << setw(10) << " Temperature must be -100 through 140. " << endl;
  72. cout << setw(10) << " Enter the Low Temperature for this month: ";
  73. cin >> Temp2;
  74. }
  75.  
  76. }
  77. void GetRain (double Rain)
  78. {
  79. cout << setw(5) << " Rainfall: ";
  80. cin >> Rain;
  81. while (cin && (Rain <= 0))
  82. {
  83. cout << setw(10) << " Rain fall must be greater than or equal to 0. " << endl;
  84. cout << setw(10) << " Enter the Rain Fall for this month: ";
  85. cin >> Rain;
  86.  
  87.  
  88. }
  89. }
  90.  
  91. double sumArray(double array[], int size)
  92. {
  93. double totalRainfall = 0; // Accumulator
  94. for (int count = 0; count < size; count++)
  95. totalRainfall += array[count];
  96. return totalRainfall;
  97. }
Reputation Points: 10
Solved Threads: 0
Light Poster
student4lyfe is offline Offline
40 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Setting array in contructor
Next Thread in C++ Forum Timeline: Columns in txt





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC