Help Debugging

Pages: 12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;
int main()

{
   char response;

   cout<<"You enter a dark hallway deep under ground..."<<endl;
   cout<<"/////////////////////////////////////////////"<<endl;

 {
    cout<<"Your flashlight begins to flicker, would you like to continue or go back?"<<endl;
    cout<<"/////////////////////////////////////////////////////////////////////////"<<endl;
    cin>>response;

    if (response == 'go back')
{
      cout<<"The door you came from is blocked, you must continue..."<<endl;
      cout<<endl;
     {
      cout<<"You continue walking for some time, you soon reach a split..."<<endl;
      cout<<"Will you go left or right?"<<endl;
      cin>>response;
     }
       if (response == 'left ')
      {
          cout<<"You walk down the left side only to find a dead end, and return to where you came from..."<<endl;
          cout<<"/////////////////////////////////////////////////////////////////////////////////////////"<<endl;
      }
     }
    }
   }


Whenever i compile this program it registers my if statement, and when i type in the response it dosent register and closes my program how could i fix this?
Last edited on
A char is just that - one character, like 'a' or '@'.
You are trying to get the user to enter a specific response, which requires multiple characters, otherwise known as a string.

Solution: Use std::string instead.

Also, the >> operator won't work with spaces, so you'll have to use getline() instead.

Notice the double quotation marks as opposed to the single quotes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
using namespace std;
int main()
{
    std::string response;
    
    cout<<"Your flashlight begins to flicker, would you like to continue or go back?"<<endl;
    cout<<"/////////////////////////////////////////////////////////////////////////"<<endl;
    getline(cin, response);

    if (response == "go back")
    {
        cout<<"The door you came from is blocked, you must continue..."<<endl;
        cout<<endl;

        cout<<"You continue walking for some time, you soon reach a split..."<<endl;
        cout<<"Will you go left or right?"<<endl;
        getline(cin, response);

        if (response == "left")
        {
            cout << "etc" << std::endl;
        }
    }
}
Last edited on
OHHHH, thanks very much i didnt know that char was only used in a single character type thing... Thanks for the help!!
Yep you're welcome
Okay so i put what you had entered and its giving me some bogus errors:
||=== Build: Debug in RolePlayTypeGame (compiler: GNU GCC Compiler) ===|
D:\My Documents\RolePlayTypeGame\main.cpp||In function 'int main()':|
D:\My Documents\RolePlayTypeGame\main.cpp|7|error: 'cout' was not declared in this scope|
D:\My Documents\RolePlayTypeGame\main.cpp|7|error: 'endl' was not declared in this scope|
D:\My Documents\RolePlayTypeGame\main.cpp|9|error: 'cin' was not declared in this scope|
D:\My Documents\RolePlayTypeGame\main.cpp|13|error: 'endl' is not a member of 'std'|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
A good programming practice is instead of using namespace std; explicitly saying std::

for example.
 
std::cout << "Hello World!" << std::endl;


You can read all about why on google.
So, what you saying is that i just need to use std:: instead of using namespace std; and it should be working?
Yes. cout, endl and cin are part of std:: (don't forget the cin inside the getlines).

If it for some reason does not, please post your new code and new errors and I'll take a look :)
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <string>
#include <iostream>
int main()
{
    std::string response;

    std::cout<<"Your flashlight begins to flicker, would you like to continue or go back?"<<std::endl;
    std::cout<<"/////////////////////////////////////////////////////////////////////////"<<std::endl;
    getline(std::cin, response);

    if (response == "go back")
    {
        std::cout<<"The door you came from is blocked, you must continue..."<<std::endl;
        std::cout<<std::endl;

        std::cout<<"You continue walking for some time, you soon reach a split..."<<std::endl;
        std::cout<<"Will you go left or right?"<<std::endl;
        getline(std::cin, response);

        if (response == "left")
        {
            std::cout<<"etc"<<std::endl;
        }
    }
}

its now working thanks very much
What Tarik said is true and a good practice, but either way, my code should have compiled. Are you sure you copied the first line, #include <iostream> ? Your errors would suggest that you didn't.

Edit: Saw your post, glad it's working.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <string>
#include <iostream>
int main()
{
    std::string response;

    std::cout<<"Your flashlight begins to flicker, would you like to continue or go back?"<<std::endl;
    std::cout<<"/////////////////////////////////////////////////////////////////////////"<<std::endl;
    getline(std::cin, response);

    if (response == "go back")
    {
        std::cout<<"The door you came from is blocked, you must continue..."<<std::endl;
        std::cout<<std::endl;

        std::cout<<"You continue walking for some time, you soon reach a split..."<<std::endl;
        std::cout<<"Will you go left or right?"<<std::endl;
        getline(std::cin, response);

        if (response == "left")

        {
            std::cout<<"You continue to walk to run into a steel door, you hear noises behind it!"<<std::endl;
            std::cout<<"You quickly turn around and run!
            getline (std::cin, response) ;
        else if (response == "right")
        {
         std::cout<<"You see a light and the end of the hallway, but hear something approaching from behind you..."<<std::endl;

            std::cout<<"You stop in the middle of the hallway..."<<std::endl;
            std::cout<<"You can either turn around, or continue walking..."<<std::endl;
            getline (std::cin, response);
            if (response == "turn around")

            {
             std::cout<<"You turn around and see a large shadow, you begin to faint and your heart beat increases, and your arm begins to hurt."<<std::endl;
             std::cout<<"You wake up in a cold sweat and realize it was a nightmare..."<<std::endl;
             std::cout<<"The End"<<std::endl;
             getline (std::cin, response);
             else if (responce == "Continue Walking")

             {
              std::cout<<"You continue walking a reach a small wooden door."<<std::endl;
              std::cout<<"You are able to kick the door down, and inside is a large room with many doors"<<std::endl;
             }
            }
        }
        }
    }
} 


i keep getting :
||=== Build: Debug in RolePlayTypeGame (compiler: GNU GCC Compiler) ===|
D:\My Documents\RolePlayTypeGame\main.cpp|24|warning: missing terminating " character [enabled by default]|
D:\My Documents\RolePlayTypeGame\main.cpp|24|error: missing terminating " character|
D:\My Documents\RolePlayTypeGame\main.cpp||In function 'int main()':|
D:\My Documents\RolePlayTypeGame\main.cpp|26|error: expected '}' before 'else'|
D:\My Documents\RolePlayTypeGame\main.cpp|40|error: expected '}' before 'else'|
D:\My Documents\RolePlayTypeGame\main.cpp|40|error: 'responce' was not declared in this scope|
D:\My Documents\RolePlayTypeGame\main.cpp|49|error: expected declaration before '}' token|
||=== Build failed: 5 error(s), 1 warning(s) (0 minute(s), 1 second(s)) ===|
how can i fix these and prevent myself from getting them in the future?
1
2
3
4
5
6
7
8
9
 if (response == "left")

        {
            std::cout<<"You continue to walk to run into a steel door, you hear noises behind it!"<<std::endl;
            std::cout<<"You quickly turn around and run! // You're missing the " and also the ;
            getline (std::cin, response) ;
        // Here you are missing the closing bracket for the if statement .Add }
        else if (response == "right")
        {


how can i fix these and prevent myself from getting them in the future?

You can read what the errors are telling you and try to understand them, they tell you where the problem is.

To avoid them in the first place, since they are silly mistakes, is just to be more careful.

For every opening bracket there must be exactly one closing bracket. Lines of codes need to end with semicolon aswell.
Last edited on
i think i dug myself into a deeper hole trying to resolve the errors above so i appreciate the help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <string>
#include <iostream>
int main()
{
    std::string response;

    std::cout<<"Your flashlight begins to flicker, would you like to continue or go back?"<<std::endl;
    std::cout<<"/////////////////////////////////////////////////////////////////////////"<<std::endl;
    getline(std::cin, response);

    if (response == "go back")
    {
        std::cout<<"The door you came from is blocked, you must continue..."<<std::endl;
        std::cout<<std::endl;

        std::cout<<"You continue walking for some time, you soon reach a split..."<<std::endl;
        std::cout<<"Will you go left or right?"<<std::endl;
        getline(std::cin, response);

        if (response == "left")
    }
        {
            std::cout<<"You continue to walk to run into a steel door, you hear noises behind it!"<<std::endl;
            std::cout<<"You quickly turn around and run!
            getline (std::cin, response) ;
        else if (response == "right")
        }
        {
         std::cout<<"You see a light and the end of the hallway, but hear something approaching from behind you..."<<std::endl;

            std::cout<<"You stop in the middle of the hallway..."<<std::endl;
            std::cout<<"You can either turn around, or continue walking..."<<std::endl;
            getline (std::cin, response);
            if (response == "turn around")
        }
            {
             std::cout<<"You turn around and see a large shadow, you begin to faint and your heart beat increases, and your arm begins to hurt."<<std::endl;
             std::cout<<"You wake up in a cold sweat and realize it was a nightmare..."<<std::endl;
             std::cout<<"The End"<<std::endl;
             getline (std::cin, response);
             else if (responce == "Continue Walking")
            }
             {
              std::cout<<"You continue walking a reach a small wooden door."<<std::endl;
              std::cout<<"You are able to kick the door down, and inside is a large room with many doors"<<std::endl;
             }
 


||=== Build: Debug in RolePlayTypeGame (compiler: GNU GCC Compiler) ===|
D:\My Documents\RolePlayTypeGame\main.cpp|24|warning: missing terminating " character [enabled by default]|
D:\My Documents\RolePlayTypeGame\main.cpp|24|error: missing terminating " character|
D:\My Documents\RolePlayTypeGame\main.cpp||In function 'int main()':|
D:\My Documents\RolePlayTypeGame\main.cpp|21|error: expected primary-expression before '}' token|
D:\My Documents\RolePlayTypeGame\main.cpp|21|error: expected ';' before '}' token|
D:\My Documents\RolePlayTypeGame\main.cpp|26|error: 'else' without a previous 'if'|
D:\My Documents\RolePlayTypeGame\main.cpp|27|error: expected primary-expression before '}' token|
D:\My Documents\RolePlayTypeGame\main.cpp|27|error: expected ';' before '}' token|
D:\My Documents\RolePlayTypeGame\main.cpp|35|error: expected primary-expression before '}' token|
D:\My Documents\RolePlayTypeGame\main.cpp|35|error: expected ';' before '}' token|
D:\My Documents\RolePlayTypeGame\main.cpp|41|error: 'else' without a previous 'if'|
D:\My Documents\RolePlayTypeGame\main.cpp|41|error: 'responce' was not declared in this scope|
D:\My Documents\RolePlayTypeGame\main.cpp|42|error: expected primary-expression before '}' token|
D:\My Documents\RolePlayTypeGame\main.cpp|42|error: expected ';' before '}' token|
D:\My Documents\RolePlayTypeGame\main.cpp|46|error: expected '}' at end of input|
||=== Build failed: 13 error(s), 1 warning(s) (0 minute(s), 1 second(s)) ===|

i tried to add the '}' and these are what i got, theres no missing ';' as it says many times in the errors above

sorry for the inconvenience before hand
Last edited on
Your errors are very obvious and Ive pointed most of them out. Your curly braces are out of place, look through your code carefully.
You are right Tarik, i resolved almost all the errors, when i went to compile im now getting :



||=== Build: Debug in RolePlayTypeGame (compiler: GNU GCC Compiler) ===|
D:\My Documents\RolePlayTypeGame\main.cpp|11|error: expected unqualified-id before 'if'|
D:\My Documents\RolePlayTypeGame\main.cpp|20|error: expected unqualified-id before 'if'|
D:\My Documents\RolePlayTypeGame\main.cpp|27|error: expected unqualified-id before 'else'|
D:\My Documents\RolePlayTypeGame\main.cpp|36|error: expected unqualified-id before 'if'|
D:\My Documents\RolePlayTypeGame\main.cpp|44|error: expected unqualified-id before 'else'|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|

and i have no clue as to what it is referring too
Probably this:


1
2
3
4
5
6
7
8
9
10
11
12
    if (response == "go back")
    {
        std::cout<<"The door you came from is blocked, you must continue..."<<std::endl;
        std::cout<<std::endl;

        std::cout<<"You continue walking for some time, you soon reach a split..."<<std::endl;
        std::cout<<"Will you go left or right?"<<std::endl;
        getline(std::cin, response);

        if (response == "left")
    }
        {


Why is there a closing bracket right after you make an if statement? There should be an opening bracket after that.

If statements look like this

1
2
3
4
if()
{

}


Which means your brackets are misplaced.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <string>
#include <iostream>
int main()
{
    std::string response;

    std::cout<<"Your flashlight begins to flicker, would you like to continue or go back?"<<std::endl;
    std::cout<<"/////////////////////////////////////////////////////////////////////////"<<std::endl;
    getline(std::cin, response);
}
    if (response == "go back")
    {
        std::cout<<"The door you came from is blocked, you must continue..."<<std::endl;
        std::cout<<std::endl;

        std::cout<<"You continue walking for some time, you soon reach a split..."<<std::endl;
        std::cout<<"Will you go left or right?"<<std::endl;
        getline(std::cin, response);
    }
        if (response == "left")

        {
            std::cout<<"You continue to walk to run into a steel door, you hear noises behind it!"<<std::endl;
            std::cout<<"You quickly turn around and run!"<<std::endl;
            getline (std::cin, response) ;
        }
        else if (response == "right")

        {
         std::cout<<"You see a light and the end of the hallway, but hear something approaching from behind you..."<<std::endl;

            std::cout<<"You stop in the middle of the hallway..."<<std::endl;
            std::cout<<"You can either turn around, or continue walking..."<<std::endl;
            getline (std::cin, response);
        }
            if (response == "turn around")

            {
             std::cout<<"You turn around and see a large shadow, you begin to faint and your heart beat increases, and your arm begins to hurt."<<std::endl;
             std::cout<<"You wake up in a cold sweat and realize it was a nightmare..."<<std::endl;
             std::cout<<"The End"<<std::endl;
             getline (std::cin, response);
            }
             else if (responce == "Continue Walking")

             {
              std::cout<<"You continue walking a reach a small wooden door."<<std::endl;
              std::cout<<"You are able to kick the door down, and inside is a large room with many doors"<<std::endl;
             }
Your entire program is outside your main. Get it back in there :D
What do you mean its outside my main?
I mean it is literally outside of main.

1
2
3
4
5
6
int main()
{
    //Here is inside main
}

//Your program is here 


I'll only advise you one more time. Please learn how to debug, and take a good look at your program, you won't learn otherwise.
Last edited on
Pages: 12