## uhcoog1989 4 years ago need help with showing the negatives in my C++ program

1. uhcoog1989

here is the question Your program will continue asking for the sales until the user types a negative number for sales, then the program should ask the user, if he/she wants to terminate the program. The program terminates if the user answers yes or y.

2. Narsat

I'm not quite sure on C++ syntax, but you need a loop that looks like this: int number; do{ ask value of number from input }while (number >= 0); //This loop will repeat until number is -1 or smaller As for the second part, what happens if the user doesn't want to terminate the program, it starts over?

3. uhcoog1989

this is what i have so far it runs just cant get it to ask for termination #include <iostream> #include <iomanip> using namespace std; double CalcCommision (double &sale) { double commission; if(sale <=999) commission= sale * .035; //This equation calculate commission that is 3.5% of sales between \$0 and \$999 else if(sale >= 1000 <=1999) commission= sale * .040; //This equation calculates commission that is 4% of sales between \$1000 and \$1999 else if(sale >= 2000 <=2999) commission= sale * .045; //This equation calculates commission that is 4.5% of sales between \$2000 and \$2999 else if (sale <=3000) commission= sale * .050; //This equation calculates commission that is 5% of sales \$3000 or greater return commission; } int main() { string yes; double sale; sale = 0; // initialize sale cout << "5000" <<endl << ".050" <<endl << endl << "In order to exit the program, you must enter a negative number." << endl; cout << "2999: \$ "; cin >> sale; while (true) //the while statement starts loop, while statement is being utilized if a positive number is entered //once an ammount is entered, the commission value returned to main module is displayed with CalcCommission {if(sale>=0) {cout << endl; cout << endl; cout << endl; cout << endl; cout << "1000: \$" << sale << ".040: \$" << CalcCommision (sale) << endl; } else // if a negative number is entered, program jumps here to ask user if they would like to terminate the program { cout << endl; cout << endl; cout<< endl; cout << "y"; cin>>yes; if(yes.compare("yes")==0| } cout << "1999: \$ "; cin >> sale; } }

4. Narsat

There's two ways to solve this. 1. You can put everything in your main() function inside a big do-while, with the "yes/y" condition at the end, where you exit the cycle if the user has typed yes or y. 2. Inside an if{ } you can use exit(0), where 0 indicates the status in which the process terminates (0 = normally, no errors). This will end your program. http://www.cplusplus.com/reference/clibrary/cstdlib/exit/

5. uhcoog1989

thanks