-
September 9th, 2009, 12:10 PM
#1
loop and array problem for beginner. Please HELP!
The code below is a program to input and print a number of books sold for three years. I'm not able to print the sum of books sold for each year, but I can print the total number of books sold for the 3years. I'm a beginner. I need help to enable continue my reading.
I will appreciate if you can throw more lights on array that could be or help.
Thanks
#include <iostream>
#include <string>
int main()
{
using namespace std;
int counter1 = 2000;
int SumSold[3][12];
int NumSold[3];
string months[3][12] =
{
{
"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"
},
{
"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"
},
{
"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"
}
};
cout << "NO. OF BOOKS SOLD FROM 2001 - 2003 " << endl << endl;
for(int i = 0; i < 3; ++i)
{
++counter1;
cout << "YEAR " << counter1 << endl;
for(int ix = 0; ix < 12; ++ix)
{
cout << "Books sold in " << months[i][ix] << ": ";
cin >> NumSold[ix];
SumSold[i][ix] = SumSold[i][ix] + NumSold[ix];
}
}
for(int iv = 0; iv < 3; ++iv)
{
for(int inval = 0; inval = 12; ++inval)
{
cout << SumSold[iv][inval] << endl;
}
}
return 0;
}
-
September 9th, 2009, 12:13 PM
#2
Re: loop and array problem for beginner. Please HELP!
It doesn't seem terribly useful to duplicate the months 3 times like that. It's not like they're about to change or anything.
-
September 9th, 2009, 12:19 PM
#3
Re: loop and array problem for beginner. Please HELP!
Good point sir. Thanks alot
-
September 9th, 2009, 12:42 PM
#4
Re: loop and array problem for beginner. Please HELP!
You need to initialize your numeric arrays to contain 0s. You seem to be assuming they will for now, but that's not guaranteed. Just adding "= {0};" after the declaration should do it.
-
September 9th, 2009, 01:00 PM
#5
Re: loop and array problem for beginner. Please HELP!
Another important point.
Thanks.
Is it possible to add an array(1 or 2D) to an integer and assign the result to an integer?
-
September 9th, 2009, 01:17 PM
#6
Re: loop and array problem for beginner. Please HELP!
You mean sum up the values in the array? You could use std::accumulate, but for a beginner it's probably best to just use a loop. That's what accumulate() is doing internally anyway.
-
September 11th, 2009, 11:41 AM
#7
Re: loop and array problem for beginner. Please HELP!
-
September 13th, 2009, 03:04 AM
#8
Re: loop and array problem for beginner. Please HELP!
Im just interested to know why you used "endl" twice in the 'number of books sold from 2001-2003 bit, or is that just a mistake. Sorry Im a total noob at C++.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|