Why isn't this string comparison working?

closed account (Ey80oG1T)
So let's say that you are getting user input with getline()

1
2
3
4
5
  string userInput;
  getline(cin, userInput);

  if (userInput == "Hello World!")
    cout << "Sucessful";


However, even if I enter "Hello World!", nothing happens! It appears to be working fine with cin, but I want multiple lines of input. So why isn't this comparision working?
Last edited on
Well if you entered "Hello World" the match should fail for the string "Hello World!" since the strings are not the same.
closed account (Ey80oG1T)
Well, I didn't mean that. That was just a mistake when typing this question. Even if the inputs are correct, nothing happens...
Last edited on
You'll need to show more content.

The smallest possible complete program that illustrates your problem would be best.


Please don't change prior posts after the fact, it makes following the topic harder.
closed account (Ey80oG1T)
That is the smallest possible complete program

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int main()
{
  string userInput;
  getline(cin, userInput);

  if (userInput == "Hello World!")
    cout << "Sucessful";
}


This is all I have.
Last edited on
It works for me in the online compiler, entry "Hello", output:

1
2
Hello
Sucessful 


You really should be #including the <string> header file as well.

It works fine on cpp.sh:
Hello
Sucessful 



You may need #include <string> in some implementations, I suppose.
closed account (Ey80oG1T)
Sorry, bad code. I meant to compare the user input to "Hello World!".

Regardless of the input, my original question is why the string comparisons doesn't work with getline. If I used cin instead, things work, but I want my input to contain a space, so I have to use getline.
This program:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int main()
{
  string userInput;
  getline(cin, userInput);

  if (userInput == "Hello World!")
    cout << "Sucessful";
}


Works fine with the online compiler. Output:

Hello World!
Sucessful

there is nothing wrong with your code, just include <string> and it should work fine, if not then use a different compiler.
Dude, we seriously need more information -- unless you are only here to hate on C++.

OS? Version?
Compiler? Version? Compile command? Flags? Etc.
Got multiple compilers in your PATH?
No warnings? Messages of any kind?
Anything else linked to your code, or in your code, that you aren't sharing?

Be cause you are claiming that valid code isn't, a priori -- code that is correct and should flawlessly function for compilers 20 years old.

In other words, something on your end is screwed up, not in the language, or the compiler design, or our understanding of the presented material, or anything...

Post back and we'll do our best to help.
Perhaps there are invisible characters in your source code, or in your input?
^^^ Print the string letter by letter in hex, and also its length, for both strings, one on top of the other. It will pop out if there is a char encoding issue.
Sometimes the same looking letter isnt really the same. If you put a 0 instead of space between 2 words, it will look like a space in a std::string on most consoles. If you use windows' quotes instead of quote char, those do not match, as anyone who ever opened and resaved code in M$ word or M$ outlook knows. And there are local settings that move a few things around as well. Do the above and show us the output or see if that is your issue.
Make a completely new project, then paste in the code in jlb's post.
Compile and run that.
Topic archived. No new replies allowed.