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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

#include <cstdlib>
#include <iostream>

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..


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.