mercredi 23 mai 2018

How to check if user input is a 1 or a 2

I am trying to ask the user to enter a difficulty but when i use the if statement to check if the difficulty is 1 and i enter something else rather than 1 it just continues with the code. Same with the answers for the questions.

For example when i run the code and it asks me for my difficulty and i enter a 2 or 1000 it still proceeds with the code. Same thing with the answers it just displays you got it correct even if i enter 1945 for the first question which is wrong.

Any help is greatly appreciated!

code:

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

if (difficulty = '1')
{
    ifstream file_("questions.txt");
    if (file_.is_open())
    {
        std::getline(file_, ques1);
        cout << ques1 << std::endl;
        int answr1;
        cout << "Enter 1 for: 1945, Enter 2 for: 1914 \n";
        cin >> answr1;
        if (answr1 = '1914')
        {
            cout << "Well done you got it correct! \n";
        }
        else {
            cout << "Incorrect answer was 1914! \n";
        }

        std::getline(file_, ques2);
        cout << ques2 << std::endl;
        int answr2;
        cout << "Enter 1 for: 1937, Enter 2 for: 1939 \n";
        cin >> answr2;
        if (answr2 = '1939')
        {
            cout << "Well done you got it correct! \n";

        }
        else {
            cout << "The answer was 1939 \n";
        }

        std::getline(file_, ques3);
        cout << ques3 << std::endl;
        int answr3;
        cout << "Enter 1 for: George Bush, Enter 2 for: George Washington \n";
        cin >> answr3;
        if (answr3 = '2')
        {
            cout << "Well done you got it correct! \n";
        }
        else {
            cout << "It was George Washington \n";
        }

        std::getline(file_, ques4);
        cout << ques4 << std::endl;
        int answr4;
        cout << "Enter 1 for: 14, Enter 2 for: 11 \n";
        cin >> answr4;
        if (answr3 = '11')
        {
            cout << "Well done you got it correct! \n";
        }
        else {
            cout << "It was 11 \n";
        }

        std::getline(file_, ques5);
        cout << ques5 << std::endl;
        int answr5;
        cout << "Enter 1 for: She was born with 6 figners , Enter 2 for: 6 toes \n";
        cin >> answr5;
        if (answr3 =' 2')
        {
            cout << "Well done you got it correct! \n";
        }
        else {
            cout << "It was 6 Toes \n";
        }

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

}

Aucun commentaire:

Enregistrer un commentaire