1

I'm attempting to write a program that calculates the cost of renting rooms at a certain hotel. The program asks for the cost to rent, how many days the rooms are going to be booked, and the sales tax. I'm supposed to implement if statements in order calculate the discount given based off of the amount of time the rooms are rented and how rooms will be rented. When i try to set the parameters of what discount will be used, i keep getting an error saying "expected primary expression __" varying through each expression i use at lines 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;
}
Caleb
  • 124,013
  • 19
  • 183
  • 272
o9ka
  • 19
  • 3

3 Answers3

4

Compile with warnings enabled (e.g. -Wall in GCC), and you will get:

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
                                ^~

Let's fix these issues:

Change this: if (Reason = 1) to this if (Reason == 1), since you want to compare, not assign.

Writing ( 1 == Reason ) (i.e. with the constant on the left and the variable to test on the right) will all but guarantee you can't ever mistakenly write ( 1 = Reason ). But on the other hand, it feels backward to many people. Personally I rely on warnings.

Change this:

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

to this:

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

since you need to specify both expressions autonomously.

Similarly, chnage this:

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

to that:

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

I am pretty sure you don't want to use the shift operator, but compare with 30.

After that you should receive only one warning:

warning: variable 'thirtyTotal' set but not used [-Wunused-but-set-variable]

which is self-explanatory.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    Writing ``( 1 == Reason )`` (i.e. with the constant on the left and the variable to test on the right) will all but guarantee you can't ever mistakenly write ``( 1 = Reason )``. – kmoser Sep 16 '17 at 05:12
  • 3
    @kmoser Putting the constant first as in `if (1 == Reason) ...` feels backward to many people. Compilers typically issue a warning if you try to assign inside a condition (e.g. `-Wparentheses` in gcc), so you don't need the stilted logic if you don't ignore warnings. – Caleb Sep 16 '17 at 05:37
  • There are times when you want to assign inside a condition, and compiler warnings in those cases are a distraction. What you call "backward", experienced programmers call "normal" because they know it will help prevent shooting themselves in the foot. – kmoser Sep 16 '17 at 05:45
  • 1
    @kmoser I highly disagree. Being able to understand and understanding in a split second is different and important. The need to assign in if statements has been addressed with [initializer if statements in C++17](https://stackoverflow.com/a/45999104/4832499). Worse comes to worse, additional parentheses will shut the compiler up. – Passer By Sep 16 '17 at 07:03
1

You need to specify the variable you are comparing against. (if numRooms >= 11 && <=20) is illegal in C++, the compiler doesn't know what variable is supposed to be less than 20. Proper syntax would be:

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

This pattern repeats in most of your if statements, so change them accordingly!

alejandrogiron
  • 544
  • 2
  • 7
  • 18
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;
    }

Change the condition statements here.

Also you can try using if else statements instead of continuous if statements

Nerdy
  • 1,016
  • 2
  • 11
  • 27