0

除了 else 部分之外的所有内容都给了我一个奇怪的(预期的主要表达式在 else 之前)错误。IM认为这可能与逻辑的局限性有关。但我似乎无法解决这个错误。

#include <iostream>

using namespace std;

int main()
{
int numberRegistered = 0;
const double feePerOneThroughFour = 0.0;
const double feePerFiveThroughTen = 0.0;
const double feeElevenOrMore = 0.0;

double moneyOwed = 0.0;

cout << " Enter in the Number of Registered people";
cin >> numberRegistered;

if (numberRegistered <= 0 );
{
cout << " You have entered in either the number 0 or a negative number" ;
}
        else 
             if  (numberRegistered == 1 ) ||  (numberRegistered == 2) || (numberRegistered == 3 ) ||  (numberRegistered == 4) 
             moneyOwed = numberRegistered * feePerOneThroughFour;
             cout << " Money Owed : $ " ;
             cin >> moneyOwed ;

            else if  (numberRegistered == 5 ) ||  (numberRegistered == 6) || (numberRegistered == 7 ) ||  (numberRegistered == 8) || (numberRegistered == 9)  || (numberRegistered == 10)
            moneyOwed = numberRegistered * feePerFiveThroughTen;
            cout << " Money Owed : $ " ;
            cin >> moneyOwed ;

                    else if  (numberRegistered >= 11) 
                    moneyOwed = numberRegistered * feeElevenOrMore;
                    cout << " Money Owed : $ " ;
                    cin >> moneyOwed ;

system("pause");
    return 0;
}
4

4 回答 4

2

在你的末尾有一个分号if

if();
   ^^^^

此外,您{ }在条件语句后缺少大括号。


if  (numberRegistered == 1 ) ||  (numberRegistered == 2) || (numberRegistered == 3 ) ||  (numberRegistered == 4) 

您缺少括号:

if( (numberRegistered == 1 ) ||  (numberRegistered == 2) || (numberRegistered == 3 ) ||  (numberRegistered == 4) )
于 2013-02-24T07:56:24.540 回答
0

尝试为每个 if 和 else 使用括号

于 2013-02-24T07:59:50.183 回答
0

if (numberRegistered <= 0 ); 您有一个额外的分号,并在嵌套 if /else 时尝试使用 { } 以避免逻辑问题

于 2013-02-24T08:00:35.237 回答
0

正如其他人所指出的, - 语句末尾的if分号是一个常见的错误。你的意思是:

if (numberRegistered <= 0)
{
    // [...]
}

下一个else if区块还有另外几个问题。首先,您需要将整个表达式包装在括号中(这是您看到的编译器错误),然后将块包装在大括号中:

else if ((numberRegistered == 1) || (numberRegistered == 2) || (numberRegistered == 3 ) || (numberRegistered == 4))
{
    moneyOwed = numberRegistered * feePerOneThroughFour;
    cout << " Money Owed : $ " ;
    cin >> moneyOwed ;
}

您可以缩短它以提高可读性:

else if ((numberRegistered >= 1 ) && (numberRegistered <= 4))
{
    moneyOwed = numberRegistered * feePerOneThroughFour;
    cout << " Money Owed : $ " ;
    cin >> moneyOwed ;
}

你的其他else if块有同样的问题。

于 2013-02-24T08:05:28.957 回答