Homework assignment for the week is to create a basic c++ program asking a user to input a length in feet and inches and then output them in centimeters; the catch is we are to create an exception and have it handle it for if the user enters a negative number or a character. I have the code written but when it compiles i get the error:
In function 'int main()': expected primary-expression before "int" expected ')' before "int" on line 19.
Here's my code:
#include <iostream>
using namespace std;
const double centimetersPerInch = 2.54; //named constant
const int inchesPerFoot = 12; //named constant
int main ()
{
int feet;
int inches; //declared variables
int totalInches;
double centimeters;
//statements
cout << "Enter two integers one for feet and " << "one for inches: ";
cin >> feet >> inches;
try
{
if ( int feet, int inches < 0.0 )
throw "Please provide a positive number";
cout << endl;
cout << "The numbers you entered are " << feet << " for feet and " << inches << " for inches. " << endl;
totalInches = inchesPerFoot * feet + inches;
cout << "The total number of inches = " << totalInches << endl;
centimeters = centimetersPerInch * totalInches;
cout << "The number of centimeters = " << centimeters << endl;
}
catch (char* strException)
{
cerr << "Error: " << strException << endl;
}
return 0;
}
I figure it is something simple that I am overlooking but I just can't figure out what my issue is. Any help is much appreciated. Thanks in advance.
In
if ( int feet, int inches < 0.0 )
you sort-of implicitly re-declare two integer variables you have declared before. You shouldn't do that. Instead, simply use them:
if (feet, inches < 0.0 )
Now still, this is probably not what you meant. You probably meant to say something like this:
if (feet < 0.0 || inches < 0.0 )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With