Problem with the == part of an if statment

I am having a problem with my if statement as on the tutorial in the "Statements and flow control" section it has this code:
1
2
3
4
if (x == 100)
  cout << "x is 100";
else
  cout << "x is not 100";

But when I put that in i get an error with the == part saying it's not a operator. If I am doing anything wrong, Please reply to me as soon as possible as I can trying to learn C++ but not getting anywhere with this bug.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string x;
	cout << "Enter number here please: ";
	cin >> x;
	if (x == 100)
		cout << "your number is 100" << endl;
	else
		cout << "Your number is not 100. It is " << x << endl;
	return 0;
}

I have tried using getline(cin, x) instead of just cin x; but still getting the same error.
Its simple. Why is your x variable a string? Its a number and should be an integer.

You cant do string(x) == integer(100).

do this instead

1
2
3
int x;
cout << "Enter number here please: ";
cin >> x;


And it should work fine.
change string x to int x;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

using namespace std;
int main()
{
	int x;
	cout << "Enter number here please: ";
	cin >> x;
	if (x==100)
		cout << "your number is 100" << endl;
	else
		cout << "Your number is not 100. It is " << x << endl;
	return 0;
}

@Arslan7041 there is really no point of literally repeating what I said.
@TarikNeaj


Your post was not visible when I had clicked "reply". I simply "submitted" after you did.
Last edited on
Topic archived. No new replies allowed.