c++ - 比较运算符 == 不起作用,我该如何让它起作用? [cpp]

标签 c++ if-statement equals

我收到以下错误:

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 ===|

使用以下代码:

#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;
}

我认为它会像 if (x == y) 之类的东西一样简单,但我想不是。我该怎么做才能解决这个问题?提前致谢!

最佳答案

不能直接比较字符串和整数。在数字周围使用引号。

关于c++ - 比较运算符 == 不起作用,我该如何让它起作用? [cpp],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5526888/

相关文章:

c++ - 生成文件 : error running 'make all'

c++ - 在 Directshow 中代替使用 ISpecifyPropertyPages 的替代编程方式

javascript - 如何在阶乘计算时打印动态生成的乘法运算字符串?

java - 比较 TreeSet 和 HashSet 的对象

java - 有什么可以警告我不要使用 type.equals(incompatibleType) 吗?

java - Java 中重写 equals() 方法 (OCJP)

c++ - 用类 fnc 中的数据填充 vector

c++ - 如何使 boost::serialization 与 std::shared_ptr 一起工作?

javascript - 为什么 javascript 不能在 if 语句中使用 .style.color

javascript - 检查具有一定宽度的div