"expected primary expression __"是什么意思?

What does "expected primary expression __" mean?

本文关键字:意思 是什么 expression expected primary      更新时间:2023-10-16

我正在尝试编写一个程序来计算某个酒店租房的成本。该计划要求租金费用,要预订多少天的房间以及营业税。我应该实施如果语句按顺序计算基于房间的租金时间以及如何租用房间的折扣。当我尝试设置将使用哪种折扣的参数时,我会遇到一个错误,说"预期eartial表达式__"在我在第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;
}

启用警告(例如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)

我很确定您不想使用Shift Operator,而是与30。

进行比较。

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

警告:变量'thirtytotal'集,但不使用[ - wunused in-stet-nable]

是自称的。

您需要指定要比较的变量。(if numRooms >= 11 && <=20)在C 中是非法的,编译器不知道什么变量应小于20。正确的语法将是:

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

此模式在您的大多数IF语句中都重复,因此请相应地更改它们!

    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语句