Need help for C++ programming

ladyluck4772's Avatar, Join Date: Mar 2010
Go4Expert Member
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!

Last edited by shabbir; 15Mar2010 at 11:30.. Reason: Code blocks
ladyluck4772's Avatar, Join Date: Mar 2010
Go4Expert Member
Can anyone give me an idea of what to try? PLEASE, PLEASE, PLEASE!

Thank you for your time,
Lori
virxen's Avatar, Join Date: Nov 2009
Pro contributor
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;
 while (northRegion <3 && northRegionSales>=0)
 {
  northRegion++;
  totalNorthSales = totalNorthSales + northRegionSales;
  cout << "Next sales amount for North region (negative number to stop): $ ";
  cin >> northRegionSales;
 }
 cout << endl;
 if (northRegionSales>=0) totalNorthSales = totalNorthSales + northRegionSales;
 
  //enter sales amounts for East region
do the same changes for the rest regions
ladyluck4772 like this
ladyluck4772's Avatar, Join Date: Mar 2010
Go4Expert Member
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
ladyluck4772's Avatar, Join Date: Mar 2010
Go4Expert Member
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?
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
virxen's Avatar, Join Date: Nov 2009
Pro contributor
Quote:
Originally Posted by ladyluck4772 View Post
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?
if you want to terminate your program immediately you can do this:
if (northRegionSales<0) exit(1);

put it under all
cin<<nortRegionSales;
ladyluck4772's Avatar, Join Date: Mar 2010
Go4Expert Member
How do I change the post title? I don't see a way to edit it.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by ladyluck4772 View Post
How do I change the post title? I don't see a way to edit it.
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.
ladyluck4772's Avatar, Join Date: Mar 2010
Go4Expert Member
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;