while ((ch = cin.get() != '\n') && (index < MAX-1))
{
array[index] = ch;
index ++;
}
array[index] = '\0';
Your while-loop runs from 0 to MAX-1 (19). This is good because the array can contain 20 elements (0-19). But then you do this:
array[index] = '\0'; . But index now == MAX so in other words:
array[20] = '\0'; . This will try to write a \0 to element 20 which is the 21th element in the array get it? So not only are you trying to write to memory which is not 'yours', your string doesn't have an 'end of string character' (\0). This will result in memory exceptions and all kinds of other nasty things. (in your case: smilies)
[edit]
Also, I think there should be || instead of && inside the condition of while loop.
Nope, it's good the way it is.
Last edited by Nick Evan; May 11th, 2010 at 9:05 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Offline 4,132 posts
since Oct 2006