# How can i stop a user from inputting a number as a decimal in an integer variable

**URL:** <https://forum.kirupa.com/t/how-can-i-stop-a-user-from-inputting-a-number-as-a-decimal-in-an-integer-variable/329850>\
**Category:** Uncategorized\
**Created:** [October 24, 2012, 8:49pm UTC](https://forum.kirupa.com/t/how-can-i-stop-a-user-from-inputting-a-number-as-a-decimal-in-an-integer-variable/329850 "2012-10-24T20:49:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![sumosaurus](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@sumosaurus](https://forum.kirupa.com/u/sumosaurus)\
**Post date:** [October 24, 2012, 8:49pm UTC](https://forum.kirupa.com/t/how-can-i-stop-a-user-from-inputting-a-number-as-a-decimal-in-an-integer-variable/329850/1 "2012-10-24T20:49:13Z")

</div>

hi there

i am very now to programming and have just got my first assignment.  
I give the user three lines of options with the choice of 1,2 or 3. i am now stress testing the program and if for instance 2.5 is entered into the “choice” variable the program goes haywire! any help would be much appreciated! here is the program

#include\<iostream\>  
#include\<iomanip\>

using namespace std;

int main()

{

```
int choice;
double totalrepay;
double eachrepay;
double interest;
double purchase;
char response;

do
{
cout&lt;&lt;" Hire Purchase calculation Programme 

```

“;  
cout\<\<” \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
";  
cout\<\<"1. Immediate Payment (10% reduction)  
";  
cout\<\<"2. Medium Term Payment (10% Increase, 12 Payments)  
";  
cout\<\<"3. Long term Payment (30% Increase, 36 Payments)  
“;  
cout\<\<” Please enter your number of your choice  
";  
cin\>\>choice;

```
            if (choice&lt;1 || choice&gt;3)
            do
            {
            cout&lt;&lt;"Your choice is invalid, please re-enter your choice 

```

";  
cin\>\>choice;  
}  
while (choice\<1 || choice\>3);

```
            cout&lt;&lt;"Please enter the Cost of your purchase. 

```

";  
cin\>\>purchase;

```
            if(choice==1)
            {
            totalrepay=0;
            eachrepay=0;
            interest=0;
            
            cout&lt;&lt;fixed&lt;&lt;setprecision(2)&lt;&lt;"The total cost of your repayments is €"&lt;&lt;totalrepay;
            cout&lt;&lt;endl;
            cout&lt;&lt;fixed&lt;&lt;setprecision(2)&lt;&lt;"The cost of each repayment is €"&lt;&lt;eachrepay;
            cout&lt;&lt;endl;
            cout&lt;&lt;fixed&lt;&lt;setprecision(2)&lt;&lt;"The cost of your interest is €"&lt;&lt;interest;
            cout&lt;&lt;endl;
            }
            
            else if(choice==2)
            {
            totalrepay=purchase*1.1;
            eachrepay=totalrepay/12;
            interest=purchase*0.1;
            
            cout&lt;&lt;fixed&lt;&lt;setprecision(2)&lt;&lt;"The total cost of your repayments is €"&lt;&lt;totalrepay;
            cout&lt;&lt;endl;
            cout&lt;&lt;fixed&lt;&lt;setprecision(2)&lt;&lt;"The cost of each repayment is €"&lt;&lt;eachrepay;
            cout&lt;&lt;endl;
            cout&lt;&lt;fixed&lt;&lt;setprecision(2)&lt;&lt;"The cost of your interest is €"&lt;&lt;interest;
            cout&lt;&lt;endl;
            }
            
            else if(choice==3)
            {
            totalrepay=purchase*1.3;
            eachrepay=totalrepay/36;
            interest=purchase*0.3;
            
            cout&lt;&lt;fixed&lt;&lt;setprecision(2)&lt;&lt;"The total cost of your repayments is €"&lt;&lt;totalrepay;
            cout&lt;&lt;endl;
            cout&lt;&lt;fixed&lt;&lt;setprecision(2)&lt;&lt;"The cost of each repayment is €"&lt;&lt;eachrepay;
            cout&lt;&lt;endl;
            cout&lt;&lt;fixed&lt;&lt;setprecision(2)&lt;&lt;"The cost of your interest is €"&lt;&lt;interest;
            cout&lt;&lt;endl;
            }
            
            else
            cout&lt;&lt;"I don't know how you got here 

```

"\<\<endl;

```
cout&lt;&lt;"Do you want another go? y/n 

```

";  
cin\>\>response;  
cout\<\<endl;  
}  
while (response==‘y’ || response==‘Y’);

```
system ("pause");
return 0;

```

}
