Help! How do I fix this problem in my program?

This is a discussion on Help! How do I fix this problem in my program? within the C++ Programming forums, part of the General Programming Boards category; Code: int main() { char userChoice; int userNumber; char N; char S; char E; char W; char continueChoice; cout<< "Welcome ...

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    11

    Help! How do I fix this problem in my program?

    Code:
    int main()
    {
        char userChoice;
        int userNumber;
        char  N;
        char  S;
        char  E;
        char  W;
        char continueChoice;
        cout<< "Welcome to the game!"<< endl;  
        cout<< "You just woke up with amnesia" <<endl;
        cout<< "Dizzy and tired, you look around" <<endl;
        cout<< "You are in a forest" <<endl;
        cout<< "A sign says Beware! Leave this forest A.S.A.P!!!" <<endl;
        do    
        {    
            cout<< "You have " << health << " health points" <<endl;
            cout<< "What direction do you want to walk to?" << endl;
            cout<< "(Press N, S, E, or W to move North, South, East, or West)" <<endl;
            cin>> userChoice;
            if ((userChoice == 'N') || (userChoice == 'n'))
            {   
                userNumber = 1;
            }
            else if ((userChoice == 'S') || (userChoice == 's'))
            {
                userNumber = 2;
            }
            else if ((userChoice == 'E') || (userChoice == 'e'))
            {
                userNumber = 3;
            }
            else if ((userChoice == 'W') || (userChoice == 'w'))
            {
                userNumber = 4;     
            }    
            switch (userNumber)
            {        
                case 1:
                {
                    RmOne(userNumber);
                }
                break;
                case 2:
                {
                    RmTwo(userNumber);
                }
                break;
                case 3:
                {   
                    RmThree(userNumber);
                }
                break;
                case 4:
                {
                    RmFour(userNumber);
                }
                break;
                default:
                cout<< userChoice << " is not a valid choice" <<endl;
                break;
            } 
        cout<< "Do you want to continue searching for a way out of the forest? (Press Y to continue, N to quit)" <<endl;
        cin>> continueChoice;
        }
        while ((continueChoice=='Y') || (continueChoice=='y'));
    
        return 0;
    }
    The first time I loop, any key other than NSEW will bring me to the first userChoice that I inputted.

    For example, if I start the program from the beginning and my userChoice is N, it'll run correctly and when I select Y to continue program and M as my next userChoice, it'll go to my case 1 rather than the default.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,650
    Code:
    /*
    main.cpp||In function 'int main()':|
    main.cpp|21|error: 'health' was not declared in this scope|
    main.cpp|45|error: 'RmOne' was not declared in this scope|
    main.cpp|50|error: 'RmTwo' was not declared in this scope|
    main.cpp|55|error: 'RmThree' was not declared in this scope|
    main.cpp|60|error: 'RmFour' was not declared in this scope|
    main.cpp|9|warning: unused variable 'N'|
    main.cpp|10|warning: unused variable 'S'|
    main.cpp|11|warning: unused variable 'E'|
    main.cpp|12|warning: unused variable 'W'|
    ||=== Build finished: 5 errors, 4 warnings ===|
    
    */
    For starters, "health", "RmOne()", "RmTwo()", "RmThree()", and "RmFour()" are not declared anywhere that we can see.

    For example, if I start the program from the beginning and my userChoice is N, it'll run correctly and when I select Y to continue program and M as my next userChoice, it'll go to my case 1 rather than the default.
    So you enter 'N'. "userNumber" will be equal to 1. The "switch()" will execute "RmOne()". The user selects continue. The user enters 'M' (invalid). It will skip the check for 'N' (line 21), for 'S' (line 25), for 'E' (line 29), and for 'W' (line 33). At this point, "userNumber" remains unchanged (is still equal to 1). You run the "switch()" again ... and guess what happens!

    Besides that, you're running two separate logic flows that can easily be combined into one - especially considering the "fall-through" characteristic of "switch()".

    Code:
    get input
    switch(input)
        case 'n':
        case 'N':
            // do stuff
            break;
        case 's':
        case 'S':
            // do stuff
            break;
        case 'w':
        case 'W':
            // do stuff
            break;
        case 'e':
        case 'E':
            // do stuff
            break;
        default:
            // error

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    11
    Sorry, I forgot to paste in my void functions. But I figured out my problem so thanks for replying anyhow! :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Program
    By slick00_2 in forum C++ Programming
    Replies: 5
    Last Post: 09-22-2010, 02:20 AM
  2. Replies: 4
    Last Post: 10-16-2008, 08:30 PM
  3. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 12:53 PM
  4. Problem with a program...
    By brian85 in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2005, 01:19 PM
  5. Program Problem
    By Budgiekarl in forum C Programming
    Replies: 6
    Last Post: 05-06-2003, 06:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21