Public Group

# GRRR!

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

## 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 on other sites
It's a typo. Look hard at the end of the line.

##### 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 on other sites
Yeah, "n\" should be "\n".

##### 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.

• 10
• 48
• 12
• 10
• 10
• ### Forum Statistics

• Total Topics
631382
• Total Posts
2999679
×