Home > front end >  Why does my code ignore a large chunk of itself?
Why does my code ignore a large chunk of itself?

Time:03-01

hey I've just started on a school project but can't figure out why the code just ignores a large chunk of itself.

(also the code is part of a function inside of "math.h")

ill paste the whole segment here so that debugging it is easier.

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

I wanted it to continue on to actually calculate the question and to then check if I was right or not...

here's what I got instead:

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

and then:

EASY MODE ACTIVATED
WELL DONE!

that's all it says, despite all the other things its also supposed to do... ps. tell me if you need any more info.

CodePudding user response:

Because of type mismatch! you defined "diff" as a char and use it as such in your switch statement. However, in your second switch statement, you remove the quotes and use it as a number. That causes a char to int conversion according to the ascii table.

ASCII Table

Look at the decimal field, the number 1 as an int corresponds to a non-printable character that you will rarely see in real life. The character '1' corresponds to 49.

Solution:

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

CodePudding user response:

for this line

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

you mean

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

given that diff is a typed in char not an int

  • Related