What have I missed in this while-loop?

107 views Asked by At

I am trying to write a simple program that reads a name as a C-style string. The name is then printed vertically, one character per line.

Currently when the program prompts a user to enter their name, eg. Henry James, only 'Henry' is printed vertically. It stops printing at the break between the names.

char myName[ 64 ] = "";
cout << "Enter your name: ";
cin.get( myName, 64 );

int i = 0;
while ( myName [ i ] != ' ' )
    {
        cout << myName[ i ] << endl;
        i++;
    }



getch();
return 0;

I've tried putting cin.ignore() the line before cin.get(), but this ends up breaking the program. What am I missing in the while loop?

1

There are 1 answers

0
Klaus On BEST ANSWER

You explicitly write that your loop should stop at ' ' space character. Everything as expected :-)

If you want to print until end of the C style string, check against the terminating char which is a zero.

while ( myName [ i ] != '\0' )