Thread: Help with strings

  1. #1
    Registered User
    Join Date
    Feb 2005

    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?

    void readLine(istream &, char *, int); //used for TASK 3 only
    #define TASK 1
    #if TASK==1
    const int MAX_LEN = 7;   //longest string allowed.
    const int MAX_LEN = 25;
    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
    Join Date
    Dec 2007

  3. #3
    Registered User
    Join Date
    Jan 2005
    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, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM