“预期的主要表达方式__”是什么意思?

时间:2017-09-16 05:01:40

标签: c++ if-statement

我正在尝试编写一个程序来计算在某个酒店租用房间的费用。该计划要求支付租金,房间预订的天数以及销售税。我应该实施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;
}

3 个答案:

答案 0 :(得分:4)

编译并启用警告(例如GCC中的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)

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

同样,chnage this:

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

到那个:

export class TestClass{
Add() {
      this.finalValues.push(new SelectedList(this.selectedItemType, this.txtEntered, this.date, this.chkBox));
    }
 HandleEdit(i) {
      this.selectedItemType = this.finalValues[i].attributeName;
      this.txtEntered = this.finalValues[i].value;
       this.date = this.finalValues[i].date;
       this.chkBox = this.finalValues[i].checked;
    }

    HandleRemove(i) {
      this.finalValues.splice(i, 1);
    }
}

我很确定你不想使用移位运算符,但与30比较。

之后你只会收到一个警告:

  

警告:变量&#39; threeTotal&#39;设置但未使用[-Wunused-but-set-variable]

这是不言自明的。

答案 1 :(得分:1)

您需要指定要比较的变量。 if(numRooms >= 11 && numRooms <= 20){ //do something } 在C ++中是非法的,编译器不知道哪个变量应该小于20.正确的语法是:

{{1}}

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

答案 2 :(得分:0)

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

相关问题