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
const int MAX_LEN = 7; //longest string allowed.
const int MAX_LEN = 25;
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;
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.)