Hey :D this is my first post here^^, and it's a problem with an exercise from the book C++ Primer Plus, which I have been self-studying (I'm the beginner).
The following code is how I did the exercise, which is in the header comment. It was built in VC++ 2010, and I entered 100 for all the 12 months for all 3 years in the resulting program.
The result should have been 1200 for each individual year and 3600 for all the years, but the program reported -858992260 for each individual year and 1717990516 for the combined years O.O I can't figure out what the problem is, so I hope you guys will help me^^
Here is the code:
/*
Do Programming Exercise 4, but use a two-dimensional array to store input for 3 years
of monthly sales. Report the total sales for each individual year and for the combined
years.
Exercise 4:
You sell the book C++ for Fools. Write a program that has you enter a year’s worth of
monthly sales (in terms of number of books, not of money). The program should use a
loop to prompt you by month, using an array of char * (or an array of string objects, if
you prefer) initialized to the month strings and storing the input data in an array of int.
Then, the program should find the sum of the array contents and report the total sales
for the year.
*/
#include <iostream>
#include <string>
int main()
{
using std::cin; using std::cout; using std::endl;
using std::string;
cout << "C++ For Fools Sales Manager" << endl;
//Define the items:
const int MONTHS = 12, YEARS = 3;
const string MONTH_LABELS[MONTHS] =
{
"January","February","March","April","May","June",
"July","August","September","October","November","December"
};
long Sales[YEARS][MONTHS]; //Store sales data for 3 years, 12 months each
cout << "Please enter the number of books sold each month for three years.\n"
<< "Sales Manager will then report the total sales for each year\n"
<< "and for the combined years.\n" << endl;
//GET DATA:
/*
The Loop for user-input data: The Months loop nested in the Years loop.
The Years loop:
- Display the number of year
- Execute the Months loop
- Increase the counter
The Months loop:
- Display the month label
- Get input
- Increase the counter
*/
// The YearCounter starts at 0: runs the loop 3 times
for (int YearCounter = 0; YearCounter < YEARS; ++YearCounter)
{
cout << endl << "Year " << YearCounter + 1 << ": " << endl; //increase the counter to display the year correctly (arrays start at 0)
/*
The MonthCounter has to be 0 to start the loop by:
- Displaying the first element in MONTH_LABELS array;
- Assigning the sales to the first year's January
Similarily to the YearCounter.
*/
for (int MonthCounter = 0; MonthCounter < MONTHS; MonthCounter++)
{
cout << MONTH_LABELS[MonthCounter] << ": ";
cin >> Sales[YearCounter][MonthCounter];
}
}
//DATA PROCESSING
//The array to store the individual sums (Each element is a year's sum):
long TotalSalesForIndividualYears[YEARS];
//The variable to store the combined:
long TotalSalesForCombinedYears = 0;
//The Loop for summing up the sales for Individual years:
for (int YearCounter = 0; YearCounter < YEARS; ++YearCounter)
{
for (int MonthCounter = 0; MonthCounter < MONTHS; ++MonthCounter)
{
TotalSalesForIndividualYears[YearCounter] += Sales[YearCounter][MonthCounter];
}
}
//The Loop for summing up the sales for Combined Years:
for (int YearCounter = 0; YearCounter < YEARS; ++YearCounter)
{
TotalSalesForCombinedYears += TotalSalesForIndividualYears[YearCounter];
}
//REPORTING:
cout << "The total sales for: " << endl;
//The Loop for reporting individual years' total sales:
for (int YearCounter = 0; YearCounter < YEARS; ++YearCounter)
{
cout << "Year " << YearCounter + 1 << " is " << TotalSalesForIndividualYears[YearCounter] << endl;
}
//Report total sales for combined years:
cout << "The total sales for combined years is " << TotalSalesForCombinedYears;
cin.clear(); cin.get(); cin.get();
return 0;
}