taking decimal input from the user in c++

I want the input for the price in decimal format(15.00). If the input is in integer format then I want to convert it to decimal format.

The maximum price for a sale is 999.99 and minimum is 0.00.

I have tried so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include<iostream>
using std::cout;
using std::cin;

void main(){
    float eventPrice;

    bool cPrice = true;
    while (cPrice)
    {
        cout << "Please enter the event ticket price:";
        cin >> eventPrice;
        if (eventPrice <= 999.99 && eventPrice>=0) cPrice = false;
        else
        {
            cout << "Invlaid price. Try again!!";
        }
    }
    cout << "the price is:";
        cout << eventPrice;
}

valid input will be:

1.8,
0.8,
0.00,
0,
9,
99
Other than void main() do you have an actual problem or a specific question?

not really but i have to follow specific format for the input for my assignment which my code doesn't for example if 15 is inserted by user then the output will be $15.00 and 1000 out put will be invalid
i think instead of taking input as a float i should take it in as string that would be easy to format
Or you could use some of the manipulators provided in <iomanip> along with things like std::fixed() and std::setprecision() so you can display 15 as 15.00 preceded by a '$' character.


Last edited on
Topic archived. No new replies allowed.