Reputation: 1294
I am getting the following error:
C:\Users\*******\Documents\CodeBlocksProjects\encryptText\main.cpp||In function 'int main()':|
C:\Users\*******\Documents\CodeBlocksProjects\encryptText\main.cpp|14|error: no match for 'operator==' in 'givenText == 1'|
C:\Users\*******\Documents\CodeBlocksProjects\encryptText\main.cpp|25|error: no match for 'operator==' in 'givenText == 2'|
||=== Build finished: 2 errors, 0 warnings ===|
Using the following code:
#include <iostream>
#include <string>
#include "encrypt.h"
#include "decrypt.h"
using namespace std;
int main() {
startOver:
string givenText, pass;
cout << "Encrypt (1) or Decrypt (2)?" << endl << "Choice: ";
getline(cin, givenText);
if (givenText == 1) {
cout << endl << "Plain-Text: ";
getline(cin, givenText);
cout << endl << "Password: ";
getline(cin, pass);
cout << endl << encrypt(givenText, pass) << endl << "Restart? (Y/N)";
getline(cin, givenText);
if (givenText == "Y") {
cout << endl;
goto startOver;
}
} else if (givenText == 2) {
cout << endl << "Ciphered-Text: ";
getline(cin, givenText);
cout << endl << "Password: ";
getline(cin, pass);
cout << endl << decrypt(givenText, pass) << endl << "Restart? (Y/N)";
getline(cin, givenText);
if (givenText == "Y") {
cout << endl;
goto startOver;
}
} else {
cout << endl << "Please input 1 or 2 for choice." << endl;
goto startOver;
}
return 0;
}
I thought it would be as simple as a if (x == y) sort of thing, but I guess not. What am I suppose to do to fix this? Thanks ahead of time!
Upvotes: 2
Views: 284
Reputation: 5204
The datatype on givenText
is string. You are comparing it to an integer.
There are two ways to solve this, the simple one:
if (givenText == "1")
Which will take consider 1 as a string.
The other opion (which will work with 1, 01, 0randomCharacters01 etc etc), is int givenTextInt = atoi(givenText.c_str());
And now you can compare like this:
if (givenTextInt == 1)
Upvotes: 2
Reputation: 4458
There's no implicit type conversion for your string, so you either needed to:
a) change your test to compare strings: if (givenText == "1")
or
b) parse your givenText to an integer before comparing: if (atoi(givenText.c_str()) == 1)
Have fun!
Upvotes: 2
Reputation: 10381
1 and 2 are integers, you can't compare them with a string. Compare "1" and "2" instead.
Upvotes: 1
Reputation: 38412
can't compare string to int directly. use quotes around the numbers.
Upvotes: 3