本周的家庭作业是创建一个基本的 c++ 程序,要求用户输入以英尺和英寸为单位的长度,然后以厘米为单位输出;关键是我们要创建一个异常并让它处理用户输入负数或字符的情况。我已经编写了代码,但是当它编译时出现错误:
在函数 'int main()' 中:第 19 行的“int”之前的预期主表达式在“int”之前预期的 ')'。
这是我的代码:
#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;
}
我认为这是我忽略的简单事情,但我无法弄清楚我的问题是什么。任何帮助深表感谢。提前致谢。