【问题标题】:What is wrong my C++ code about the 'if and if else' function and the 'pause'?关于“if and if else”函数和“暂停”的 C++ 代码有什么问题?
【发布时间】:2015-12-15 14:10:27
【问题描述】:
#include <iostream>
#include <string>
#include <sstream>
#include <unistd.h>
using namespace std;

int main()
{   
    int score;
    string name, sco;
    cout<<"Hello! I am Mrs. Marian! I am the administratress of this humble website!"<<'\n';
    cout<<"This website is kindly brought to you by our Prime Minister Candry Veneroth."<<'\n';
    cout<<"This is the 'National School Grading System', or the NSGS, to help you see if your student fails or not."<<'\n';
    cout<<"The first thing you have to do is type the student's name: "<<'\n';
    getline(cin,name);
    cout<<"Great! Now please enter "<<name<<"'s correct answers in the National Education Test: ";
    getline(cin,sco);
    stringstream(sco)>>score;
    cout<<"The NET (National Education Test) has a total score of 180 with 150 questions with each question having a score of 1.2"<<'\n';
    cout<<"Loading data"<<'\n';

    if (score*1.2<90, score*1.2>=0)
    {
        cout<<"The student "<<name<<" has NOT PASSED, with a score of "<<score*1.2<<". I am sorry, and hope the best for next time.";
    }
    else if (score*1.2<180, score*1.2>=90)
    {
        cout<<"CONGRATULATIONS!"<<'\n'<<"The student "<<name<<" has PASSED, with a score of "<<score*1.2<<". Once again, congratulations!";
    }
    else if (score*1.2==180)
    {
        cout<<"CONGRATULATIONS!!!"<<'\n'<<"The student "<<name<<" has PASSED with a perfect score of "<<score*1.2<<". You will have a bright future ahead of you, "<<name<<". And I hope you for a bright future";
    }
    else if (score*1.2>180, score*1.2<0)
    {
        cout<<"This is invalid, and entering an invalid score in the NSGS is considered an act of malice against the students of Lantreind and is illegal according to the National Education Test Act (NETA) verse 5:";
        cout<<'\n'<<"'For thee who dare to destroy a student's scores, shall be convicted in the name of the Veneroth name.'"<<'\n'<<"You have been warned.";
    }
}

好的,我!我是新手!练习'if else'功能..我希望能够这样做:

  1. 如果分数为 0-89,则不及格。
  2. 如果分数为 90-179,则通过。
  3. 如果分数是180,他们以满分通过!耶!
  4. 如果分数低于 0 或高于 180,我希望程序告诉他们做对。

我一直在修改代码,起初,第一个和第二个'if and if else(s)'没有'逗号后的代码'。所以,即使分数是 180 或 -897 或 1131,它仍然会说它们(PASS/NOT PASS),完全忽略了第 3 和第 4 条“if else(s)”。

哦!而且,我想“暂停(1)”,因为我为什么放 #include 很明显,但它不起作用!消息是 =

错误:函数“int pause ()”的参数太多

我真的不知道如何解决这个问题,我希望任何人都可以帮助我找出问题所在以及如何解决它并使其按照应有的方式运行..

谢谢!

真诚地, 紧缩口香糖

【问题讨论】:

  • score*1.2&lt;90, score*1.2&gt;=0 等价于score*1.2&gt;=0,因为逗号运算符的左操作数将被丢弃。还要注意浮点运算中的错误。
  • The Definitive C++ Book Guide and List 中挑选一本或多本书籍,然后阅读。
  • 尝试使用score*1.2 &gt; 0 &amp;&amp; score*1.2 &lt; 90
  • 编辑 if 语句从 " , " 到 " && " 将完成上述@enrm 提到的工作
  • 在最后一个 if 语句中,您必须将“,”更改为“||”因为在这种情况下,您只需要其中一个条件。

标签: c++ if-statement pause


【解决方案1】:
  • 如果您想让结果只有在两个条件都为真时才为真,请使用&amp;&amp; 运算符。
  • 您不需要最后一个条件,因为所有“分数”都未被先前的条件捕获。
  • 您应该使用变量以避免多次执行相同的计算。
  • 小心浮点运算中的错误。我认为您应该尽可能避免使用浮点值。

可能的修复:

#include <iostream>
#include <string>
#include <sstream>
#include <unistd.h>
using namespace std;

int main()
{

    int score;
    string name, sco;
    cout<<"Hello! I am Mrs. Marian! I am the administratress of this humble website!"<<'\n';
    cout<<"This website is kindly brought to you by our Prime Minister Candry Veneroth."<<'\n';
    cout<<"This is the 'National School Grading System', or the NSGS, to help you see if your student fails or not."<<'\n';
    cout<<"The first thing you have to do is type the student's name: "<<'\n';
    getline(cin,name);
    cout<<"Great! Now please enter "<<name<<"'s correct answers in the National Education Test: ";
    getline(cin,sco);
    stringstream(sco)>>score;
    cout<<"The NET (National Education Test) has a total score of 180 with 150 questions with each question having a score of 1.2"<<'\n';
    cout<<"Loading data"<<'\n';

    int mscore = score * 12;

    if (mscore<900 && mscore>=0)
        {
            cout<<"The student "<<name<<" has NOT PASSED, with a score of "<<score*1.2<<". I am sorry, and hope the best for next time.";
        }
    else if (mscore<1800 && mscore>=900)
        {
            cout<<"CONGRATULATIONS!"<<'\n'<<"The student "<<name<<" has PASSED, with a score of "<<score*1.2<<". Once again, congratulations!";
        }
    else if (mscore==1800)
        {
            cout<<"CONGRATULATIONS!!!"<<'\n'<<"The student "<<name<<" has PASSED with a perfect score of "<<score*1.2<<". You will have a bright future ahead of you, "<<name<<". And I hope you for a bright future";
        }
    else
        {
            cout<<"This is invalid, and entering an invalid score in the NSGS is considered an act of malice against the students of Lantreind and is illegal according to the National Education Test Act (NETA) verse 5:";
            cout<<'\n'<<"'For thee who dare to destroy a student's scores, shall be convicted in the name of the Veneroth name.'"<<'\n'<<"You have been warned.";
        }
}

【讨论】:

    【解决方案2】:

    更改:if (score*1.2&lt;90, score*1.2&gt;=0)
    使用:if (score*1.2&lt;90 &amp;&amp; score*1.2&gt;=0)
    既然你想同时满足这两个条件

    更改:else if (score*1.2&lt;180, score*1.2&gt;=90)
    使用:else if (score*1.2&lt;180 &amp;&amp; score*1.2&gt;=90)
    既然你想同时满足这两个条件

    更改:else if (score*1.2&gt;180, score*1.2&lt;0)
    使用:else if (score*1.2&gt;180 || score*1.2&lt;0)
    既然你想满足这两个条件之一

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      • 2019-03-18
      • 1970-01-01
      • 2023-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多