0

I'm writing a C++ program for bank to withdrawal money , in that code I used fixed and set precision to change the output for two decimal point but I don't want to do that , I want to make sure the user only need to input in decimal number I mean like only two decimal number . I want like he/she can't able to enter more than two decimal number .00

  • 1
    https://stackoverflow.com/questions/25987688/how-can-i-limit-the-input-the-number-of-decimals-till-which-user-can-input-value – Omid CompSCI Aug 12 '21 at 05:04
  • I think the question linked by Omid is a pretty good duplicate. In addition you could take inspiration from [this question about input validation loops](https://stackoverflow.com/questions/2075898/good-input-validation-loop-using-cin-c). If you combine both you get the rough idea: 1) Read input as string 2) check if the string is a decimal number with at most two decimal places 3) if the value is valid, convert it to a number, else show an error and prompt again. – Lukas-T Aug 12 '21 at 07:11
  • 1
    Btw, in financial math **never** use floating point numbers (`float` or `double`). It might work for simple examples, but it's imprecise and will get you in all sorts of trouble when cents start to dissappear or appear out of thin air. – Lukas-T Aug 12 '21 at 07:13

1 Answers1

0

You can't do this. At least not with standard input and CPP uses the same for taking input.

The best you can do is to take input into a value and cut off the decimal part after the second digit using setprecision(2), or truncate the value once you have it in the variable.

Hope it helped you.