943,916 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 489
  • C++ RSS
Dec 5th, 2008
0

arrays! a little help!

Expand Post »
oopss i still have a problem!!.. What's wrong with my code?
C++ Syntax (Toggle Plain Text)
  1. main()
  2. {
  3.  
  4. int a[4];
  5. int b[4];
  6. int counter,sum=0;
  7.  
  8. cout<<"JANUARY";
  9. for(counter=0;counter<4;counter++)
  10. {
  11. cout<<"Numbers of Rainfall:";
  12. cin>>a[counter];
  13. }
  14.  
  15. for(counter=0;counter<4;counter++)
  16. {
  17. sum+=a[counter];
  18. }
  19.  
  20. cout<<"The numbers of rainfall in the first month"<<sum<<endl;
  21.  
  22. cout<<FEBRUARY;
  23. for(counter=0;counter<4;counter++)
  24. {
  25. cout<<"Numbers of Rainfall";
  26. cin>>b[counter];
  27. }
  28.  
  29. for(counter=0;counter<4;counter++)
  30. {
  31. sum+=b[counter];
  32. }
  33.  
  34. cout<<"The numbers of rainfall in the second month"<<sum<<endl;
  35.  
  36. getch();
  37. return 0;
  38. }
now my aim is to add all the values of first month and then print it. and same as the second month.(add all the values of second month and then print it). And when i run the program and then i put a values in the first month and its working fine but my problem is in second month because after i put a values in second month, the output of 1st month will add to the values of second month.

ex.
JANUARY
Numbers of Rainfall:4
Numbers of Rainfall:3
Numbers of Rainfall:2
Numbers of Rainfall:1

The numbers of rainfall in the first month 10

FEBRUARY
Numbers of Rainfall:5
Numbers of Rainfall:4
Numbers of Rainfall:3
Numbers of Rainfall:2

The numbers of Rainfall in Second month 24

now i need help on the second month on how to print the second month without adding the first month..?

Sorry i'm a newbie programmer but i'm studying hard. thanks guys..
Last edited by Narue; Dec 5th, 2008 at 11:18 am. Reason: added code tags
Similar Threads
Reputation Points: 10
Solved Threads: 2
Light Poster
FrancisC07 is offline Offline
30 posts
since Feb 2008
Dec 5th, 2008
0

Re: arrays! a little help!

make sum = 0 after you have calculated the sum of first month.
Last edited by sweeya; Dec 5th, 2008 at 11:18 am.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
sweeya is offline Offline
8 posts
since Nov 2008
Dec 5th, 2008
0

Re: arrays! a little help!

You need int a[4] and int b[4] for every month, something like:

int a[12][4];
int b[12][4];
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007
Dec 5th, 2008
0

Re: arrays! a little help!

Just put sum = 0; before starting to add the second month rainfalls.

Please read this and make sure to use code tags when posting code from now on

EDIT: sweeya was faster, sorry.
Last edited by mrboolf; Dec 5th, 2008 at 11:21 am.
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Dec 5th, 2008
0

Re: arrays! a little help!

thanks for helping guys! sorry for i don't use the code tags.. it won't be happen again..
Reputation Points: 10
Solved Threads: 2
Light Poster
FrancisC07 is offline Offline
30 posts
since Feb 2008
Dec 5th, 2008
0

Re: arrays! a little help!

Well as they already said reset sum to zero; next problem in month two would be, you forgot quotes " in cout, and I'm assuming the lack of int before main() is a copy-paste oversight.
You could create a class that holds all of the months, extra info and functions that deal with them; if you've gotten that far in your studies, yet.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 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:
Previous Thread in C++ Forum Timeline: i want an instant answer
Next Thread in C++ Forum Timeline: How to Initialize COM Library in VC++.NET 2005





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


Follow us on Twitter


© 2011 DaniWeb® LLC