943,539 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1082
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Dec 15th, 2008
0

Re: Help asap

You want totaMonths to be the months that you entered data for? You don't need the variable, either use numYears*12 or declare month outside the loop and then use its last value.
Last edited by mrboolf; Dec 15th, 2008 at 7:19 pm.
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Dec 15th, 2008
0

Re: Help asap

ohh ok yeah numYears * 12 works perfect.
i was also able to calcluate the avg damn that was a bit hard. once again thanks for the help.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
anbuninja is offline Offline
61 posts
since Mar 2008
Dec 15th, 2008
0

Re: Help asap

anyone know why this isnt working?

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.  
  10. double totalRain = 0.0;// total rainfall inches
  11. double count = 0.0;
  12. int numData;
  13. char answer;
  14.  
  15. cout <<"For how many years have you collected rainfall data? ";
  16. cin >> numData;
  17. if (numData < 1)
  18. {
  19. cout <<"Invalid: number of years must be at least one." << endl;
  20. cout <<"Do you want to try again (y/n)?";
  21. cin >> answer;
  22. {
  23. if (answer == 'Y' || answer == 'y')
  24. cout <<"For how many years have you collected rainfall data? ";
  25. cin >> numData;
  26. if (answer != 'Y' || answer != 'y')
  27. cout <<"Goodbye";
  28. }
  29. }
  30.  
  31. for(int month = 0; month < numData*12; month++)
  32.  
  33. {
  34. float inches;
  35. cout << "Enter rainfall (in inches) for month " << (month%12) + 1 << " of year "
  36. << (month/12) + 1 << ": ";
  37. cin >> inches;
  38.  
  39. count++;
  40. totalRain += inches;
  41.  
  42.  
  43. }
  44. cout <<"The total number of months of rainfall data:" << numData * 12 << endl;
  45. cout <<"The total inches of rainfall for that period:" << totalRain << endl;
  46. cout <<"The average rainfall per month for that period:" << totalRain / (float)count << endl;
  47.  
  48.  
  49. system("pause");
  50. return 0;
  51. }

the part where you enter
would you like to try again y/n)?
it works fine if i put y or Y but if i dont want to continue it wont terminate the program.

ex

For how many years have you collected rainfall data? -4
Invalid: number of years must be at least one.
Do you want to try again (y/n)? y
For how many years have you collected rainfall data? 0
Invalid: number of years must be at least one.
Do you want to try again (y/n)? y
For how many years have you collected rainfall data? 2
//Program continues as in Part 1
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
anbuninja is offline Offline
61 posts
since Mar 2008
Dec 15th, 2008
0

Re: Help asap

You didn't tell it to do so, that's why it would not terminate the program.

c++ Syntax (Toggle Plain Text)
  1. if(choice=='Y') {
  2. // do something
  3. }
  4. else {
  5. return EXIT_FAILURE;
  6. }

You should put your input validation in a loop though - what if the user enters two times consecutively a number of years <= 0 ?

Also you don't need count variable: you already have totalMonths (i.e. numData * 12).
Last edited by mrboolf; Dec 15th, 2008 at 8:09 pm.
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Dec 15th, 2008
0

Re: Help asap

cool thanks!

and actually im using count variable for the avg and it works fine.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
anbuninja is offline Offline
61 posts
since Mar 2008
Dec 15th, 2008
0

Re: Help asap

Yes but count == totalMonths
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Dec 16th, 2008
0

Re: Help asap

(sorry missed second page of comments)
Last edited by Murtan; Dec 16th, 2008 at 12:47 am.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 16th, 2008
0

Re: Help asap

well hope this helps...i ddnt check the other guys...just wanted 2 do smting cuz i was bored...if u find something wrong tell me...thnx... xD
C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. int nb_of_year;
  3. int i,j,n;
  4. double r_inches;//rainfall inches
  5. int totalm=0;//total months
  6. double total_i;//total inches
  7. double averagei;
  8.  
  9.  
  10. int main()
  11. {
  12. cout << "enter the number of years u wanna do the calculations for :\n";
  13. cin >> n;
  14.  
  15. for (i=1;i<=n;i++)
  16. {
  17. for (j=1;j<=12;j++)
  18. {
  19. cout << "Enter the rainfall (in inches) for month "<< j << " of year ";
  20. cout << i << ":\n";
  21. cin >> r_inches;
  22.  
  23. total_i+=r_inches;
  24.  
  25. totalm+=1;
  26.  
  27. }
  28.  
  29. }
  30.  
  31.  
  32. cout << "The total number of months of rainfall data :" << totalm << endl;
  33. cout << "The total inches of rainfall for that period:" << total_i << endl;
  34. averagei=total_i/totalm;
  35.  
  36. cout << "The average rainfall per month for that period is:" << averagei <<endl;
  37.  
  38.  
  39.  
  40. return 0;
  41. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deviantrunner is offline Offline
6 posts
since Jul 2008

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:





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


Follow us on Twitter


© 2011 DaniWeb® LLC