DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   arrays! a little help! (http://www.daniweb.com/forums/thread160885.html)

FrancisC07 Dec 5th, 2008 10:09 am
arrays! a little help!
 
oopss i still have a problem!!.. What's wrong with my code?
main()
{

int a[4];
int b[4];
int counter,sum=0;

cout<<"JANUARY";
for(counter=0;counter<4;counter++)
{
cout<<"Numbers of Rainfall:";
cin>>a[counter];
}

for(counter=0;counter<4;counter++)
{
sum+=a[counter];
}

cout<<"The numbers of rainfall in the first month"<<sum<<endl;

cout<<FEBRUARY;
for(counter=0;counter<4;counter++)
{
cout<<"Numbers of Rainfall";
cin>>b[counter];
}

for(counter=0;counter<4;counter++)
{
sum+=b[counter];
}

cout<<"The numbers of rainfall in the second month"<<sum<<endl;

getch();
return 0;
}
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..

sweeya Dec 5th, 2008 10:17 am
Re: arrays! a little help!
 
make sum = 0 after you have calculated the sum of first month.

jencas Dec 5th, 2008 10:19 am
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];

mrboolf Dec 5th, 2008 10:21 am
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.

FrancisC07 Dec 5th, 2008 10:33 am
Re: arrays! a little help!
 
thanks for helping guys! sorry for i don't use the code tags.. it won't be happen again..

MosaicFuneral Dec 5th, 2008 1:22 pm
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.


All times are GMT -4. The time now is 4:21 am.

Forum system based on vBulletin Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
©2003 - 2010 DaniWeb® LLC