Click to See Complete Forum and Search --> : Quiet not a number??? Help please!


Jpsarapuu
February 12th, 2008, 02:42 PM
I have no idea why this bug comes.

Look at the code:

#include <iostream>

using namespace std;

int main()
{
double yearsum[3];
double sum;
int nob[3][12]; // number of books
double price;
char * months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
cout << "Enter the price of the book: ";
cin >> price;
cout << "Enter a year's worth of monthly sales (in number of books)\n";

for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 12; j++)
{
cout << "Year " << i + 1 << "#, " << months[j] << ": ";
cin >> nob[i][j];
yearsum[i] += price * nob[i][j];
sum += price * nob[i][j];
}
}
cout << "\nYear 1# worth of monthly sales is $" << yearsum[0] << ".\n";
cout << "Year 2# worth of monthly sales is $" << yearsum[1] << ".\n";
cout << "Year 3# worth of monthly sales is $" << yearsum[2] << ".\n";
cout << "\nTotal worth of monthly sales is $" << sum << ".\n";
cin.get();
cin.get();
return 0;
}

Everything is ok but yearsum[1].
I can't understand why, because yearsum[0] & [1] are working fine.


Sample run:

Enter the price of the book: 29.95
Enter a year's worth of monthly sales (in number of books)
Year 1#, January: 4
Year 1#, February: 6
Year 1#, March: 2
Year 1#, April: 7
Year 1#, May: 4
Year 1#, June: 7
Year 1#, July: 5
Year 1#, August: 9
Year 1#, September: 3
Year 1#, October: 7
Year 1#, November: 4
Year 1#, December: 1
Year 2#, January: 8
Year 2#, February: 5
Year 2#, March: 9
Year 2#, April: 3
Year 2#, May: 6
Year 2#, June: 3
Year 2#, July: 8
Year 2#, August: 5
Year 2#, September: 3
Year 2#, October: 7
Year 2#, November: 3
Year 2#, December: 2
Year 3#, January: 9
Year 3#, February: 2
Year 3#, March: 1
Year 3#, April: 7
Year 3#, May: 4
Year 3#, June: 8
Year 3#, July: 3
Year 3#, August: 9
Year 3#, September: 5
Year 3#, October: 0
Year 3#, November: 7
Year 3#, December: 2

Year 1# worth of monthly sales is $1767.05.
Year 2# worth of monthly sales is $-1.#QNAN. // what the **** is this doing here?
Year 3# worth of monthly sales is $1707.15.

Total worth of monthly sales is $5331.1.

Help please.

Lindley
February 12th, 2008, 02:49 PM
I don't see you initializing yearsum to 0 anywhere.

Jpsarapuu
February 12th, 2008, 03:25 PM
I wanted to say that yearsum[0] and yearsum[2] display a correct value, but yearsum[1] doesn't. I don't see any logic here.

Jpsarapuu
February 12th, 2008, 03:28 PM
Ok, i found one solution, but it doesn't satisfy me.

The solution would be to use
char months[12][12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
instead of
char * months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
But I don't understand why this affects the value of yearsum[1].

Ideas, anyone?

potatoCode
February 12th, 2008, 04:03 PM
Hello Jpsarapuu,

I believe the problem lies with the following compound statement
yearsum[i] += price * nob[i][j];

because,
double yearsum[3]; // uninitialized, can lead to error if not careful
does not define the array.
the left hand operand of the assignment of yearsum in your compound statement
yearsum[i]/*this operand is OK defined*/ = yearsum[i] /* this yearsume is NOT ok */ + price * nob[i][j];" is just fine.
It's the right operand of the yearsum[i] that's causing the problem
because you are using an undefined element as part of the equation.
(please, correct me if I'm wrong. thanks)

like Lindley said,
had you defined your array in the first place, to something you thought the compiler think what you thought,
double yearsum[3] = {0};
your code would have worked just fine.

your first design of the program was fine,
so the only thing that needs to be changed is the code above.

I think this should solve the problem.
Thanks
*edit: I had the left and the right hand operand backwards.
sorry. :)

Jpsarapuu
February 13th, 2008, 02:19 PM
Thank you very much potatoCode, it helped!:D