C-string input / output

Sep 15, 2009 at 12:27am
I'm writing code for a c-string input and output, but when I go for the output, it skips the first word and outputs all the other words after that.

heres the code:

cout << " Input a a phrase with a maximum 30 characters: ";
cin.ignore();
cin >> sphrase;
getline(cin,sphrase);
cout << " The phrase inputed was: " << sphrase << endl;
Sep 15, 2009 at 12:36am
You are mixing formatted and unformatted input (std::cin >> and std::getline()). Read:

http://www.cplusplus.com/forum/articles/6046/
Sep 15, 2009 at 8:04am
cin >> sphrase takes the first word and puts it in sphrase and leaves the rest in the buffer. getline(cin, sphrase) takes the rest of the words from the buffer and puts them in sphrase which is why you lose the first word.
Sep 15, 2009 at 10:28am
If you're using C-strings then use:

1
2
char str[80];
cin.getline(str, 80);

If using String objects(from the string class) use:

1
2
string str;
getline(cin, str);
Topic archived. No new replies allowed.