no global operator found

error C2677: binary '==' : no global operator found which takes type 'std::string' (or there is no acceptable conversion)

How to fix this code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "stdafx.h"
#include <iostream>
#include <string>
#include <limits>

	//set std;
	using namespace std;

int main()
{
	//set variables
	int ep = 52;
	int op = 9;
	int dp = 11;
	string es = "Elmeri";
	string os = "Oliver";
	string input;

	//start program
	cout << "What's your name?"
		cin >> input;
	if (input == es)
	{
		cout << "Lol, your penis is " << ep << "cm long!" << endl;
	}
	else if (input == os)
	{
		cout << "Lol, your penis is " << op << "cm long!" << endl;
	}
	else 
	{
		cout << "Lol, your penis is " << dp << "cm long!" << endl;
	}
	cout << "";
	cout << "Press ENTER to continue...";
    cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
	return 0;
}
Last edited on
You are trying to test if a number (input, an integer) is the same as a string. That makes no sense. One is a single number and the other is a fancy class object wrapping an array of char. What does it even mean for them to be the same?

You're also asking the user what their name is, and then trying to store that in an int value. Storing input such as "Oliver" as an int value makes no sense.

Think carefully about what kind of object input should be.
By the way, I fixed the input to be a string, but it still doesn't work.
Im totally new to C++, I just came from the world of Lua to test out c++.
Please do not change code in posts. It makes following posts nonsensical for everyone reading the thread.

Well let's take a look at the new error.

Line 21: error: expected β€˜;’ before β€˜cin’

Let's take a look at line 21.
cin >> input;
Well there's the cin. It says it expected a semi-colon before that. Let's take a look at what comes before that.
cout << "What's your name?"
Hey, look, here's a line with no semi-colon on the end.

The moral of today's story is read the error messages and look at the code it points you to.
Last edited on
Oh my, what a little mistake ruining 3 hours. Oh well. Atleast ive gotten used to this while programming big lua scripts :D
closed account (3qX21hU5)
Well actually the moral of the story is learn what errors mean what on the compiler you are using. If you have just started learning C++ this is still pretty easy to do. Just write a simple program like the one you have above. Once it has run with no compiler errors, in there and start changing little things one at a time and see what error it pops out. Also look at the manual for your compiler they usually have sections on there error messages.

I know what you are thinking "but that could take hours", yes it could but it will pay off in the long run when forget to a something and a pesky mysterious error code pops up and you already know what it means instead of spending "hours" trying to fix it.
You shall insert a semicolon after the first statement in this code snip

1
2
	cout << "What's your name?"
		cin >> input;
Topic archived. No new replies allowed.