samedi 12 décembre 2020

How to use string variable as if statement condition C++

I'm writing my final project in C++ for my computer class. We have to make a text adventure game so a little basics on mine: I made it so that the user has to pick from 3 subjects of learning: math, English, and science. Once that happens they go to a function for the subject choice. They have to solve 5 questions for 3 different students in the room. Pass the room, then they progress onto 2 other rooms for that subject. Once they finish all the rooms they take a "final test" for that subject.

So I made a string variable for subject choice, and I would like to use that variable in an if statement condition so that it sends the user to the corresponding subject room. I have it coded as

std::string subject_choice;

cout << "Which subject would you like to choose?" << endl;
    cout << "You can choose between: math, english, or science." << endl;
    cin >> subject_choice;
    while (subject_choice != "math" && subject_choice != "english" && subject_choice != "science")
    {
        cout << "Error: Please enter math, english, or science." << endl;
        cin >> subject_choice;

if(std:: string subject_choice = "math")
    {
        bool pOf1 = math_room1();
        if (pOf1 = true)
        {
            cout << "Congratulations! You passed the first room!" << endl;
            cout << "Now let's move to the next room!" << endl;
            bool pOf2 = math_room2();
            if (pOf2 = true)
            {
                cout << "Congratulations! You passed the second room!" << endl;
                cout << "Now let's move to the next room!" << endl;
                bool pOf3 = math_room3();
                if (pOf3 = true)
                {
                    cout << "Congratulations! You passed the final room!" << endl;
                    cout << "Now let's move on to your final test!" << endl;
                    bool pOf4 = math_final();
                    cout << "Congratulations! You passed your test! You are now a teacher!" << endl;
                    cout << "You win the game!" << endl;

With the code the way it is I get an error that highlights the first if statement that says

if(std:: string subject_choice = "math")

and the error says expression must have bool type (or be convertible to bool).

My question is: If I put

if (bool subject_choice = "math")

will it change what I'm trying to do? Will it still work? I'm kind of confused.

Aucun commentaire:

Enregistrer un commentaire