Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

why is their an error

0 votes
asked May 3, 2020 by Ivana Ogunlaja (630 points)

#include <iostream>
using namespace std;

int main()
{
    char w
    ;cout << "Welcome to the forest adventure game.\n";
    ;cout << "You can bring one object.\n";
    ;cout << " what object do you choose enter(f) for food (m) for map and (s) for sleeping bag.\n";
    ;cin >> w;
    ;cout << "You hear a sound do you go towards it enter (y) or (n)";//y means yes n means no
    char t
    ;cin >> t;
    if(t=y){
        ;cout << "The sound gets louder and then it srikes you get killed by the lion you lose the game";
    }if(t=n){
        ;cout << "The sound gets quiter but now you are lost enter (c) to stop and shout for help enter (r) to carry on running";
    }
    return 0;
}

output:

main.cpp: In function ‘int main()’:
main.cpp:14:10: error: ‘y’ was not declared in this scope
     if(t=y){
          ^
main.cpp:16:11: error: ‘n’ was not declared in this scope
     }if(t=n){
           ^

1 Answer

0 votes
answered May 3, 2020 by LiOS (6,360 points)
To compare if it's equal, you should use == and === for data type check too. Also, for char put, the character within to compare within ' '. Also, you should implement error checking since when taking a value, you can enter anything and it will carry on. Simply, check if it's y or n else error and ask for the value again etc.

#include <iostream>
using namespace std;

int main()
{
    char w;
    cout << "Welcome to the forest adventure game.\n";
    cout << "You can bring one object.\n";
    cout << " what object do you choose enter(f) for food (m) for map and (s) for sleeping bag.\n";
    cin >> w;
    cout << "You hear a sound do you go towards it enter (y) or (n)";//y means yes n means no
    char t;
    cin >> t;
    if(t=='y'){
        cout << "The sound gets louder and then it srikes you get killed by the lion you lose the game";
    }if(t=='n'){
        cout << "The sound gets quiter but now you are lost enter (c) to stop and shout for help enter (r) to carry on running";
    }
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...