How's this work?

This is a discussion on How's this work? within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, can a I please pose a silly question, I've been working my way through a teach yourself book ...

  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 06:51 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,189
    There is nothing more to add. Change the single quotes to double quotes.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  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
    20,973
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,189
    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.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  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 12:34 PM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

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, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21