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;
}

Recommended Answers

All 5 Replies

Please could you post what the exercise is so that we have a better idea of what's going on. Thanks :)

Please could you post what the exercise is so that we have a better idea of what's going on. Thanks :)

Hey, thanks for replying. As said earlier, the exercise is in the header comment, but now I see I hadn't been very elaborate :)) so here it is:

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

Ah apologies. My fault :)

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;
}

Hi Tdba,

The small mistake you did when declaring the

long TotalSalesForIndividualYears[YEARS]

variable. Since you are summing up all the month value to this variable this variable should be initialized with 0.

Here is the fix for your code.Just initialized the variable with 0

long TotalSalesForIndividualYears[YEARS]={0};

Hi Tdba,

The small mistake you did when declaring the

long TotalSalesForIndividualYears[YEARS]

variable. Since you are summing up all the month value to this variable this variable should be initialized with 0.

Here is the fix for your code.Just initialized the variable with 0

long TotalSalesForIndividualYears[YEARS]={0};

Hey Perry, thanks! Thant worked perfect xD The results are now correctly 1200s and 3600 <3 small mistakes like these happen all the time for me. I have to be more careful!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.