Help with strings

This is a discussion on Help with strings within the C++ Programming forums, part of the General Programming Boards category; I am learning about strings and I am trying to interpret what the +1 is for when the const MAX_LEN ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    76

    Help with strings

    I am learning about strings and I am trying to interpret what the +1 is for when the const MAX_LEN is used. I know strings have a null terminating value, does this allow room for it?


    Code:
    void readLine(istream &, char *, int); //used for TASK 3 only
    
    #define TASK 1
    
    #if TASK==1
    const int MAX_LEN = 7;   //longest string allowed.
    #else
    const int MAX_LEN = 25;
    #endif
    
    int main(){
    	char aString[MAX_LEN+1];   // what is +1 used for?
    
    	cout << "Enter a string (maximum of " << MAX_LEN << " characters)\n";
    	for (int i=1; i<=MAX_LEN; i++) cout << i%10;  cout << endl;

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    Yes.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Note that it is C-style strings that require the extra character for a null terminator. C++ strings do not use the null terminator and so they don't need that extra character. (You can, of course, use C-style strings in C++ if you want, but generally speaking the C++ string would be a better choice.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Problem with Strings, Please help!
    By varus in forum C++ Programming
    Replies: 8
    Last Post: 11-27-2006, 11:47 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 12:23 PM

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