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:
Can anyone give me an idea of what to try? PLEASE, PLEASE, PLEASE! :freak: Thank you for your time, Lori
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
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
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?
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.
if you want to terminate your program immediately you can do this: if (northRegionSales<0) exit(1); put it under all cin<<nortRegionSales;
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.
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;
http://www.cplusplus.com/reference/clibrary/cstdlib/exit/ when you finish main() you use return 0; ---->(exit(0) finished without errors) exit(1) --->reurns 1 which means the program terminated with error.