本周的家庭作业分配是创建一个基本的c ++程序,要求用户输入以英尺和英寸为单位的长度,然后以厘米为单位输出; 我们要创建一个异常并让它处理它,如果用户输入一个负数或一个字符.我有代码写,但当它编译时,我得到错误:
在函数'int main()'中:在第19行的"int"之前的"int"期望')之前的期望的primary-expression.
这是我的代码:
#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;
}
Run Code Online (Sandbox Code Playgroud)
我认为这是一个简单的事情,我忽略了但我无法弄清楚我的问题是什么.任何帮助深表感谢.提前致谢.
在
if ( int feet, int inches < 0.0 )
Run Code Online (Sandbox Code Playgroud)
你可以隐式重新声明之前声明过的两个整数变量.你不应该这样做.相反,只需使用它们:
if (feet, inches < 0.0 )
Run Code Online (Sandbox Code Playgroud)
现在,这可能不是你的意思.你可能想说这样的话:
if (feet < 0.0 || inches < 0.0 )
Run Code Online (Sandbox Code Playgroud)