代码不工作?

问题描述:

我正在学习 C ++ 。作为一个家庭作业,我开始尝试分支..但我没有得到它的悬念...这里的代码我试图执行(请耐心与我,如果我犯了巨大的错误。 。)

I am learning C++. As a homework I've started to try the branching.. but I didn't quite get the hang of it... here's the code I've tried to perform (please bear patience with me if I'm making huge mistakes..)

#include <iostream>
using namespace std;

int main () {
    int age;
    char * yes;
    char * no;
    bool Rated = Rated ? yes : no;
    int ticketPrice = 5;
    int discountPrice = ticketPrice - (ticketPrice * 0.1);
    int matineePrice = (ticketPrice * 0.5);
    int hour = 8 <= hour <= 24;

    cout << "Is the movie R_rated? \n";

    cin >> yes or no;

    cout << "How old are you?";
    cin >> age;

    if (age < 0 or age >100) {
        cout << "Not a valid age!";
    }
    else if ((age <= 13) and (Rated = yes)) {
        cout << "You must be accompanied by a Janitor";
    }
    else if (((age > 13) and ((Rated = yes) or (Rated = no)))
    or ((age <=13) and (Rated = yes))) {
        cout << "What time do you want the ticket for?";
        cin >> hour;
        if (hour < 8 or hour > 24) {
            cout << "Not a valid hour!";
        }
        else if (hour < 18) {
            if (age <= 5) {
                cout << "The entrance is free";
            }
            else if (age >= 55) {
                cout << "Matinee Ticket price is "<<
                matineePrice;
            }
            else if (5 < age < 55) {
                cout << "Matinee ticket price is " << matineePrice;
            }
        }
        else if (hour >= 18) {
            if (age <= 5) {
                cout << "The entrance is free";
            }
            else if (5 < age <= 55) {
                cout << "Ticket price is " << ticketPrice;
            }
            else if (age > 55) {
                cout << "You're eligibile for a 10% "
                        "discount \n";
                cout << "Ticket price is " << discountPrice;
            }
        }
    }
}

strong>输出:(我回答 67 20 ),我应该得到

Output: (to which I answer no, 67, and 20) and I should get the discountedPrice instead of the ticketPrice value...

Is the movie R_rated? 
no
How old are you?67
What time do you want the ticket for?20
Ticket price is 5

任何建议,链接或教程帮助将非常感谢...

Any suggestion, link or tutorial help would be really appreciated...

首先,删除 yes no ,这没有意义,输入到字符串变量:

To start with, get rid of yes and no, which make no sense, and read the input into a string variable:

string Rated;
cin >> Rated;

然后使用它,记住使用 == = 用于比较:

then to use that, remember to use == not = for comparison:

if (Rated == "yes") {/*whatever*/}

或者,使用布尔变量:

string yes_or_no;
cin >> yes_or_no;
bool Rated = yes_or_no == "yes";

if (Rated)  {/*whatever*/}

这个:

8 <= hour <= 24

不会执行您认为的操作。您需要两次单独的比较:

doesn't do what you think it does. You'd need two separate comparisons:

8 <= hour and hour <= 24

虽然,在这种情况下,你根本不想要它 - 没有意义的初始化 hour 。您正在读取 hour 的值,稍后检查其范围,不需要在此处初始化。

although, in this case, you don't want it at all - it doesn't make sense to initialise hour with that. You're reading the value of hour and checking its range later, and don't need to initialise it here.

可能有更多的问题,但这应该让你开始。我希望当我超过100时,我还可以去看电影。

There are probably more problems, but that should get you started. And I hope I can still go to the cinema when I'm over 100.