Like Tree2Likes
  • 2 Post By manasij7479

cout code, endl, and \n help?

This is a discussion on cout code, endl, and \n help? within the C++ Programming forums, part of the General Programming Boards category; Alright, so I just started getting into c++ and there's a few questions I have. My first question is what ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    3

    Arrow cout code, endl, and \n help?

    Alright, so I just started getting into c++ and there's a few questions I have.

    My first question is what is the difference between endl; and \n? I have seen a bit on this, but I am not understanding it.

    My second question has to do with a simple code that I wrote up.

    Code:
    #include <iostream>
    #include <conio.h>
    
    
    using namespace std;
    
    
    int main()
    {
        char name;
        cout << "Please type your name in and press enter..." << endl;
        cin >> name;
        cout << "Welcome, " << name << ". Thank you for telling us your name!" << endl;
        getch();
        return 0;
    }
    With this code, compiled and used in the cmd prompt, it allows you to enter a name and everything. However, say I enter the name of "ZezXion", it only replies with "Welcome, Z. Thank you for telling us your name!"

    If anyone can explain to me why this is happening, or explain the difference between endl and n, I'd appreciate it. Thanks!

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    1. std::endl flushes(i.e forces output) the output buffer while just putting '\n' doesn't.

    2. Declare name as std::string instead of char, which is for a single character.
    rags_to_riches and Elysia like this.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,532
    My first question is what is the difference between endl; and \n? I have seen a bit on this, but I am not understanding it
    \n is a character and endl is a stream manipulator. You have a choice because what can happen depends on what you want.

    If anyone can explain to me why this is happening
    Characters are not strings. If you want to fix your program, read the chapter that covers strings from your book.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    3
    Alright, thanks to you both. Got it working.

  5. #5
    Dweeb dojha00's Avatar
    Join Date
    Feb 2012
    Location
    Global
    Posts
    23
    I got something interesting link related to this topic so i thought to put it here..

    Cambridge University Engineering Department - C++ Input/Output
    What is life??

  6. #6
    Registered User
    Join Date
    Nov 2012
    Location
    Some rock floating in space...
    Posts
    32
    Dude... If you change the "char name;" declaration in your code to "string name;" it will fix your problem.. A char is a single character... A char* is a pointer to a character string however, but it would have to be allocated with malloc or new. std::string is a string class supplied by the standard template library and if you're using the standard template library, then std::string is what you need to hold strings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cout << oct << -12 << endl; ?
    By manav in forum C++ Programming
    Replies: 1
    Last Post: 04-16-2008, 03:39 AM
  2. cout << SunTradingLLC << "is looking for C++ Software Developers" << endl;
    By sun trading in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 04-02-2008, 11:48 AM
  3. Problem with in, "cout" (Each "endl"
    By andreas_nordman in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2004, 07:25 AM
  4. Whats the difference between cout and std::cout?
    By mdshort in forum C++ Programming
    Replies: 10
    Last Post: 12-30-2003, 04:34 PM
  5. endl vs \n
    By Chewbacca in forum C++ Programming
    Replies: 5
    Last Post: 09-08-2002, 12:42 PM

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