Need help for C++ programming

Discussion in 'C++' started by ladyluck4772, Mar 15, 2010.

  1. ladyluck4772

    ladyluck4772 New Member

    Joined:
    Mar 15, 2010
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    I am back again with another problem. Hope someone can help. I can get the program to run, but I cannot figure out how to end the program when sales < than 0 for each section.

    The problem is this:

    Create a program that displays the sum of the sales amounts made in each of four regtions (North, East, South, and West) during a 3 month period. The program also should display the total sales made during the three months. The C++ code should allow the user to enter 4 sets (one set for each region) of 3 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.
    Use the following four sets of data to test the program:
    2000, 4000, 3000
    2000, 5000, 5000
    3000, 2000, 1000
    4000, 1000, 6000

    Here is my program so far:
    Code:
     
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
     
    int main()
    {
    
     //declare variables
     int northRegionSales = 0;
     int totalNorthSales = 0;
     int northRegion = 1;
     int eastRegionSales = 0;
     int totalEastSales = 0;
     int eastRegion = 1;
     int southRegionSales = 0;
     int totalSouthSales = 0;
     int southRegion = 1;
     int westRegionSales = 0;
     int totalWestSales = 0;
     int westRegion = 1;
     int totalSales = 0;
     
     //enter sales amounts for North region
     cout << "Sales amount for North region (negative number to stop): $ ";
     cin >> northRegionSales;
     do
     {
      northRegion += 1;
      totalNorthSales = totalNorthSales + northRegionSales;
      cout << "Next sales amount for North region (negative number to stop): $ ";
      cin >> northRegionSales;
     } while (northRegion <= 2); 
     cout << endl;
     totalNorthSales = totalNorthSales + northRegionSales;
     
      //enter sales amounts for East region
      cout << "Sales amount for East region (negative number to stop): $ ";
      cin >> eastRegionSales;
       do
       {
        eastRegion += 1;
        totalEastSales = totalEastSales + eastRegionSales;
        cout << "Next sales amount for East region (negative number to stop): $ ";
        cin >> eastRegionSales;
       } while (eastRegion <= 2);
       cout << endl;
       totalEastSales = totalEastSales + eastRegionSales;
    
      //enter sales amounts for South region
      cout << "Sales amount for South region (negative number to stop): $ ";
      cin >> southRegionSales;
       do
       {
        southRegion += 1;
        totalSouthSales = totalSouthSales + southRegionSales;
        cout << "Next sales amount for South region (negative number to stop): $ ";
        cin >> southRegionSales;
       } while (southRegion <= 2);
      cout << endl;
      totalSouthSales = totalSouthSales + southRegionSales;
     
      //enter sales amounts for West region
      cout << "Sales amount for West region (negative number to stop): $ ";
      cin >> westRegionSales;
       do
       {
        westRegion += 1;
        totalWestSales = totalWestSales + westRegionSales;
        cout << "Next sales amount for South region (negative number to stop): $ ";
        cin >> westRegionSales;
       } while (westRegion <= 2);
      cout << endl;
      totalWestSales = totalWestSales + westRegionSales;
     // end do while
     
      //display total sales for each region
      cout << "Total sales for North region: $ " << totalNorthSales << endl;
      cout << "Total sales for East region: $ " << totalEastSales << endl;
      cout << "Total sales for South region: $ " << totalSouthSales << endl;
      cout << "Total sales for West region: $ " << totalWestSales << endl;
      totalSales = totalNorthSales + totalEastSales + totalSouthSales + totalWestSales;
      cout << "Total sales: $ " << totalSales << endl;
      return 0;
    }   //end of main function
     
    Thank u for your help! :cryin:
     
    Last edited by a moderator: Mar 15, 2010
  2. ladyluck4772

    ladyluck4772 New Member

    Joined:
    Mar 15, 2010
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Can anyone give me an idea of what to try? PLEASE, PLEASE, PLEASE! :freak:

    Thank you for your time,
    Lori
     
  3. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    if i understand correctly what you want try this

    Code:
     //enter sales amounts for North region
     cout << "Sales amount for North region (negative number to stop): $ ";
     cin >> northRegionSales;
     [COLOR=Red]while (northRegion <3 && northRegionSales>=0)[/COLOR]
     {
      northRegion++;
      totalNorthSales = totalNorthSales + northRegionSales;
      cout << "Next sales amount for North region (negative number to stop): $ ";
      cin >> northRegionSales;
     [COLOR=Red]}[/COLOR]
     cout << endl;
    [COLOR=Red] if (northRegionSales>=0) [/COLOR]totalNorthSales = totalNorthSales + northRegionSales;
     
      //enter sales amounts for East region
    
    do the same changes for the rest regions
     
  4. ladyluck4772

    ladyluck4772 New Member

    Joined:
    Mar 15, 2010
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Where were u yesterday when I needed you, lol. I do think this will work. I was using the wrong loop command I guess. Plus, I did not know that you could use two different variable in one condition. I will try this and let you know how it goes. Thank you so much.
    Lori
     
  5. ladyluck4772

    ladyluck4772 New Member

    Joined:
    Mar 15, 2010
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    OK, this works great, but shouldn't the program go to the very end as soon as a negative number is entered. Is this even possible?
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You may be back with another problem but the title you have given to your problem is nothing different from your other problem.

    Try give better title for good responses.

    See your latest thread - Need help for C++ programming

    I was about to close that.
     
  7. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    if you want to terminate your program immediately you can do this:
    if (northRegionSales<0) exit(1);

    put it under all
    cin<<nortRegionSales;
     
  8. ladyluck4772

    ladyluck4772 New Member

    Joined:
    Mar 15, 2010
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    How do I change the post title? I don't see a way to edit it.
     
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Not possible now but keep that in mind for the next time or if you really want to do let me know and I will do that for you.
     
  10. ladyluck4772

    ladyluck4772 New Member

    Joined:
    Mar 15, 2010
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    What does the "exit (1) in the following mean? We have not covered that in computer class yet.

    if you want to terminate your program immediately you can do this:
    if (northRegionSales<0) exit(1);

    put it under all
    cin<<nortRegionSales;
     
  11. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    exit the program with the return code as 1
     
  12. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice