Lesson 9: C-strings and '\n'

This is a discussion on Lesson 9: C-strings and '\n' within the C++ Programming forums, part of the General Programming Boards category; Here's a link to the tutorial: C Strings - C Tutorial - Cprogramming.com I'm a little confused on this part ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    13

    Lesson 9: C-strings and '\n'

    Here's a link to the tutorial: C Strings - C Tutorial - Cprogramming.com

    I'm a little confused on this part of the tutorial:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      char string[256];                               // A nice long string
    
      cout<<"Please enter a long string: ";
      cin.getline ( string, 256, '\n' );              // Input goes into string
      cout<<"Your long string was: "<< string <<endl;
      cin.get();
    }
    Remember that you are actually passing the address of the array when you pass string because arrays do not require an address operator (&) to be used to pass their address. Other than that, you could make '\n' any character you want (make sure to enclose it with single quotes to inform the compiler of its character status) to have the getline terminate on that character.
    I thought '\n' was something to put on the end of a line of coding to take it to the next line, simply for tidiness?

    If I substitute '\n' for '\b', shouldn't the getline terminate at the point I type in b?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      char string[256];                               // A nice long string
    
      cout<<"Please enter a long string: ";
      cin.getline ( string, 256, '\b' );              // Input goes into string
      cout<<"Your long string was: "<< string <<endl;
      cin.get();
    }
    Thanks for any help.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,769

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Quote Originally Posted by rwebb2305 View Post
    If I substitute '\n' for '\b', shouldn't the getline terminate at the point I type in b?
    More precisely, it should terminate when it finds '\b' in the input string, not 'b'.
    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.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    '\b' is also probably the backspace character and it's usually not in 'cooked' input, a la getline.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Also, keep in mind that whatever the operation of getline, your terminal will read until you press the enter key, and allow you to modify the string you type until you submit it. Only the final version you see on the screen is submitted, not the string or typed characters, cursor movements, and deletions that lead to that string.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    13
    OK, thanks these posts have helped a lot and I think I understand now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lesson #4 - The Quiz
    By oval in forum C# Programming
    Replies: 0
    Last Post: 04-27-2006, 09:31 AM
  2. now im on lesson 5
    By NiVaG in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2004, 10:40 PM
  3. Lesson 4 of Tutorial
    By polonyman in forum C++ Programming
    Replies: 5
    Last Post: 09-09-2004, 07:36 AM
  4. Lesson 5
    By polonyman in forum C++ Programming
    Replies: 5
    Last Post: 09-09-2004, 07:06 AM
  5. OpenGL Lesson 3
    By Gonavitch in forum C++ Programming
    Replies: 8
    Last Post: 08-09-2004, 10:31 PM

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