Remove:
Code:
cin >> choicechar[1000];
That I didn't present in my code.
What this line does is reading the first character from the stream. So that character is "eaten" away.
Then when you exectute:
Code:
cin.getline(choicechar, 1000, '\n');
The first character (wich was "B") is gone. Therefore cin.getline will read what is left of the stream.
I have a few tips, though wich go beyond your question:
- You preserved 1000 bytes (almost 1 Kilo Bytes) for a nick.
1000 characters is about 12 lines of characters for a nick in a console app. Console is 81 characters wide.
- Why declaring a C-style char (choicechar[1000]) only to store it in a C++ style string (charnick)?
Use my first code with
getline.
- You declared your variables on a global scope. Better forget that that's possible for now.
Once you are ready for (multi)threaded applications you will find out why. Declare them in
main, or even better,
the class that handles user input.
- You are implementing
main() with a
void return type.
Do it like this:
Code:
int main()
{
// your code here
return 0;
}
This has to do with portability with other compilers.
- Look at the second line of your code on top of this thread.
One long sentence or so. Split it over multiple lines.
Code:
int main()
{
string charnick;
cout << "Before you may start the game you have to create you character.\n"
"This will only take a few moments, remember, "
"that you can at any time in the game type \"help\", "
"\"exit\" or \"restart\". \n\nNow just follow the instructions point by point."
"\n\nFirst of all, you need to decide what you will be known as during the game."
"\n\nWrite the name you wish to be known by here (may not contain any spaces): \n";
getline(cin, charnick);
cout << endl << endl << charnick<<endl;
return 0;
}
This is much more readable, on forums and during coding.