C++ Assignment 5.4

Moderators: vinyard, KreepingMonkey

Post Reply
Message
Author
User avatar
vinyard
Server Admin
Posts: 301
Joined: Fri Jun 15, 2007 7:43 pm
Operating System: Windows Vista Ultimate 32bit
CPU: Intel C2D E8200 [Stock]
Video Card: EVGA GeForce 8800 GT 512MB SC
Sound Card: Onboard
Memory: Corsair Twin2x2048-6400Pro
Motherboard: EVGA 680i AR1 p32
Contact:

C++ Assignment 5.4

#1 Post by vinyard » Fri Oct 12, 2007 12:17 am

Here is assignment 5.4.
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.
Here is my work, It was very easy to do but it can be improved.

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
}
If you have any suggestions or ways to improve the code post away. :)
- vinyard.

Post Reply