2

So, this is my very first questions on Stack Overflow. I'm in the process of learning C++ after some hard-fought experience with MATLAB. I've got a simple exercise that builds fine, but does not produce the expected result.

I get no errors, either. I'm running Xcode 5.

I suspect the problem has something to do with the initialization of my variables. When I look into debugging, my variables stay set to 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;
}

Sorry for the long post, I did not want to leave anything out. Thanks again.

ETA: Fixed the newline characters. I retyped the code in line for line and it ran just fine. I suspect it had something to do with the way all this stuff was being cast, but I'm not sure.

Samson
  • 21
  • 2
  • 3
    "does not produce the expected result." - please describe what your input was, what results you received and how this differs from what you expected – M.M Jun 16 '14 at 00:09
  • 3
    Why have you got "/n" instead of "\n" or more correctly "<< endl"? – hookenz Jun 16 '14 at 00:12

2 Answers2

2

Welcome to SO, and to C++!

This issue may all come down to a simple typo - where you've used the newline character, you've typed a forward (instead of back-) slash; the correct newline character is \n.

There is actually another method for outputting the newline character as follows:

cout << endl;

which is the method I would recommend, at least for now, while you have no reason to choose one over the other. Others disagree however, and would advocate the use of \n. The difference between the two is that endl performs a flush, while \n does not (and /n certainly does not!) - at least not as standard.

If all this flush talk sounds like I've gone potty - just ignore it, stick to endl (unless you're on a course where your instructor has specified to use \n), and you'll no doubt encounter more about buffers and flushing soon!

Assuming your "unexpected output" was "everything is on the same line and it says '/n' everywhere" - this is all you need do to fix (you can go ahead and remove those '/n's).


NB: The reason for /n vs \n is that \ is the escape character - that is, whatever follows it is said to be escaped. Thus in the newline character, \n, n is escaped, and we do not see an 'n' displayed in cout.

But that does not mean that the n is not important! Together with \ it forms a single ASCII character (single will be important later, when you begin manipulating variables of the char type) to print a new line. Conversely, / is not the escape character, so when you used /n you saw both / and n displayed.

Community
  • 1
  • 1
OJFord
  • 10,522
  • 8
  • 64
  • 98
  • 1
    My preferred method for outputting a newline is `cout << '\n'`. It's a waste of time to do an explicit flush when it's not needed. – M.M Jun 16 '14 at 00:43
  • @MattMcNabb For someone new to C++, I'd personally recommend letting `std::endl` do its thing. Lack of flushes caused me enough headaches when I started out, and the erroneous output can seem truly bizarre when you haven't learnt about streams and buffers yet. – OJFord Jun 16 '14 at 00:47
  • Some people beg to differ: http://stackoverflow.com/questions/8311058/n-or-n-or-stdendl-to-stdcout – PlasmaHH Jun 16 '14 at 12:31
  • @PlasmaHH Interesting, noted. I'll update my answer to be less biased toward `std::endl` when I'm off mobile. – OJFord Jun 16 '14 at 15:53
0

I cleaned up your code some and made note of some common practices that may be helpful to know. Also, as the previous answer mentioned the most likely cause of your problem was the newline character. Welcome the wonderful world of c++!

include

using 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;
}
Community
  • 1
  • 1
user3735633
  • 251
  • 2
  • 19
  • Why should it "really be a double if we are talking about grades"? As far as you know, this score is out of 100, rendering fractional values impossible, and certainly within range of `int`. At least explain the use of `double` so OP doesn't think it's just something we use to talk about school grades. – OJFord Jun 16 '14 at 11:06
  • The reason I say we should use a double is that this program is now more versatile. It not only applies to a single grade, but it could apply to a calculated course grade with decimal places. Yes, the use of int reduces the amount memory we use, but with only one variable, the memory is negligible. – user3735633 Jun 16 '14 at 20:29
  • 1
    Thanks for the help. I seem to have gotten the hang of it now. I just started learning C++ in earnest this week. I'm finding it very logical, but it's sometimes a struggle to keep an eye out as far as syntax goes. I'm sure that will come as I develop better coding practices. Thanks again. – Samson Jun 18 '14 at 15:42