Here is my work, It was very easy to do but it can be improved.Programming Exercises: Pg. 229, #4 wrote: 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 data in an array of int. Then, the program should find the sum of the array contents and report the total sales of the year.
Code: Select all
/** Assignment 5.4 ********
** Code by: David Rivera **
** 10.11.2007 ************/
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main()
{
const int arraySize = 12; //Size of arrays
const string month[arraySize] = {"January","February","March","April","May","June", //Array of Months
"July","August","September","October","November","December"};
int monthSales[arraySize]; //Array for month sales
for(int i =0; i < arraySize; i++) //While i is less than 12
{
cout << "Enter the number of C++ for Fools books sold for the month of " //Promt user for nuber of books sold
<< month[i] << "." << endl; // for month[i] (i can be 0 through 11)
cin >> monthSales[i]; //Get number of books sold
}
int yearSum = 0; //Declare variable yearSum
for(int i = 0; i < arraySize; i++) //While i is less than 12
{
yearSum += monthSales[i]; //Add all monthly book sales
}
cout << "The total number of C++ for Fools books sold for the year is " << yearSum << "." << endl; //Output total of books sold for that year
system("pause"); //Wait for user to press a key
}