Help asap

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: Help asap

 
0
  #11
Dec 15th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 61
Reputation: anbuninja is an unknown quantity at this point 
Solved Threads: 0
anbuninja anbuninja is offline Offline
Junior Poster in Training

Re: Help asap

 
0
  #12
Dec 15th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 61
Reputation: anbuninja is an unknown quantity at this point 
Solved Threads: 0
anbuninja anbuninja is offline Offline
Junior Poster in Training

Re: Help asap

 
0
  #13
Dec 15th, 2008
anyone know why this isnt working?

  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: Help asap

 
0
  #14
Dec 15th, 2008
You didn't tell it to do so, that's why it would not terminate the program.

  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 61
Reputation: anbuninja is an unknown quantity at this point 
Solved Threads: 0
anbuninja anbuninja is offline Offline
Junior Poster in Training

Re: Help asap

 
0
  #15
Dec 15th, 2008
cool thanks!

and actually im using count variable for the avg and it works fine.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: Help asap

 
0
  #16
Dec 15th, 2008
Yes but count == totalMonths
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: Help asap

 
0
  #17
Dec 16th, 2008
(sorry missed second page of comments)
Last edited by Murtan; Dec 16th, 2008 at 12:47 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 6
Reputation: deviantrunner is an unknown quantity at this point 
Solved Threads: 0
deviantrunner deviantrunner is offline Offline
Newbie Poster

Re: Help asap

 
0
  #18
Dec 16th, 2008
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
  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. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC