3

今週の宿題は、ユーザーに長さをフィートとインチで入力し、センチメートルで出力するよう求める基本的な C++ プログラムを作成することです。キャッチは、例外を作成し、ユーザーが負の数または文字を入力した場合にそれを処理することです。コードを作成しましたが、コンパイルするとエラーが発生します。

関数 'int main()' 内: 19 行目の "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;
}

見落としているのは簡単なことだと思いますが、自分の問題が何であるかわかりません。どんな助けでも大歓迎です。前もって感謝します。

4

2 に答える 2

8

if ( int feet, int inches < 0.0 )

以前に宣言した2つの整数変数を暗黙的に再宣言します。あなたはそれをすべきではありません。代わりに、単にそれらを使用してください:

if (feet, inches < 0.0 )

それでも、これはおそらくあなたが意図したことではありません。あなたはおそらくこのようなことを言うつもりでした:

if (feet < 0.0 || inches < 0.0 )
于 2012-08-17T03:54:03.430 に答える
0

inttry-blockを開始するifステートメント内の単語をカットする必要があります。

||また、コンマの代わりに使用する必要があります。すなわちfeet < 0.0 && inches < 0.0

于 2012-08-17T03:54:58.157 に答える