I have tried using getline()
but the delimiter
being set to "!!
" causes the program to not compile. I need it the string read into a string variable called messages. My code looks like this... Help?
cout << "Enter the message> ";
getline(cin, message, "!!");
You are not using the std function properly. You are trying to pass a string for the delimiter rather than a character. If that is the delimiter you need, getline will not help you off-hand.
http://www.cplusplus.com/reference/string/string/getline/
Here you can find a working code for what you would like to achieve:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string message;
cout << "Enter the message>";
cin >> message;
cout << message.substr(0, message.find("!!")) << endl;
return 0;
}
You need to run this command or something similar for your scenario: g++ main.cpp && a.out
Output is:
Enter the message>sadfsadfdsafsa!!4252435
sadfsadfdsafsa
str.substr(0, inStr.find("!!"));
Example: http://codepad.org/gPoqJ1Ie
Explanation
dmitri explains why the error occurs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With