### Looping Program

I just wrote a simple program that calculates markup prices by calling user defined functions I created. That part is done, but I need to make a validation check where the user cannot enter in a negative number. I sorta did that using the if statement, but the program needs to repeat until the user enters a valid number. Any help with this? The way I have it set up, if the user enters a negative number, it just stops.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` `````` #include #include using namespace std; float caculateRetail100(float a) { float b; b=a*2; return (b); } float caculateRetail50(float a) { float b; b=(a/2)+a; return (b); } int main() { cout << "Please enter the wholesale cost of an item:" << endl << endl <<"\$"; float x; cin >> x; do { if (x < 0) { cout << "Invalid number. Please enter a non-negative cost number." << endl << endl; } else { cout << endl << "The mark up percentage at 100% is: \$" << caculateRetail100 (x) << endl << endl; cout << endl << "The mark up percentage at 50% is: \$" << caculateRetail50 (x) << endl << endl; } } while(x<0); system("PAUSE"); return EXIT_SUCCESS; } ``````
@epicred
Hope this helps..

 ``1234567891011121314151617181920`` ``````int main() { float x = 0.0; // Initialize as 0. do { cout << "Please enter the wholesale cost of an item:" << endl << endl <<"\$"; // Move request here cin x; if (x <= 0.0) { cout << "Invalid number. Please enter a positive cost number." << endl << endl; } else { cout << endl << "The mark up percentage at 100% is: \$" << caculateRetail100 (x) << endl << endl; cout << endl << "The mark up percentage at 50% is: \$" << caculateRetail50 (x) << endl << endl; } } while(x<=0.0);``````
Thanks! This worked!
@epicred

You're welcome. Mark the thread as solved.
Topic archived. No new replies allowed.