为什么我的代码忽略了它自己的一大块?

Why does my code ignore a large chunk of itself?

嘿,我刚刚开始一个学校项目,但无法弄清楚为什么代码只是忽略了它自己的一大块。

(代码也是“math.h”中函数的一部分)

我将整个片段粘贴到此处以便调试它。

#include <iostream>
#include <chrono>
#include <thread>
#include <algorithm>

using namespace std;
using namespace chrono;
using namespace this_thread;


class math {
private:
    unsigned short pts;

public: 
    unsigned short questAmount;
    char diff;
    short arrEasy[4] = {1, 2, 5, 10}, secondVar, ans, studAns;

    void questProc() {
        start:
        cout << "How hard do you want the questtions to be?" << endl; 
        cout << "(The harder you choose, the larger the numbers you'll have to calculate)" << endl;
        cout << "1) EASY" << endl;
        cout << "2) MEDIUM" << endl;
        cout << "3) HARD" << endl;
        cout << "4) CALCULATOR MODE LOL" << endl << "~> ";
        cin >> diff;

        switch (diff) {
        case '1':
            system("cls");
            cout << "EASY MODE ACTIVATED" << endl;
            break;

        case '2':
            system("cls");
            cout << "MEDIUM MODE ACTIVATED" << endl;
            break;

        case '3':
            system("cls");
            cout << "HARD MODE ACTIVATED" << endl;
            break;

        case '4':
            system("cls");
            cout << "CALCULATOR MODE ACTIVATED" << endl;
            break;

        default:
            system("cls");
            cout << "INPUTED VAL IS EITHER NOT AN INT OR ISN'T IN THE LIST"; //checks only the starting int ignores the rest...
            sleep_for(0.5s); cout << "."; sleep_for(0.5s); cout << "."; sleep_for(0.5s); cout << "."; sleep_for(0.5s);
            break;

            if (diff != 1 && diff != 2 && diff != 3 && diff != 4) { goto start; }
        }

        // The following creates the question according to the set difficulty.
        srand(time(NULL)); // Initializing random seed.
        switch (diff) {
        case 1: // Easy Mode:
            system("cls");
            random_shuffle(&arrEasy[0], &arrEasy[4]); // For first var.
            secondVar = rand() % 10 + 1; // Rand number from 1 to 10 (for second var).
            ans = arrEasy[0] * secondVar;
            cout << arrEasy[0] << " * " << secondVar << " = ?" << endl << "~> ";
            cin >> studAns;
            break;

        default:
            break;
        }

        // The following checks if ans is correct.

        if (studAns == ans) {
            cout << "WELL DONE!";
        }
        else {
            cout << "WRONG, CORRECT ANSWER WAS:" << ans;
        }
    }
};

我希望它继续实际计算问题,然后检查我是否正确...

这是我得到的:

How hard do you want the questtions to be?
(The harder you choose, the larger the numbers you'll have to calculate)
1) EASY
2) MEDIUM
3) HARD
4) CALCULATOR MODE LOL
~> 1

然后:

EASY MODE ACTIVATED
WELL DONE!

这就是它所说的全部,尽管它还应该做所有其他事情...... ps。如果您需要更多信息,请告诉我。

因为类型不匹配! 您将“diff”定义为 char 并在 switch 语句中使用它。但是,在您的第二个 switch 语句中,您删除了引号并将其用作数字。这会根据 ascii table.

将 char 转换为 int

看小数域,作为int的数字1对应了一个non-printable现实生活中很少见的字符。字符'1'对应49.

解决方案:

switch(diff){
case '1':   //notice the quotations
   //rest of code
   break;
}

这条线

if (diff != 1 && diff != 2 && diff != 3 && diff != 4) { goto start; }

你是说

if (diff != '1' && diff != '2' && diff != '3' && diff != '4') { goto start; }

假设 diff 是 char 类型而不是 int