Compare string doesnt work with cin - c++

I am working on a trivial problem, but I cant figure it out. The program should put out the part after "simon says" if the beginning of str1 is "simon says". if i run it like in the following code, it works, but if i enter the string over cin >> str1; myself, it doesnt. Does anybody have a tip for me? (and yes, this is a kattis problem)
int main()
{
string str1("simon says write a program");
//cin >> str1;
string str2 ("simon says");
if (str1.compare(0,10,str2,0,10) == 0){
cout << str1.substr(11,str1.size());
}
return 0;
}

It's because std::cin gets whitespace-separated strings. If you will try to read a string from the standard input using
std::cin << str1;
// something here
std::cin << str2;
And you will enter "simon says", "simon" will land in the str1 and "says" will go to the str2. To read a whole line you should use
std::getline()

With using cin >> str1, if you print str1 for the sentence "Simon write something", you will see that str1 has the value "Simon".
To not cut the sentence to the first space, Replace cin >> str1 by getline(cin, str1)

Related

String not working with #include <string> and using namespace std

about the code below, string doesn't light up anymore and when I entered "John Smith", only "John" appears, string was working fine for me weeks ago until i tried calling strings function today which didn't work so i tested for a simpler one.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string name;
// Get the user's name
cout << "Please enter your first name: ";
cin >> name;
// Print the greeting
cout << "Hello, " << name << "." << endl;
return 0;
}
string doesn't light up like int
I might be asking at the wrong place but I cant' tell what's the problem, please help :(
To get all the line, use getline(cin, name);
instead of cin >> name;
See http://www.cplusplus.com/reference/string/string/getline/
With std::string's, using std::cin >> someString will only read the first word off the buffer (it will stop at the first whitespace encountered).
Use getline(std::cin, someString) instead to read the entire line.
std::cin gets only characters to first 'white' character, like space, tab or enter.
If you want to read whole line use e.g. getline()
string line;
cin.clear(); //to make sure we have no pending characters in input buffer
getline(cin, line);

How to print multilple words in c++

I can get the file to save right but I can't seem to get multiple words to write to .txt, like if I type "Hi purple" it just writes "Hi", here is code
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
system("color F0");
string name0;
cout << "Please enter a file name, no spaces/special characters" << endl;
cin >> name0;
name0 = name0+".txt";
system("cls");
cout << " FISHSOFT" << endl;
cout << "The best text editor in the world" << endl << endl;
string text;
cin >> text;
ofstream myfile;
myfile.open (name0.c_str() , ios::out | ios::trunc);
myfile << text;
myfile.close();
system("PAUSE");
return 0;
}
Use std::getline to read an entire line of input including spaces.
cin >> text; will read one whitespace delimited token from the input stream. One word in == one word out.
std::getline(cin, text); will read a whole line. Reading more than that is tricky, but typically a loop around multiple calls to getline.
Suggestion: Save yourself time and fire up your IDE's debugger to see what's happening in your code before posting a question. Almost always faster, and if not, you can make much better, tighter-focused questions.
std::cin is able to get several parameters at once.
That mean you may write:
std::cin >> name0 >> name1 >> name2;
// input: a1 a2 a3
// make: name0: a1, name1: a2, name3:a3
By default, the space is the separator between parameters.
To avoid this behavior, you could use getLine:
std::getline(std::cin, name0);
There are 2 ways to get the string with spaces and special character.
cin.getline(name);
gets(name);
Hope this will serve your purpose.

Splitting string from inputstream with whitespace delimiter

I can't seem to split my string with the whitespace delimiter.
I tried using the getline(cin, myString).
I input the string as "10 20 30", when I print the string onto the output stream, I get "20 30", but not the 10.
I also tried the while(cin >> string). It works here, but the while loops never terminates.
Any help is appreciated.
You can use istringstream:
string str;
getline(cin, str);
istringstream ss(str);
for(string word; ss >> word; )
cout << word << endl;
Your while(cin >> str); doesn't stop unless:
Something fails in >> which is hard in your case.
EOF occurs (You can use Ctrl+Z, Ctrl+D, F6 It depends on your system)
A user defined condition: if(str.find('\n')) break;

C++ what is wrong with this do while loop

I am trying to input a string. It's working fine when I input something like John.
But if I input something like John Smite I end up in an endless loop and a terminal crash.
string fullname;
do{
cout << "Please input the Full Name of the user: ";
cin >> fullname;
}while(fullname=="");
The space is throwing cin off. You should use getline.
You can try this one:
do{
cout << "Please input the Full Name of the user: ";
cin >> fullname;
}
while(fullname.length() == 0);
As to why you get an infinite loop - operator>> overload for std::string will first discard any leading whitespace and then read up to either next whitespace or the end of avaliable input.
When you enter "John Smite", first iteration reads "John", second iteration reads "Smite" and then there's no more input for subsequent iterations. The problem is that your implementation seems to clear fullname before it attempts a read. But because the stream is not in good state anymore, no more reads are possible and there's your infinite loop.
You could do something like this instead:
string temp;
string fullname;
do {
cin.clear(); // clear any error flags
do {
if (cin >> temp) fullname += temp + ' ';
} while (cin.good());
} while (fullname.empty());
This has a (good) side effect that it collapses multiple adjacent whitespace characters, but it's still pretty clunky.
Much better way would be to just say
do std::getline(std::cin, fullname); while (fullname.empty());

Trouble reading char by char from a file in C++

So I have this piece of code:
ifstream sFile;
sFile.open(argv[1]);
stringstream ss;
unsigned char aChar;
string aString;
while (sFile >> noskipws >> aChar) {
ss << noskipws << aChar;
}
ss >> noskipws >> aString;
cout << noskipws << aString << endl;
My file contains:
"SHE SMELLS"
In stdout, all I see is "SHE". I've wasted so much time trying to figure this out, can someone help me understand why this is happening?
Also, will this be able to read in characters from the extended ascii alphabet?
Look at this page for noskipws:
http://www.cplusplus.com/reference/iostream/manipulators/noskipws/
In the example it shows the problem you are having, it is fixed by using:
iss.seekg(0);
This should work if you do this:
sFile.seekg(0);
before your while loop.
Hope that is right / helps.