GRRR!

Started by
3 comments, last by Zahlman 19 years, 4 months ago
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
Grim_reaper7Lamp Geekz
Advertisement
It's a typo. Look hard at the end of the line.
-------------"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."- Charles Babbage (1791-1871)
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.
BladeStoneOwner, WolfCrown.com
Yeah, "n\" should be "\n".
Ra
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.

This topic is closed to new replies.

Advertisement