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.
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.