简体   繁体   English

C ++期望在“int”之前的primary-expression

[英]C++ expected primary-expression before “int”

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; 本周的家庭作业分配是创建一个基本的c ++程序,要求用户输入以英尺和英寸为单位的长度,然后以厘米为单位输出; 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. 在函数'int main()'中:在第19行的“int”之前的“int”期望')之前的期望的primary-expression。

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 )

You need to cut the words int inside of the if statement beginning your try-block. 你需要在if语句中剪切int ,从你的try-block开始。

And you need to use a || 你需要使用|| instead of a comma. 而不是逗号。 ie feet < 0.0 && inches < 0.0 feet < 0.0 && inches < 0.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM