cout中的意外输出

时间:2014-06-16 00:06:51

标签: c++

所以,这是我关于Stack Overflow的第一个问题。在与MATLAB进行了一些艰苦的经历之后,我正在学习C ++。我做了一个简单的练习,但是没有产生预期的结果。

我也没有错。我正在运行Xcode 5。

我怀疑这个问题与我的变量初始化有关。当我查看调试时,我的变量保持设置为0.

#include <iostream>
using namespace std;

int main()
{

//Declare variables
int score = 0;

//input score
    cout << "Emter score: ";
    cin >> score;


if (score == 100)
{
    cout << "Final Score: " << score;
    cout << "You received a perfect score! \n";
    // 100% is an A.
}
else if ((score >= 90) && (score <= 100))
{
    cout << "Final Score: " << score;
    cout << "Your grade is an A. Nice Job! \n";
    // 90-100 is an A.
}
else if ((score >= 80) && (score <= 89))
{
    cout << "Final Score: " << score;
    cout << "Your grade is a B. Well done. \n";
    // 80-89 is a B.
}
else if ((score >= 70) && (score <= 79))
{
    cout << "Final Score: " << score;
    cout << "Your grade is a C. Really? \n";
    // 70-79 is a C.
}
else if ((score >= 60) && (score <= 69))
{
    cout << "Final Score: " <<score;
    cout << "Your grade is a D. You suck. Seriously. \n";
    // 60-69 is a D.
}
else if ((score >= 0) && (score <= 59))
{
    cout << "Final Score: " << score;
    cout << "You got an F! YOU FAIL! GO JUMP OUT THE WINDOW. \n";
    // 0-59 is an F.
}

return 0;
}

对不起,很长的帖子,我不想遗漏任何东西。再次感谢。

ETA:修正了换行符。我在线上重新输入代码,它运行得很好。我怀疑它与所有这些东西的投射方式有关,但我不确定。

2 个答案:

答案 0 :(得分:2)

欢迎来到SO和C ++!

这个问题可能都归结为一个简单的错字 - 你已经使用了换行符,你输入了一个前锋(而不是后退)斜线;正确的换行符是\n

实际上还有另一种方法可以输出换行符,如下所示:

cout << endl;

这是 I 建议的方法,至少现在是这样,而你没有理由选择其中一个。 Others disagree however, and would advocate the use of \ndifference between the twoendl执行刷新,而\n则没有(而/n 当然没有! ) - 至少不是标准的。

如果所有这些同花顺的声音听起来像我已经过时了 - 只需忽略它,坚持endl(除非你正在指导你使用\n的课程),并且毫无疑问,你很快就会遇到更多有关缓冲区和冲洗的问题!

假设你的“意外输出”是“所有内容都在同一条线上并且在任何地方都显示'/ n' - 这就是你需要做的所有修复(你可以继续删除那些'/ n')。


注意:/n vs \n的原因是\转义字符 - 也就是说,无论后面是什么,都被称为转义。因此,在换行符中,\nn被转义,我们在cout中看不到'n'。

意味着n并不重要!与\一起形成单个ASCII character将在以后开始操作char类型的变量时重要)以打印新行。相反,/ 是转义字符,因此当您使用/n时,您会看到显示/n

答案 1 :(得分:0)

我清理了一些代码并记下了一些可能有助于了解的常见做法。此外,正如之前的回答所提到的,问题的最可能原因是换行符。欢迎来到c ++的精彩世界!

包括

使用namespace std;

int main()
{

  //Declare variables
  double score = 0;          //this should really be a double if we are talking about grades

  //input score
  cout << "Enter score: ";
  cin >> score;

  cout << "Final Score: " << score << endl; //endl is the more common line ending that I've seen
                                            //    with my experience in C++
  cout << "Your grade is a";                //you can remove all commonalities between if
                                            //    statements to save yourself some typing
                                            //    This is also common practice

   if (score == 100)
   {
      cout << " perfect score!" << endl;
      // 100% is an A.
   }
   else if (score >= 90 && score < 100)      //we use < 100 instead of <= 99 because what about
                                             //    numbers between 99 and 100
   {
      cout << "n A. Nice Job!" << endl;
      // 90-100 is an A.
   }
   else if (score >= 80 && score < 90)
   {
      cout << " B. Well done." << endl;
      // 80-89 is a B.
   }
   else if (score >= 70 && score < 80)
   {
      cout << " C. Really?" << endl;
      // 70-79 is a C.
   }
   else if (score >= 60 && score < 70)
   {
      cout << " D. You suck. Seriously." << endl;
      // 60-69 is a D.
   }
   else                                               //if we are not handling errors (assuming user
                                                      //    enters 0 to 100), we can just say else
   {
    cout << "n F! YOU FAIL! GO JUMP OUT THE WINDOW." << endl;
    // 0-59 is an F.
   }

   return 0;
}