Thread: How's this work?

  1. #1
    Registered User
    Join Date
    Dec 2009
    Location
    Leicester, UK.
    Posts
    2

    Unhappy How's this work?

    Hi everyone,
    can a I please pose a silly question, I've been working my way through a teach yourself book on c++ and ended up basically with a line of code that looked something like this:

    cout<<result<<'\n';

    Which worked fine, so I then decided I needed an extra line so I ended up with this:

    cout<<result<<'\n\n';

    Which did not work at all and I ended up with the following:

    102570Please enter a character to exit

    Now, I've worked out that this is due to using single quotes which only have a single byte storage capacity and I'm trying to store two bytes of data, but what I can't understand is the output, where does 102570 come from?


    Many thanks,

    Harvey
    Last edited by TheNovice; 12-23-2009 at 07:51 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Recall that single quotes are used for character literals, but double quotes are used for string literals.

    Quote Originally Posted by TheNovice
    Now, I've worked out that this is due to using single quotes which only have a single byte storage capacity and I'm trying to store two bytes of data, but what I can't understand is the output, where does 102570 come from?
    It could be the result of trying to print a multicharacter literal. I am afraid that I am unable to elaborate, other than to say that I recall that multicharacter literals are of type int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2009
    Location
    Leicester, UK.
    Posts
    2
    Thanks very much for that Laserlight, If anyone else can add anything more to this that would be great.

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    There is nothing more to add. Change the single quotes to double quotes.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    There is more to add. Two things:
    Okay, '\n\n' is probably done in ascii, making it 10*256 + 10 = 2570. The 10 before it is probably the actual result. So yes, if result is 10 then cout << result << '\n\n'; in ascii may very well output 102570.
    Secondly, you shouldn't use '\n' as a newline. Not all operating systems use it. While it works fine in Linux, for instance, Windows uses \r\n. And outputting only \n may move a line down without resetting the cursor to the first character in the line. Better output std::endl.
    Furthermore, the output isn't flushed after '\n', but it is after std::endl, although that likely won't matter in this case.

    So you should really use:
    cout << result << endl << endl;
    (Both cout and endl are in namespace std, but you seem to be using namespace std)

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by EVOEx
    Secondly, you shouldn't use '\n' as a newline. Not all operating systems use it. While it works fine in Linux, for instance, Windows uses \r\n. And outputting only \n may move a line down without resetting the cursor to the first character in the line.
    From what I understand, unless the stream is in binary mode, translation from '\n' to the appropriate newline sequence will be done automatically.

    Quote Originally Posted by EVOEx
    Better output std::endl.
    Case in point: the C++ standard says that the effect of endl is to call "os.put(os.widen('\n')), then os.flush()", where os is a non-const reference to a basic_ostream. From what I understand, the purpose of widen is not to perform newline sequence translation, but rather character mapping based on locale.

    Quote Originally Posted by EVOEx
    Furthermore, the output isn't flushed after '\n', but it is after std::endl, although that likely won't matter in this case.
    Yes, though it may make sense to not explicitly flush when you do not need to.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by EVOEx View Post
    Secondly, you shouldn't use '\n' as a newline. Not all operating systems use it. While it works fine in Linux, for instance, Windows uses \r\n. And outputting only \n may move a line down without resetting the cursor to the first character in the line. Better output std::endl.
    You need a citation for this, because I've used \n for 15 years and never seen anything even remotely like what you claim. Windows does not use \r\n.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by abachler View Post
    You need a citation for this, because I've used \n for 15 years and never seen anything even remotely like what you claim. Windows does not use \r\n.
    Quite a few times I've seen in the windows terminal that the cursor is moved a line down but not the "carriage return", ie. the cursor being set to the first character in the terminal. Though it may not have been C++, it may have been C. C++ may convert it to \r\n, I'm not sure.

    But a citation:
    Newline - Wikipedia, the free encyclopedia
    # Systems based on ASCII or a compatible character set use either LF (Line feed, 0x0A, 10 in decimal) or CR (Carriage return, 0x0D, 13 in decimal) individually, or CR followed by LF (CR+LF, 0x0D 0x0A); see below for the historical reason for the CR+LF convention. These characters are based on printer commands: The line feed indicated that one line of paper should feed out of the printer, and a carriage return indicated that the printer carriage should return to the beginning of the current line.

    * LF: Multics, Unix and Unix-like systems (GNU/Linux, AIX, Xenix, Mac OS X, FreeBSD, etc.), BeOS, Amiga, RISC OS, and others
    * CR+LF: DEC RT-11 and most other early non-Unix, non-IBM OSes, CP/M, MP/M, DOS, OS/2, Microsoft Windows, Symbian OS
    * CR: Commodore 8-bit machines, Apple II family, Mac OS up to version 9 and OS-9
    laserlight, does that mean '\n' is also converted to '\r' in those Mac OS < 9 systems? So, would '\n' work there as well?
    Last edited by EVOEx; 12-23-2009 at 01:34 PM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by EVOEx
    laserlight, does that mean '\n' is also converted to '\r' in those Mac OS < 9 systems? So, would '\n' work there as well?
    Your "citation" agrees with my understanding:
    When writing a file in text mode, '\n' is transparently translated to the native newline sequence used by the system, which may be longer than one character. (...) When reading in text mode, the native newline sequence is translated back to '\n'. In binary mode, the second mode of I/O supported by the C library, no translation is performed, and the internal representation of any escape sequence is output directly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by laserlight View Post
    Your "citation" agrees with my understanding:
    Yes, I know, and I never disagreed with what you said. Only to what Abachler said (in fact, I don't think I've ever seen you being wrong laserlight :P).
    But that would also mean that \n translates into \r for systems that use only \r. However, another source I read stated that \n might be translated into \r\n or \n only. So the sources didn't completely agree there.
    But then again, if it wouldn't, I doubt std::endl would work.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by abachler View Post
    Windows does not use \r\n.
    Yeah, it does. \n is implicitly converted to \r\n by C++, but try setting the text in a textbox to \n. It won't work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM