//this is how I was told to get input.
Well, the first thing I was going to comment on is
getline
will get a word for you no problem. Also, it's a really weird loop. I did a double take before I understood what was going on.
This would be more understandable, I find:
1 2 3 4 5 6 7 8 9 10 11
|
char new_char;
do
{
new_char = getchar();
if(new_char != '\n')
{
user_input[x++] = new_char;
}
} while(new_char != '\n');
|
OK, now on to the real problem.
but when the user inputs anything less than four characters, it outputs the first few and then gibberish. |
When I ran it there was gibberish even if I put four or more characters. The gibberish is there because when
cout
prints a
char
array or
char*
, it keeps printing until it sees the null terminator character
'\0'
(ASCII 0). This tells it when the printing is finished.
After the loop, you should put
user_input[x] = '\0';
and then there will be no garbage printed out. This of course assumes that
x <= 99
upon termination of the loop.