Jump to content
  • Advertisement
Sign in to follow this  


This topic is 4955 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

wondering why this code(*** inicates where it is giving me the error from): //a small C++ program #include <iostream> using namespace std; int main() { int cbare; int alternate; int older; int younger; cout<<"HEY, you, I'm alive! Oh, and Hello World!\n"; cout<<"enter you favorite number\n"; cin>>cbare; cin.ignore(); if (cbare > 21){ older = cbare - 21; cout<<"You are older than me by "<<older<<" years\n"; } else if (cbare < 21){ younger = 21 - cbare; **** cout<<"I am older than you by "<<younger<<"n\"; } cin.get(); } give me this error: error C2001: newline in constant

Share this post

Link to post
Share on other sites
the slash n command is \n it stands for new line which in the older languages ment, go down one line.

\r is return to beginning of current line, which in the older languages returned the curser to the beginning of the line.

Now either starts a new line and goes to the beginning of current line so you don't have to do \r\n anymore, just \n.

Share this post

Link to post
Share on other sites
The reason you get the error that you do is because the \" gets interpreted as an escape sequence for a double-quote, so your string doesn't end there. And since there aren't any more double-quotes on that line (just a semicolon), the compiler reads up to the end-of-line and says "hey, there's a newline in this string constant". The language doesn't allow you to have actual return characters inside a string (I'm pretty sure; otherwise, this is the compiler being 'friendly' to you and trying to make it easier to catch errors). That's so that you don't end up having the next several lines of your program being interpreted as part of the string (all the way up until the next quote mark) and subsequent code reporting a bunch of much weirder errors (because it doesn't make sense any more without the quoted-out part).

To put actual return characters inside a string constant, use \n as you already know. To wrap a long string constant across several lines, you can:

1) Put a \ at the end of the line, which will tell the preprocessor to join the lines up:

string foo = "I am the very model of a modern Major General";

//Notice that whitespace at the beginning of each line counts,
//which can look bad given your code indentation. Also, this is
//relying on the preprocessor, which is a bad idea in general
//especially when there's a cleaner way.

2) Just write the constant as several adjacent constants, which will be joined together implicitly:

string foo = "I am the very model of a modern "
"Major General";

// Ahh, much cleaner, and using a built-in language feature
// instead. This way you get to indent things as you'd prefer.

3) Do the joining explicitly, by making use of std::string:

string foo = string("I am the very model of ") +
"modern Major General";

// std::string overloads operator+ so that you can concatenate
// either another string or a plain char *. Also, you can (as
// illustrated) initialize one from either an existing string
// or a (possibly const) char *. But note that double-quoted
// string are NOT std::strings as is.

// Anyway, go with method 2 in general. This way puts off the
// addition to runtime (although the compiler will *probably*
// optimize that away), and is needlessly verbose.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!