2017-09-16 11 views
0

私は、特定のホテルで部屋を借りるコストを計算するプログラムを作成しようとしています。プログラムは、賃貸料、部屋の予約日数、および消費税を尋ねます。私は部屋を借りた時間と部屋の賃貸料の額から割引を計算するためにif文を実装することになっています。 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; 
} 
+2

を使用して試すことができ、https://stackoverflow.com/questions/388242/the-definitive-([良い本を読みます] ^書籍ガイドとリスト) – StoryTeller

+0

^特に、表現が何であるかを読んでください。 –

答えて

4

有効に警告してコンパイル( - 私は割引が使用されるかのパラメータを設定しようとすると、私は45行で使用する各発現を介して様々な「__一次式を期待」というエラーを取得しておきます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) 

あなたが自律的に両方の式を指定する必要があるため。同様に

、このchnage:私はあなたがシフト演算子を使用したいのですが、30

と比較しないかなり確信しています

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

:それに

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

をその後、1つの警告のみが表示されます。

警告:変数 'thirtyTotal'は設定されていますが、使用されていません。[使用されている変数です]

これは自明です。

+1

''(1 == Reason) ''を書く(つまり、左に定数を、右にテストする変数で) ''(1 = Reason) ''を間違って書くことはできません。 – kmoser

+3

@kmoser 'if(1 == Reason)... 'のように定数を最初に置くと、多くの人に後ろ向きに感じます。コンパイラは通常、警告を無視しない場合には、ステイットドロジックを必要としないように、内部に条件(例えば、gccの '-Wraphesises')を割り当てると警告を出します。 – Caleb

+0

内部で条件を割り当てたい場合があり、そのような場合のコンパイラの警告は注意散漫です。あなたが「後方」と呼ぶものは、経験豊富なプログラマーは足に自分自身を撃つのを防ぐのに役立つと知っているので、「正常」と呼んでいます。 – kmoser

1

比較する変数を指定する必要があります。 (if numRooms >= 11 && <=20)はC++で違法であるコンパイラが20未満で適切な構文であると考えられるものを変数知らない、次のようになります。

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

このパターンは、あなたのif文のほとんどで繰り返されるので、それに応じて変更!

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

関連する問題