So I'm somewhat new to programming and I have a assignment in which I have to verify if the numerator entered by the user is a decimal, instead of a integer. If it is, then I throw an exception...the thing is I don't really know how to do it because as you all know, it doesn't matter if the variable is integer, if I input 3.49...it will be shortened to 3. So how can I know if it was decimal?

``````int main()
{

int numerator,denominator;

try
{
cout << "Input the numerator : " ;
cin >> numerator;
cout << "Input the denominator : " ;
cin >> denominator;
if (denominator == 0)
throw denominator;
else
cout << numerator << '/' << denominador
<< "=" << double(numerator)/double(denominator) << endl;

}

catch(int e)
{
cout << " The value of the denominator " << e << " is invalid. " <<endl;
system("PAUSE");

}
return EXIT_SUCCESS;
}``````
3
Contributors
9
Replies
10
Views
8 Years
Discussion Span
Last Post by chary8088

You could input a string instead of an int and then check if the string contains a '.'.

add a double type variable ldnumer , the variable receive a double type input
numerator 's value is transfered from ldnumer , if ldnumer is a integer, the ldnumer should be equal to numerator , else greater; see follow code:

``````int main()
{

int numerator,denominator;
double ldnumer = 0;

try
{
cout << "Input the numerator : " ;
cin >> ldnumer;
numerator = (int)ldnumer;//3.2->3 3.0->3
cout << "Input the denominator : " ;
cin >> denominator;
if (denominator == 0 && numerator < ldnumer)//3.0 == 3 3.2 > 3
throw denominator;
else
cout << numerator << '/' << denominador
<< "=" << double(numerator)/double(denominator) << endl;

}

catch(int e)
{
cout << " The value of the denominator " << e << " is invalid. " <<endl;
system("PAUSE");

}
return EXIT_SUCCESS;
}``````

but I want to be able to catch the exception if the user inputs a decimal number and display it on screen...that's the exercise.

Can't my code satisfy your requirement ???

your code is converting the decimal to integer. That's not the exercise. The exercise is to let the user do the input like decimal so that I can throw the error and catch it...like the denominator one.

ok...I just saw what you did...my bad...the problem is that the catch is integer...so that if you did would only work if both of them are wrong inputs.

EDIT: Well...thanks chary8088...i just modified it with what you gave me and it resulted:

``````int main()
{

int numerator,denominator;
double ldnumer = 0;

try
{
cout << "Input the numerator : " ;
cin >> ldnumer;
numerator = (int)ldnumer;//3.2->3 3.0->3
if (numerator < ldnumer)
throw numerator;
cout << "Input the denominator : " ;
cin >> denominator;
if (denominator == 0)//3.0 == 3 3.2 > 3
throw denominator;
else
cout << numerator << '/' << denominator << "=" << double(numerator)/double(denominator) << endl;

}
catch (double a)
{
cout << " The value of the numerator " << a << " is invalid. " << endl;
system("PAUSE");
}

catch(int e)
{
cout << " The value of the denominator " << e << " is invalid. " <<endl;
system("PAUSE");

}
return EXIT_SUCCESS;
}``````

Edited by J.Killa: problem solved

I have revised the ,and can run normal ,which throw the exception

``````int main()
{
int numerator,denominator;
double ldnumer = 0;
try
{
cout << "Input the numerator : " ;
cin >> ldnumer;
numerator = (int)ldnumer;//3.2->3 3.0->3
cout << "Input the denominator : " ;
cin >> denominator;
if (denominator == 0 || numerator == ldnumer)//!!!!! revised
throw denominator;
else
cout << numerator << '/' << denominator<< "=" << double(numerator)/double(denominator) << endl;
}
catch(int e)
{cout << " The value of the denominator " << e << " is invalid. " <<endl;
system("PAUSE"); }
return EXIT_SUCCESS;
}``````

Edited by chary8088: n/a

I have test it , you also run the code
the code been revised which satisfy your want

if the numerator entered by the user is a decimal, instead of a integer. If it is, then I throw an exception

That is if the numerator entered by the user is a decimal, then throw an exception
..

``````int main()
{
while(1)
{
int numerator,denominator;
double ldnumer = 0;
try
{
cout << "Input the numerator : " ;
cin >> ldnumer;
numerator = (int)ldnumer;//3.2->3 3.0->3
cout << "Input the denominator : " ;
cin >> denominator;
if(numerator < ldnumer)//3.0 == 3 3.2 > 3
throw ldnumer;
if (denominator == 0 )
throw denominator;
else
cout << numerator << '/' << denominator<< "=" << double(numerator)/double(denominator) << endl;
}
catch(double e)
{
cout << " The value of the denominator " << e << " is invalid. " <<endl;
system("PAUSE");
}
}
return EXIT_SUCCESS;
}``````
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.