【问题标题】:Code not working? [closed]代码不起作用? [关闭]
【发布时间】:2013-09-05 13:24:52
【问题描述】:

我正在学习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;
            }
        }
    }
}

输出:(我回答 no、67 和 20),我应该得到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

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

【问题讨论】:

  • 欢迎来到本站。发布时,请确保您的代码格式正确;特别要突出显示它并使用{} 按钮将其标记为代码,否则将丢失正确的格式。而且,请确保您的缩进正确对齐,并且没有不必要的空行。
  • cin &gt;&gt; yes or no; 还能编译吗?
  • 这段代码有很多错误,甚至很难选择从哪里开始讲述......
  • 这段代码有太多错误。为什么不从更简单的事情开始,或者将问题分解成更小的部分,然后以易于测试的方式独立解决每个部分。
  • @trojanfoe:是的;它相当于(cin &gt;&gt; yes) || no。它应该给出一个警告,因为no 没有做任何事情并且它的值被忽略了。

标签: c++


【解决方案1】:

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

不过,这里有一些事情:

char* 不适合用于字符串。您应该使用std::string 类。

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

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

【讨论】:

  • 另外,不能输入我知道的“或”....尽管 C 和 C++ 世界非常奇怪。 ||不过,确实有效。
  • @RickyMutschlechner or 和 and 分别是 || 和 &amp;&amp; 的完美同义词。只是它们几乎从未使用过,但这并没有使它们出错(只是令人困惑)。
  • @RickyMutschlechner: and、or 和其他一些包含在标准中,与 有向图 属于同一类别。
  • 但是 Microsoft 的编译器不将它们识别为关键字 - 它需要包含宏定义的标头。需要记住的一点。
【解决方案2】:

首先,去掉毫无意义的yes和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多岁时还能去电影院。

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      开始:

      char * yes;
      char * no;
      
      // ...
      
      cin >> yes or no;
      

      使编码毫无意义。

      【讨论】:

      • 你打算如何解决这个问题?
      • or 的优先级低于&gt;&gt;,因此它应该可以正常编译。
      • @CharlesBailey 仍然没有意义。
      【解决方案5】:

      一般来说,“yes”和“no”在c++中不是关键字。 “真”和“假”是。

      好的,有些事情: 1) cin >> 是或否;

      应该是:

      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;
      

      【讨论】:

      • 编译器应该能够弄清楚如何处理or no,即使丢弃or 表达式的结果可能会引发警告。
      • bool Rated = Rated ? yes : no 当然很傻,但它可以编译。事实上,整个“坏”代码都会编译。提问者可能已经非常努力地让它编译。该代码可能是无意义的代码可以编译得很好然后离工作很远的光辉例子:)
      【解决方案6】:

      行:

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

      和所有的

      (Rated = yes)
      (Rated = no)
      

      没有意义。

      首先,如果Rated 是bool,那么您只能分配给它true 或false(不是0 或NULL 的所有内容以及编译器接受的内容都将转换为@ 987654329@)。

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

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

      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)
      

      【讨论】:

        【解决方案7】:

        对于折扣票使用: 双倍折扣价格 = 0,9*ticketPrice;

        您将输入小时并对其进行测试,因此您只能编写: 整数小时;

        【讨论】:

          【解决方案8】:

          试试这个

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

          您的代码

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

          正确的方法

          这将为您提供折扣价

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

          【讨论】:

            猜你喜欢
            • 2018-04-01
            • 2015-03-04
            • 2014-02-28
            • 2015-12-24
            • 2013-10-11
            • 1970-01-01
            • 2014-01-05
            • 2014-12-05
            • 2016-03-23
            相关资源
            最近更新 更多