how to check if the input is double

closed account (48CM4iN6)
Hi guys . I'm new to c++ . My last objective is to check if the input has a decimal value . If it has, then it will not be accepted by the program and it will undergo the loop of the wrong input .


(I'll just show here the function part where my concern shows)

void widrawMoney(double& newbalance)
{

int thd=1000, fhnd=500, thnd=200, ohnd= 100 ;

int wnum, ctr;
double test;



cout<<" Enter amount to Withdraw: ";
cin>>wnum;

//this is the code for the breakdown of money inputted

thd= wnum/1000;
fhnd= (wnum%1000)/fhnd;
thnd= (wnum%1000)%500/200;
ohnd= (wnum%1000)%500%200/100;


if ((wnum>=500.00)&&(wnum<=5000.00))

{
cout<<" 1000 -> "<<thd<<endl
<<" 500 -> "<<fhnd<<endl
<<" 200 -> "<<thnd<<endl
<<" 100 -> "<<ohnd<<endl
<<" Total: "<<wnum<<endl;

newbalance= newbalance - wnum;
}
else if ((wnum<=500.00)||(wnum>=5000.00)) -----> i need another condition to test if the input has a decimal so that the input with decimal will proceed to this condition/loop
{
while ((wnum<500.00)||(wnum>5000.00))// loop for the validation of the range of amount to be inputted
{
cout<<" Amount should be 500 - 5000 only "<<endl;
cout<<" Enter amount to Withdraw: ";
cin>>wnum;

if ((wnum>=500.00)&&(wnum<=5000.00))

{
cout<<" 1000 -> "<<thd<<endl
<<" 500 -> "<<fhnd<<endl
<<" 200 -> "<<thnd<<endl
<<" 100 -> "<<ohnd<<endl
<<" Total: "<<wnum<<endl;


newbalance= newbalance - wnum;
}

ctr++;
}

}


}

any help would be appreciated
Get the input as a string. Check that is is composed only of digits and a single decimal point (and presumably that there are two digits only after the decimal point). If so, convert to a double and continue.
closed account (48CM4iN6)
The input should be int because there is part where the amount(input) is broken down to money/currency and my professor requires me. Is there any way to check if it has decimal value to insert in the condition ? And one more thing. The input is just a test to know if it has decimal value, then if it is, it will go to the loop for the re-entering of a correct amount of input
If you take the input from the user and store it in an int, I guarantee that it will not have a decimal point in it when you check that stored int value.
@Moschops is right in both things

2nd thing first

1
2
3
4
double dV =33.234;
int iV = dV;

cout << iV; // output 33 



Now 1st thing

1
2
3
4
5
6
char* cV = "33.234";
int iV;
if(strtok(cV,"."))
  cout << "Input is a double";
else
  iV = atoi(cV); // Now use this int 

Unfortunatly the thing isn't that easy. You might very well have a number like 10.00000001 (due to inaccuracy).

So how many digits do you want to consider? 2 4?

In case of 2 it's like
1
2
if(int(Value * 100) % 100) == 0)
  no_digits();
Last edited on
closed account (48CM4iN6)
@maschops . sorry , i have some misconceptions in what I had said . Thank you Danishx83 and coder777 . I have done what is asked
Topic archived. No new replies allowed.