-4

我正在学习C++。作为一项家庭作业,我已经开始尝试分支......但我并没有完全掌握它......这是我尝试执行的代码(如果我犯了巨大的错误,请耐心等待。 .)

#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;
            }
        }
    }
}

输出:(我回答no6720)我应该得到discountedPrice而不是ticketPrice值......

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

任何建议、链接或教程帮助将不胜感激......

4

8 回答 8

3

你的代码有很多问题。我建议你买一本关于 C++ 的好书并从中学习。如果您已经在使用一本书,那么它很可能并不好。

不过,这里有一些事情:

char*不是用于字符串的正确方法。你应该使用这个std::string类。

您周围的整个代码Rated与 C++ 几乎没有相似之处。

=是赋值运算符。它不能用于比较事物是否相等;这就是==目的。

于 2013-09-05T13:31:25.733 回答
2

首先,去掉没有意义的yesand no,然后将输入读入一个字符串变量:

string Rated;
cin >> Rated;

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

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

或者,使用布尔变量:

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

if (Rated)  {/*whatever*/}

另外,这个:

8 <= hour <= 24

不做你认为它做的事。你需要两个单独的比较:

8 <= hour and hour <= 24

虽然,在这种情况下,你根本不想要它——用它来初始化是没有意义的hour。您hour稍后会读取 的值并检查其范围,并且不需要在此处对其进行初始化。

可能还有更多问题,但这应该可以帮助您入门。我希望我100多岁时还能去电影院。

于 2013-09-05T13:39:49.797 回答
2

下面的代码是固定的。我试图准确地解释代码的作用。

// Declare the input/output streams, including standard streams like std::cin and std::cout.
#include <iostream>
// Declare the std::string class - it's C++, we should not use C strings!
#include <string>

// Instead of using the entire std namespace, we'll only use the things that come up often.
// This saves some typing, but is safe. Otherwise, who knows what name may clash with something
// in the vast std namespace.
using std::cin;
using std::cout;
using std::endl;

int main() {
    bool ok; // Whether the most recent answer to a question is valid
    bool rated; // Whether the movie is R-rated
    int age; // Customer's age
    int hour; // Hour of the showing
    const int ticketPrice = 5;
    const int discountPrice = ticketPrice * (1.0 - 0.9);
    const int matineePrice = ticketPrice * 0.5;

    // Gather Inputs

    do {
        std::string answer; // Holds the answer to the yes/no question
        cout << "Is the movie R-rated (y/n)? ";
        cin >> answer;
        if (answer.length() > 0) {
            // If the answer is not empty, we can uppercase the first letter.
            // This way we don't have to check for lowercase answers.
            answer[0] = toupper(answer[0]);
        }
        // The answer is valid when it's non-empty and when it begins with either Y/y or N/n
        ok = answer.length() > 0 and (answer[0] == 'Y' or answer [0] == 'N');
        if (not ok) {
            cout << "That's not a valid answer." << endl;
        } else {
            // The answer is valid, so we can set the rated variable.
            rated = answer[0] == 'Y';
        }
    } while (not ok); // Repeat the question while the answer is invalid

    do {
        cout << "How old are you? ";
        cin >> age;
        // The answer is valid when it's between 0 and 150, inclusive.
        ok = age >= 0 and age <= 150;
        if (not ok) {
            cout << "That's not a valid age!" << endl;
        }
    } while (not ok);

    do {
        cout << "What hour do you want the ticket for? ";
        cin >> hour;
        // The hour 0 is mapped to 24.
        if (hour == 0) hour = 24;
        // The answer is valid when it's between 8 and 24, inclusive.
        ok = hour >= 8 and hour <= 24;
        if (not ok) {
            cout << "That's not a valid hour!";
        }
    } while (not ok);

    // Output the Messages

    if (rated and age <= 13) {
        cout << "You must be accompanied by a Janitor" << endl;
    }
    if (age <= 5) {
        cout << "The entrance is free" << endl;
    }
    else if (hour < 18) {
        cout << "Matinee ticket price is " << matineePrice << endl;
    }
    else {
        if (age <= 55) {
            cout << "Ticket price is " << ticketPrice << endl;
        }
        else {
            cout << "You're eligibile for a 10% discount." << endl;
            cout << "Ticket price is " << discountPrice << endl;
        }
    }
}
于 2013-09-05T14:05:10.303 回答
0

开始:

char * yes;
char * no;

// ...

cin >> yes or no;

没有任何编码意义。

于 2013-09-05T13:29:14.460 回答
0

通常,“yes”和“no”不是 c++ 中的关键字。“真”和“假”是。

好吧,有些事情:1)cin >> yes or no;

应该:

cin >> Rated;

因为变量名中不能有空格,正如您编写的那样,编译器会读取“cin 应该在 yes 中输入一些内容,但我不知道如何处理 'or' 和 'no'。”

2)

else if ((age <= 13) and (Rated = yes)) 

永远不会工作。我建议重写将结果存储在一个字符串(std::string)中,然后根据它设置评级。

std::string rated_str;
cin >>rated_str;
if(rated_str == "yes") {
    rated = true;
} else {
    rated = false;
}

然后在如果你使用:

if(rated)

或如果(额定 == 真)

3)在完全声明之前,您不能引用变量:

bool Rated = Rated ? yes : no;
于 2013-09-05T13:33:16.940 回答
0

线路:

bool Rated = Rated ? yes : no;
cin >> yes or no;

和所有

(Rated = yes)
(Rated = no)

没有意义。

首先,如果Rated是,bool那么您可以只分配给它truefalse(所有不是0或的东西,NULL以及编译器接受的东西都将被转换为true)。

最佳质量代码应使用枚举类型进行评级。

当你阅读时,你应该阅读一些变量并检查它的类型。为您提供的小修复:

enum EnRated
{
    RRated = 0,
    NotRRated = 1
};
...
enum EnRated Rated;
string ans; //instead of yes, no
...
cin >> ans;
if(ans == "yes") Rated = RRated;
else Rated = NotRRated; //handle ans != yes or no
...
(Rated == RRated) // instead of (Rated = yes)
(Rated == NotRRated) // instead of (Rated = no)
于 2013-09-05T13:38:56.573 回答
0

对于折扣票使用:double discountPrice = 0,9*ticketPrice;

您将输入小时并测试它们,因此您只能编写:int hour;

于 2013-09-05T13:39:47.750 回答
0

试试这个

  • 您在以下else if陈述中犯了一个错误。
  • 您没有else if正确使用您的条件检查器。
  • 你不需要or在这里使用关键字......实际上这是你在这里面临的真正问题,你只需要将你的替换orand.
  • 要获得完美的解决方案,请阅读下面 的正确方法

你的代码

    else if (5 < age <= 55) {
            cout << "Ticket price is " << ticketPrice;

        }

正确的方法

这将为您提供折扣价

else if (5 < age and age < 55)
{
    cout << "Ticket price is " << ticketPrice;
}
于 2013-09-05T13:55:25.627 回答