-
String getting cut off
Hello, I'm just learning C++, and I was working with C++-style strings when something stumbled me.
Code:
#include <iostream>
using namespace std;
int main()
{
string szOriginal, szPlease;
cout<<"Please type a message for me to reprint with the word 'please' at the end!\n";
cin>>szOriginal;
getline(cin, szOriginal, '\n');
szPlease = szOriginal + szPlease;
cout<<szPlease;
szOriginal[0] = '\0';
szPlease[0] = '\0';
cin.get();
}
Basically, what it's supposed to do is add 'please.' to the end of the phrase. However, this code always cuts out the first word.
e.g. "Give me some toast" becomes " me some toast please."
Assistance would be appreciated!
-
That is basically what you've told it to do. What's the problem?
Soma
-
Code:
cin>>szOriginal; // gets first word of sentence into szOriginal
getline(cin, szOriginal, '\n'); // overwrites first word with the rest of the sentence
-
Thank you very much! This problem is solved!
-
Wow, what a way to confuse readers of your code!
The 'sz' prefix of Hungarian notation refers to a C-style string, but you use it on std:: strings. That's one reason I stopped using Hungarian notation a long time ago.
BTW, you also forgot to #include <string>