【问题标题】:What does "expected primary expression __" mean?“预期的主要表达__”是什么意思?
【发布时间】:2017-09-16 05:01:39
【问题描述】:

我正在尝试编写一个程序来计算在某家酒店租用房间的成本。该程序会询问租金、房间的预订天数以及销售税。我应该执行 if 语句,以便根据房间出租的时间和房间的出租方式计算给出的折扣。当我尝试设置将使用什么折扣的参数时,我不断收到一条错误消息,说“预期的主要表达式 __”随着我在第 45 - 51 行使用的每个表达式而变化。

#include <iostream>

using namespace std;

int main()
{
    int Reason;
    double Rent, totRent;
    double SalesTax, calcST;
    double TimeDiscount, totDiscount;
    int numRooms;
    int RentTime;
    int tenRooms, twentyRooms, thirtyRooms;
    int tenTotal, twentyTotal, thirtyTotal, RegularPricing;
    cout << "How much is the cost of renting a room: " << endl;
    cin >> Rent;
    cout << "Are you staying for a special reason i.e. wedding/conference: " << endl;
    cin >> Reason;
    cout << "How many days are the rooms going to be booked " << endl;
    cin >> RentTime;
    cout << "What is the sales tax: " << endl;
    cin >> SalesTax;

    SalesTax = Rent * SalesTax;
    calcST = Rent + SalesTax;
    totRent = (Rent * numRooms) + RentTime;

    if (Reason = 1)
    {
        cout << "How many are rooms going to be booked: " << endl;
        cin >> numRooms;
    }
    if (RentTime >= 3)
    {
        TimeDiscount = .05 * Rent;
        totDiscount = Rent + TimeDiscount;

    }
    if (numRooms >= 10)
    {
        tenRooms = Rent * .10;
        tenTotal = (totRent + calcST) - (tenRooms + totDiscount);
        cout << "Your total fee is: $" << tenTotal << endl;
    }
    if (numRooms >= 11 && <= 20) //45
    {
        twentyRooms = Rent * .20 * SalesTax * TimeDiscount;
        twentyTotal = (totRent + calcST) - (twentyRooms + totDiscount);
        cout << "Your total fee is: $" << twentyTotal << endl;
    }
    if (numRooms >= 21 && >= 30 && >> 30) //51
    {
        thirtyRooms = Rent * .30 + SalesTax + TimeDiscount;
        thirtyTotal = (totRent + calcST) - (thirtyRooms + totDiscount);
        cout << "Your total fee is: $" << thirtyRooms << endl;
    }

    else
    {
        RegularPricing = Rent * RentTime + SalesTax;
        cout << "Your Total Fee is: $" << RegularPricing << endl;
    }

    cout << "The cost of renting one room is: $" << Rent << endl;
    cout << "Number of rooms booked : " << numRooms << endl;
    cout << "Days booked: " << RentTime << endl;
    cout << "The sales tax is: $" << calcST << endl;


    return 0;
}

【问题讨论】:

  • 不要猜测语法,read a good book
  • ^尤其是,了解什么是表达式。

标签: c++ if-statement


【解决方案1】:

在启用警告的情况下编译(例如 GCC 中的 -Wall),您将获得:

prog.cc: In function 'int main()':
prog.cc:28:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
 if (Reason = 1)
     ~~~~~~~^~~
prog.cc:45:23: error: expected primary-expression before '<=' token
 if (numRooms >= 11 && <= 20) //45
                       ^~
prog.cc:51:23: error: expected primary-expression before '>=' token
 if (numRooms >= 21 && >= 30 && >> 30) //51
                       ^~
prog.cc:51:32: error: expected primary-expression before '>>' token
 if (numRooms >= 21 && >= 30 && >> 30) //51
                                ^~

让我们解决这些问题:

将这个:if (Reason = 1) 更改为这个if (Reason == 1),因为你想比较,而不是分配。

( 1 == Reason )(即左边是常数,右边是要测试的变量)几乎可以保证你永远不会错误地写( 1 = Reason )。但另一方面,对很多人来说,这感觉很落后。我个人依赖警告。

改变这个:

if (numRooms >= 11 && <= 20)

到这里:

if (numRooms >= 11 && numRooms  <= 20)

因为您需要自主指定这两个表达式。

同样,改变这个:

if (numRooms >= 21 && >= 30 && >> 30)

到那个:

if (numRooms >= 21 && numRooms >= 30 && numRooms > 30)

我很确定您不想使用移位运算符,而是与 30 进行比较。

之后您应该只会收到一个警告:

警告:变量 'thirtyTotal' 已设置但未使用 [-Wunused-but-set-variable]

这是不言自明的。

【讨论】:

  • ( 1 == Reason )(即左边是常数,右边是要测试的变量)几乎可以保证你永远不会错误地写( 1 = Reason )
  • @kmoser 在if (1 == Reason) ... 中将常量放在首位对许多人来说感觉很落后。如果您尝试在条件内赋值(例如 gcc 中的 -Wparentheses),编译器通常会发出警告,因此如果您不忽略警告,则不需要高跷逻辑。
  • 有时您想在条件内进行赋值,在这种情况下编译器警告会分散注意力。你所说的“落后”,有经验的程序员称其为“正常”,因为他们知道这将有助于防止在脚下开枪。
  • @kmoser 我非常不同意。能够在一瞬间理解和理解是不同的和重要的。使用initializer if statements in C++17 解决了在 if 语句中分配的需要。更糟的是,额外的括号会使编译器关闭。
【解决方案2】:

您需要指定要比较的变量。 (if numRooms &gt;= 11 &amp;&amp; &lt;=20) 在 C++ 中是非法的,编译器不知道哪个变量应该小于 20。正确的语法是:

if(numRooms >= 11 && numRooms <= 20){
    //do something
}

这种模式在您的大多数 if 语句中重复出现,因此请相应地更改它们!

【讨论】:

    【解决方案3】:
        if (Reason == 1)//Reason=1 is an assignment statement.It is always true, irrespective of value of "Reason". == is used for comparison. 
        {
            cout << "How many are rooms going to be booked: " << endl;
            cin >> numRooms;
        }
        if (numRooms >= 11 && numRooms <= 20) //45--Add the variable to be compared with 
        {
            twentyRooms = Rent * .20 * SalesTax * TimeDiscount;
            twentyTotal = (totRent + calcST) - (twentyRooms + totDiscount);
            cout << "Your total fee is: $" << twentyTotal << endl;
        }
        if (numRooms >= 21 && numRooms >= 30) //51 Also >> is right shift. > 30 condition is already checked in >=30. So this can be avoided according to my perception of your code.
        {
            thirtyRooms = Rent * .30 + SalesTax + TimeDiscount;
            thirtyTotal = (totRent + calcST) - (thirtyRooms + totDiscount);
            cout << "Your total fee is: $" << thirtyRooms << endl;
        }
    

    在此处更改条件语句。

    您也可以尝试使用if else 语句而不是连续的if 语句

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-04
      • 2020-10-23
      • 2022-01-12
      相关资源
      最近更新 更多