Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Need help for C++ programming (http://www.go4expert.com/forums/help-cpp-programming-t21361/)

ladyluck4772 15Mar2010 10:46

Need help for C++ programming
 
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:

ladyluck4772 15Mar2010 19:00

Re: Need help for C++ programming
 
Can anyone give me an idea of what to try? PLEASE, PLEASE, PLEASE! :freak:

Thank you for your time,
Lori

virxen 16Mar2010 02:34

Re: Need help for C++ programming
 
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 16Mar2010 04:29

Re: Need help for C++ programming
 
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 16Mar2010 04:59

Re: Need help for C++ programming
 
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 16Mar2010 10:24

Re: Need help for C++ programming
 
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 16Mar2010 16:45

Re: Need help for C++ programming
 
Quote:

Originally Posted by ladyluck4772 (Post 65732)
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 17Mar2010 08:53

Re: Need help for C++ programming
 
How do I change the post title? I don't see a way to edit it.

shabbir 17Mar2010 09:15

Re: Need help for C++ programming
 
Quote:

Originally Posted by ladyluck4772 (Post 65772)
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 20Mar2010 06:23

Re: Need help for C++ programming
 
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;


All times are GMT +5.5. The time now is 06:10.