Unable to compare strings, need assistance.

I'm making a study guide for my NJROTC class, and I cannot compare the strings of my user's guess of the order with the correct order. I should be able to, so I think my code is wrongly formatted. I don't know for sure and I've checked over it many times.

Here's the confused line:

1
2
3
4
5
if (orderguess = "Sir, the first order to the sentry is to take charge of this post and all government property in view, sir.")
		{
			cout<<"Correct.\n";
			cin.get();
		}



c:\documents and settings\owner\my documents\visual studio 2008\projects\orders\orders\orders.cpp(76) : error C2451: conditional expression of type 'std::basic_string<_Elem,_Traits,_Ax>' is illegal

Full program (not yet completed entirely):
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "stdafx.h"
#include <iostream>
#include <windows.h>

using namespace std;

void order1hint()
{
	cout<<"Sir, the first order to the sentry is to take charge of **** **** and *** gove****** prop**** i* ****, sir.\n";
}

int main()
{
	int orderchosen2;
	int orderchosen;
	string orderguess;
	string order1("Sir, the first order to the sentry is to take charge of this post and all government property in view, sir.");
	string order2("Sir, the second order to the sentry is to walk my post in a military manner, keeping always on the alert and reporting everything that takes place within site or hearing, sir.");
	string order3("Sir, the third order to the sentry is to report all violations of orders I am instructed in enforce, sir.");
	string order4("Sir, the fourth order to the sentry is to repeat all calls from post more distant from the guardhouse (quarter-deck) than my own, sir.");
	string order5("Sir, the fifth order to the sentry is to quit my post only when properly relieved, sir.");
	string hint("hint");
	cout<<"This program is designed to help you learn and memorize the Orders Of the Sentry as quickly as possible. \
		  \n Use it as needed.\n";
CHECKPOINTONE:
	cout<<"Type in the name of the sentry you want to see.\n";
	cout<<"Order to the sentry: 1\nOrder to the sentry: 2\nOrder to the sentry: 3\nOrder to the sentry: 4\nOrder to the sentry: 5\n";
	cin>>orderchosen;
	switch(orderchosen)
	{
	case 1:
		cout<<"Sir, the first order to the sentry is to take charge of this post and all government property in view, sir.";
		cin.get();
		break;
	case 2:
		cout<<"Sir, the second order to the sentry is to walk my post in a military manner, keeping always on the alert and reporting everything that takes place within site or hearing, sir.";
		cin.get();
		break;
	case 3:
		cout<<"Sir, the third order to the sentry is to report all violations of orders I am instructed in enforce, sir.";
		cin.get();
		break;
	case 4:
		cout<<"Sir, the fourth order to the sentry is to repeat all calls from post more distant from the guardhouse (quarter-deck) than my own, sir.";
		cin.get();
		break;
	case 5:
		cout<<"Sir, the fifth order to the sentry is to quit my post only when properly relieved, sir.";
		cin.get();
		break;
	default:
		cout<<"That sentry is not included.\n";
		goto CHECKPOINTONE;
		cin.get();
	break;
	cin.get();
	}
	cin.get();
	cout<<"*********************** NJROTC SENTRY ORDERS STUDY GUIDE*************************\n";
	cout<<"\n\n\nIf at any time you need a hint for your current Order, type in \"hint\" when prompted\nfor \
		  your guess.\n";
	cout<<"What order do you want to guess? Enter its number.\n";
	cin>>orderchosen2;
	cout<<"Remember, capitals and sir at the beginning and end of the Order count.\n";
	Sleep(5000);
	switch(orderchosen2)
	{
	case 1:
		cout<<"Enter your guess...";
		Sleep(5000);
		cout<<" now.\n";
		cin.getline(cin, orderguess, '\n');
		if (orderguess = "Sir, the first order to the sentry is to take charge of this post and all government property in view, sir.")
		{
			cout<<"Correct.\n";
			cin.get();
		}
		else if (orderguess = hint)
		{
			order1hint();
			cin.get();
		}
		else (
			cout<<"Wrong.\n";
		cin.get();
		)
		cin.get();
	}
	cin.get()
}


If I use == instead of =, I get this:

c:\documents and settings\owner\my documents\visual studio 2008\projects\orders\orders\orders.cpp(76) : error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)


What do you guys think?
Try adding:

#include <string>

To your include listing. The string that iostream and/or windows includes probably doesn't have all the string class in it.
Tried it, didn't work. Same error message remains. Thanks anyway. Anyone? I need this completed soon so I can study for annual inspection next week.
Try using strcmp:
http://www.cplusplus.com/reference/clibrary/cstring/strcmp.html
For example:
if(strcmp(orderguess.c_str(), /*that long string*/) == 0) //If strcmp returns 0, then the strings are equal
It's not your code. I don't think that the string class has an operator==() for C-style strings.
BTW: need to #include <cstring>
Last edited on
Ok, I compilied it...and I got a couple of errors.

Line 72: cin.getline(cin, orderguess, '\n');
That's not valid, I think you mean getline(cin, orderguess, '\n');
(cin.getline() only works with a char*)

Line 83/86: You have ( ) instead of { }

Line 89: You are missing a ; after cin.get()

Also, when I included string (above windows.h) the == worked fine.

@QWERTY: I don't see any C-style strings in their code...
Last edited on
ALL string literals, "Hello world", for instance, are C-style.
AFAIK.
Ahh I see what you were looking at then. Strings have the == operator overloaded for const char* I believe (since I don't get a warning or anything about it when I do it)
Last edited on
You need to use the == as mentioned above. = by itself is the assignment operator.
Thank you all for your immediate advice. I took QWERTY's advice of using the strcmp function and finished my program with this:

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <string>
#include <cstring>
#include <string.h>
#include <stdio.h>

using namespace std;

void order1hint()
{
	cout<<"Sir, the first order to the sentry is to take charge of **** **** and *** gove****** prop**** i* ****, sir.\n";
}
void order2hint()
{
	cout<<"Sir, the second order to the sentry is to **** my post in a mi****** mann**, keeping always on the a***t and re***t*ng eve*y*h*ng that takes pl*c* within si** or h*ari*g, sir.\n";
}
void order3hint()
{
	cout<<"Sir, the third order to the sentry is to r****t all vio*a*i*ns of orders I am instructed to enforce, sir.\n";
}
void order4hint()
{
	cout<<"Sir, the fourth order to the sentry is to ****** all calls from post more d*s*ant from the g**rdhouse (quarter-deck) than my own, sir.\n";
}
void order5hint()
{
	cout<<"Sir, the fifth order to the sentry is to quit my post only when pr*pe*ly rel*e*ed, sir.\n";
}
int main()
{
	int orderchosen2;
	int orderchosen;
	string orderguess;
	string orderguessfix;
	string order1("Sir, the first order to the sentry is to take charge of this post and all government property in view, sir.");
	string order2("Sir, the second order to the sentry is to walk my post in a military manner, keeping always on the alert, and reporting everything that takes place within sight or hearing, sir.");
	string order3("Sir, the third order to the sentry is to report all violations of orders I am instructed to enforce, sir.");
	string order4("Sir, the fourth order to the sentry is to repeat all calls from post more distant from the guardhouse (quarter-deck) than my own, sir.");
	string order5("Sir, the fifth order to the sentry is to quit my post only when properly relieved, sir.");
	string hint("hint");
	cout<<"This program is designed to help you learn and memorize the Orders Of the Sentry as quickly as possible. \
		  \n Use it as needed.\n";
CHECKPOINTONE:
	cout<<"Type in the name of the sentry you want to see.\n";
	cout<<"Order to the sentry: 1\nOrder to the sentry: 2\nOrder to the sentry: 3\nOrder to the sentry: 4\nOrder to the sentry: 5\n";
	cin>>orderchosen;
	switch(orderchosen)
	{
	case 1:
		cout<<"Sir, the first order to the sentry is to take charge of this post and all government property in view, sir.";
		cin.get();
		break;
	case 2:
		cout<<"Sir, the second order to the sentry is to walk my post in a military manner, keeping always on the alert and reporting everything that takes place within site or hearing, sir.";
		cin.get();
		break;
	case 3:
		cout<<"Sir, the third order to the sentry is to report all violations of orders I am instructed to enforce, sir.";
		cin.get();
		break;
	case 4:
		cout<<"Sir, the fourth order to the sentry is to repeat all calls from post more distant from the guardhouse (quarter-deck) than my own, sir.";
		cin.get();
		break;
	case 5:
		cout<<"Sir, the fifth order to the sentry is to quit my post only when properly relieved, sir.";
		cin.get();
		break;
	default:
		cout<<"That sentry is not included.\n";
		goto CHECKPOINTONE;
		cin.get();
	break;
	cin.get();
	}
	cin.get();
	cout<<"*********************** NJROTC SENTRY ORDERS STUDY GUIDE*************************\n";
	cout<<"\n\n\nIf at any time you need a hint for your current Order, type in \"hint\" when prompted\nfor \
		  your guess.\n";
	cout<<"What order do you want to guess? Enter its number.\n";
	cin>>orderchosen2;
	cout<<"Remember, capitals and sir at the beginning and end of the Order count.\n";
	cout<<"Press the space bar when ready.\n";
	cin.get();
	Sleep(5000);
	switch(orderchosen2)
	{
	case 1:
CHECKPOINTTWO:
		cout<<"Enter your guess...";
		Sleep(5000);
		cout<<" now.\n";
		getline(cin, orderguessfix, '\n');
		if (strcmp (orderguessfix.c_str(),order1.c_str() ) == 0)
		{
			cout<<"Correct.\n";
			cin.get();
		}
		else if (strcmp (orderguessfix.c_str(), hint.c_str() ) == 0)
		{	
		order1hint();		
		goto CHECKPOINTTWO;
		}
		 else {
			cout<<"Wrong.\n";
			cin.get();
		}
	case 2:
CHECKPOINTTHREE:
		cout<<"Enter your guess...";
		Sleep(5000);
		cout<<" now.\n";
		getline(cin, orderguessfix, '\n');
		if (strcmp (orderguessfix.c_str(),order2.c_str() ) == 0)
		{
			cout<<"Correct.\n";
			cin.get();
		}
		else if (strcmp (orderguessfix.c_str(),hint.c_str() ) == 0)
		{
			order2hint();
			goto CHECKPOINTTHREE;
		}
		else {
			cout<<"Wrong.\n";
			goto CHECKPOINTTHREE;
		}
	case 3:
CHECKPOINTFOUR:
		cout<<"Enter your guess...";
		Sleep(5000);
		cout<<" now.\n";
		getline(cin, orderguessfix, '\n');
		if (strcmp (orderguessfix.c_str(),order3.c_str() ) == 0 )
		{
			cout<<"Correct.\n";
			goto CHECKPOINTRANDOM;
		}
		else if (strcmp (orderguessfix.c_str(),hint.c_str() ) == 0 )
		{
			order3hint();
			goto CHECKPOINTFOUR;
		}
		else {
			cout<<"Wrong.\n";
			goto CHECKPOINTFOUR;
		}
	case 4:
CHECKPOINTFIVE:
		cout<<"Enter your guess...";
		Sleep(5000);
		cout<<" now.\n";
		getline(cin, orderguessfix, '\n');
		if (strcmp (orderguessfix.c_str(),order4.c_str() ) == 0)
		{
			cout<<"Correct.\n";
		}
		else if (strcmp (orderguessfix.c_str(),hint.c_str() ) == 0)
		{
			order4hint();
			goto CHECKPOINTFIVE;
		}
		else {
			cout<<"Wrong.\n";
			goto CHECKPOINTFIVE;
		}
	case 5:
CHECKPOINTSIX:
		cout<<"Enter your guess...";
		Sleep(5000);
		cout<<" now.\n";
		getline(cin, orderguessfix, '\n');
		if (strcmp (orderguessfix.c_str(),order5.c_str() ) == 0 )
		{
			cout<<"Correct.\n";
		}
		else if (strcmp (orderguessfix.c_str(),hint.c_str() ) == 0 )
		{
			order5hint();
			goto CHECKPOINTSIX;
		}
		else {
			cout<<"Wrong.\n";
		}
CHECKPOINTRANDOM:
		cin.get();
	}
		
		cin.get();
}


Along with a few features I'll be adding it in to really make it fancy.
Topic archived. No new replies allowed.