I am very new to C++ and still studying. The below scenario just came to my mind and I was trying to figure it out how to do this.
Scenario is as below:-
- User inputs a number
- then I store it in
x
- next is to check whether the input number is an
int
orfloat
- if
int
, then pop up a message "Entered Number is not a Decimal Number" and go back to the beginning and inform the user to re-enter a number - if the entered number is
float
then I round it to the nearestint
and pop up a messagecout<<"Nearst Rounded Number is : "<<round(x)<<endl;
I assume this can be done with a loop, but I cannot figure it out.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float x,y;
cout<<"Enter Number Here : ";
cin>>x;
{
if ( (x- int(x) == 0))
cout<<"Entered Number is not a Decimal Number"<<endl<<endl;
else
cout<<endl;
cout<<"Nearst Rounded Number is : "<<round(x)<<endl;
}
}