How do I create a C++ program displaying total sales made during the three months?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wutang
    New Member
    • Oct 2009
    • 5

    How do I create a C++ program displaying total sales made during the three months?

    Create a program that displays the sum of the sales amounts made in each of four regions (North, South, East, West) during a three month period. The program should display the total sales made during the three months.
    1. Complete the program by entering the C++ code that allows the user to enter four sets(one set for each region) of three sales amounts (one sales amount for each month). The program should display each region's total sales for the 3-month period, and the company's total sales for the 3-month period.
    -You must use some type of loop to receive credit for the exercise....can not use arrays

    What I have so far.....
    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    
    int main()
    {
    //declare variables
    char region = ' ';
    int monthlySales1 = 0;
    int monthlySales2 = 0;
    int monthlySales3 = 0;
    int totalSales = 0;
    int totalRegSales = 0;
    
    cout << "Enter region (N/S/E/W): ";
    cin >> region;
    
    do
    {
    // Get Monthly sales...
    cout << "First monthly sales amount for Region " << region << ": ";
    cin >> monthlySales1;
    
    cout << "Next monthly sales amount for Region " << region << ": ";
    cin >> monthlySales2;
    
    cout << "Next monthly sales amount for Region " << region << ": ";
    cin >> monthlySales3;
    
    // Add our regional sales
    totalRegSales = monthlySales1 + monthlySales2 + monthlySales3;
    
    // Display the sales made for that region....
    cout << "Total sales for Region " << region << ": " << totalRegSales << "\n\n";
    
    totalSales += totalRegSales;
    
    // And if our total sales is more than 0, then display it before
    // asking for our next region.
    if( totalSales > 0 ){
    cout << "\nTotal Sales at this point: " << totalSales << "\n\n";
    }
    
    cout << "Enter region (N/S/E/W): ";
    cin >> region;
    }
    while (region == 'N' || region == 'S' || region == 'E' || region == 'W');
    return 0;
    }
    Last edited by Banfa; Oct 21 '09, 08:03 PM. Reason: Added [Code]...[/code] tags
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    What about this program doesn't work?

    Comment

    • wutang
      New Member
      • Oct 2009
      • 5

      #3
      ok here I go.
      1. I can't seem to get a " Invalid entry" code if I enter say ' T ' instead of ' N' , ' S ', ' E', or ' W '.

      2. I also added region = toupper(region) in order to make the letter of region capitalized. When it asks for a another region, I enter lower case ' w ' but it does not let me keep adding information. It will if I enter a capital ' W '.

      3. Also im not sure how to make it stop after I enter the fourth region.



      sorry and thanks. I'm a Noob when it comes to C++ still. Is there a better way to do this than what I have?

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        Think you are on the right track.
        Concentrate on getting the N region to print the correct result. Then add the code for the next region and so on.
        Two loops would appear practical with the first loop controlling region and the second nested one controlling sales total. Maybe with 2 loops you will get double marks!

        Comment

        • ekassouf1
          New Member
          • Oct 2011
          • 1

          #5
          okay, this is what i have, i know it might too late for a post but im practicing this....


          Code:
          //Ch8AppE05.cpp
          //Displays the quarterly sales made 
          //in each of four regions, and the total 
          //quarterly sales for the company
          //Created/revised by <Elie Kassouf>
          
          #include <iostream>
          #include <iomanip>
          
          using std::cout;
          using std::cin;
          using std::endl;
          using std::setprecision;
          using std::ios;
          
          typedef struct st_quarter
          {
              float month_1;
              float month_2;
              float month_3;
          } QUARTERLY_SALES;
          
          typedef struct st_region
          {
              char name[24];
              QUARTERLY_SALES quarter;
          } SALES_REGION;
          
          void input_quarterly_sales_by_region( SALES_REGION* region )
          {
              cout << "Enter sales for Month 1 in Region " << region->name << ": ";
              cin >> region->quarter.month_1;
          
              cout << "Enter sales for Month 2 in Region " << region->name << ": ";
              cin >> region->quarter.month_2;
          
              cout << "Enter sales for Month 3 in Region " << region->name << ": ";
              cin >> region->quarter.month_3;
          }
          
          void display_quarter_sales_total_by_region( const SALES_REGION* region )
          {
              cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 2 );
              cout << "Total Quarter Sales for Region " << region->name << " = " << 
          	    (region->quarter.month_1 + region->quarter.month_2 + region->quarter.month_3) << endl;
          }
          
          void display_quarterly_sales_total_for_all_regions( const SALES_REGION* regions, const int count )
          {
              float total = 0.0F;
              for( int i = 0; i < count; i++ )
              {
          	total += (regions[i].quarter.month_1 + regions[i].quarter.month_2 + regions[i].quarter.month_3);
              }
              cout << "Total Quarter Sales for All Regions: " << total << endl;
          }
          
          int main()
          {
          char c;
          do {
              SALES_REGION regions[] = { {"North", {0} },
          			       {"South", {0} },
          			       {"East", {0} },
          			       {"West", {0} } };
          
              for( int i = 0; i < (int)(sizeof( regions )/sizeof( SALES_REGION )); i++ )
              {
          	input_quarterly_sales_by_region( &regions[i] );
              }
          
              for( int i = 0; i < (int)(sizeof( regions )/sizeof( SALES_REGION )); i++ )
              {
          	display_quarter_sales_total_by_region( &regions[i] );
              }
          
              display_quarterly_sales_total_for_all_regions( regions, (int)(sizeof( regions )/sizeof( SALES_REGION )) );
              
              cout<<"Do you want to continue (Y/N)?">
              cin>>c;
              }while(c=='y'||c=='Y');
              
              system("pause");
          
          
              return 0;
          }   //end of main function
          Last edited by Banfa; Oct 21 '11, 01:50 PM. Reason: Added [code] ... [/code] tags round the code, please use the in future

          Comment

          Working...