You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++代码用户输入校验问题:判断输入是否为1/2失效排查

你的C++代码问题分析与修复方案

嘿,我一眼就看出你代码里的几个关键问题,这也是新手写C++时常踩的坑,咱们一个个捋清楚:

核心问题1:搞混了赋值和比较运算符

这是最致命的错误!你所有的if判断用的都是赋值运算符=,而不是比较运算符==。比如:

if (difficulty = '1') { ... }

这行代码的作用是把字符'1'的ASCII值(49)赋值给difficulty,然后这个赋值表达式的结果是49——非零值在C++里会被当作true,所以不管你输入1、2还是1000,这个if条件都会成立,这就是为什么输入任何数字都会执行难度1的代码块。

同样,答题环节里的if (answr1 = '1914')也是一样的逻辑:赋值后表达式结果非零,不管你输入什么答案,都会直接执行“答对”的分支,这就是错误答案也显示正确的原因。

核心问题2:字符与整数的类型不匹配

  • difficulty是int类型,你却和字符'1'比较(哪怕改成==也不对),因为字符'1'的ASCII值是49,而你要判断的是用户输入的整数1,应该写成if (difficulty == 1)。
  • 另外,'1914'这种多字符常量是C++里的未定义行为,它的值完全不是你想要的整数1914。而且你的答题环节是让用户输入1或2选选项,所以应该比较的是用户输入的选项编号(比如answr1 == 2,因为正确选项是第二个),而不是和答案内容本身比较。

次要问题:变量名错误复用

看后面的答题代码:

if (answr3 = '11') { ... } // 这里应该用answr4
if (answr3 =' 2') { ... } // 这里应该用answr5

你错误地重复使用了answr3变量,而不是对应的answr4和answr5,这会导致后续的判断逻辑完全混乱。


修复后的完整代码

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
    int difficulty;
    cout << "Please choose a difficulty: 1 for easy 2 for medium 3 for hard: ";
    cin >> difficulty;

    if (difficulty == 1) { // 改用==比较整数1
        ifstream file_("questions.txt");
        if (file_.is_open()) {
            string ques1;
            getline(file_, ques1);
            cout << ques1 << endl;

            int answr1;
            cout << "Enter 1 for: 1945, Enter 2 for: 1914 \n";
            cin >> answr1;
            if (answr1 == 2) { // 输入2对应正确选项1914
                cout << "Well done you got it correct! \n";
            } else {
                cout << "Incorrect answer was 1914! \n";
            }

            string ques2;
            getline(file_, ques2);
            cout << ques2 << endl;

            int answr2;
            cout << "Enter 1 for: 1937, Enter 2 for: 1939 \n";
            cin >> answr2;
            if (answr2 == 2) { // 输入2对应正确选项1939
                cout << "Well done you got it correct! \n";
            } else {
                cout << "The answer was 1939 \n";
            }

            string ques3;
            getline(file_, ques3);
            cout << ques3 << endl;

            int answr3;
            cout << "Enter 1 for: George Bush, Enter 2 for: George Washington \n";
            cin >> answr3;
            if (answr3 == 2) { // 输入2对应正确选项
                cout << "Well done you got it correct! \n";
            } else {
                cout << "It was George Washington \n";
            }

            string ques4;
            getline(file_, ques4);
            cout << ques4 << endl;

            int answr4;
            cout << "Enter 1 for: 14, Enter 2 for: 11 \n";
            cin >> answr4;
            if (answr4 == 2) { // 改用answr4判断
                cout << "Well done you got it correct! \n";
            } else {
                cout << "It was 11 \n";
            }

            string ques5;
            getline(file_, ques5);
            cout << ques5 << endl;

            int answr5;
            cout << "Enter 1 for: She was born with 6 fingers , Enter 2 for: 6 toes \n";
            cin >> answr5;
            if (answr5 == 2) { // 改用answr5判断
                cout << "Well done you got it correct! \n";
            } else {
                cout << "It was 6 Toes \n";
            }

            file_.close();
        } else {
            cout << "File is not open\n";
        }
    }
    return 0;
}

额外注意

记得包含<string>头文件,用来存储从文件读取的问题文本;另外答题判断要和你给用户的选项严格对应,比如用户选2对应正确答案,就判断answrX == 2。

内容的提问来源于stack exchange,提问作者stcg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:51:02