-1

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 statements 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.

3 Answers3

3

This is because you're creating a variable inside the statement. std::string is a type notation (you only need it once). When you declare it. Try

if (your_choice == "math") {
Turret
  • 133
  • 8
  • Thanks yeah I realized with the above comment I should have put the == instead of one =. From what I can see so far it seems to have solved the whole boolean problem. – Jessica Alvarado Dec 12 '20 at 21:34
1

You're confusing assignment (=) with comparison (==). Try changing that line to if (subject_choice == "math") {.

-2

maybe I can help you. Try to change if(std:: string subject_choice = "math") to if(std:: string subject_choice == "math")

gnendraaw
  • 1
  • 2
  • Your version produces: `error: expected initializer before '==' token` – JaMiT Dec 12 '20 at 21:50
  • 1
    hmm, okay then. How about `if(subject_choice == "math") { ` is it still gives an error? – gnendraaw Dec 12 '20 at 22:05
  • 1
    As the one answering, it is your responsibility to see if the code in your answer compiles without errors. You might find an [online compiler](https://wandbox.org/) convenient for this task. (You can find a list of online compilers in the [c++ tag info](https://stackoverflow.com/tags/c%2b%2b/info), rather far down the page. Choose whichever you like best.) – JaMiT Dec 12 '20 at 22:14
  • 1
    okay, I appreciate it. Thanks alot for giving an advice – gnendraaw Dec 12 '20 at 22:34