Thread: Trouble with while loops

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    89

    Trouble with while loops

    I dont know what the deal is, but I always seem to have trouble with them...I cant seem to find anything wrong with the code...I have commented where things seem to go wrong.

    CreepyManQuest.cpp

    Code:
    bool CreepyMansQuest(PLAYER* player)
    {
        Clr_Scrn();
    
        std::cout << "You find yourself standing just outside an entrance to what appears\n"
                  << "to be a ruined castle.  A relic that was probably left over from the\n"
                  << "middle war days.\n" << std::endl;
        Pause();
    
        int CastleEntrance = -1;
    
    // this while loop works...
        while(CastleEntrance != 0)
        {
            Clr_Scrn();
            std::cout << "Location: Castle Entrance\n\n" << std::endl;
            std::cout << "What would you like to do?\n"
                      << "<1> Enter the ruins.\n"
                      << "<2> View Stats\n"
                      << "<3> View Inventory\n"
                      << "<0> Leave Immediately\n"
                      << ">";
            std::cin  >> CastleEntrance;;
            std::cin.clear();
            std::cin.ignore(INT_MAX,'\n');
    
            if (CastleEntrance == 1)
            {
                if (!Battle(player, CMQGen()))
                {
                    return false;
                } // player loses battle
    
                else
                {
                    Clr_Scrn();
                    std::cout << "You are now inside the ancient castle.  All around you there are signs\n"
                              << "of war.  One that was fought centuries ago.  Just a head of you, you see\n"
                              << "what once was a door.\n" << std::endl;
                    Pause();
    
                    int ChamberOne = -1;
    
    // this while loop works for obvious reasons
                    while(ChamberOne !=0)
                    {
                        Clr_Scrn();
                        std::cout << "Location: Chamber One\n\n" << std::endl;
                        std::cout << "What would you like to do?\n"
                                  << "<1> Explore further\n"
                                  << "<2> Check Stats\n"
                                  << "<3> Check Inventory\n"
                                  << "<0> Leave Imediately\n"
                                  << ">";
                        std::cin  >> ChamberOne;
                        std::cin.clear();
                        std::cin.ignore(INT_MAX,'\n');
    
                        if (ChamberOne == 1)
                        {
                            if (!Battle(player, CMQGen()))
                            {
                                return false;
                            } // player loses
    
                            else
                            {
                                int DiningHall = -1;
    /* this one does not...it wont exit when player enters 4...Its exits the function like it 
    should when player enters 0...but take out the  return statement and it wont exit at all*/
                                while (DiningHall !=0 || DiningHall !=4)
                                {
                                    Clr_Scrn();
                                    std::cout << "Location: Dining Hall\n\n" << std::endl;
                                    std::cout << "You now stand in what seems to be the dining hall.\n"
                                            << "The walls on three sides of the chamber have caved in\n"
                                            << "Leaving you three differnt exits.\n"
                                            << "<1> Go Left\n"
                                            << "<2> Go Right\n"
                                            << "<3> Go Forward\n"
                                            << "<4> Go Back\n"
                                            << "<5> Check Stats\n"
                                            << "<6> Check Inventory\n"
                                            << "<0> Run Away\n"
                                            << ">";
                                    std::cin  >> DiningHall;
                                    std::cin.clear();
                                    std::cin.ignore(INT_MAX,'\n');
    
                                    switch (DiningHall)
                                    {
                                        case 1:
                                        {
                                            int ChamberFour = -1;
    
                                           // this loop wont exit at all no matter what you enter
                                            while (ChamberFour != 2 || ChamberFour != 0)
                                            {
                                                Clr_Scrn();
                                                std::cout << "Location: Chamber Four\n\n\n\n\n" << std::endl;
                                                std::cout << "<1> Go Right\n"
                                                          << "<2> Go Back\n"
                                                          << "<3> Check Stats\n"
                                                          << "<4> Check Inventory\n"
                                                          << ">";
                                                std::cin  >> ChamberFour;
                                                std::cin.clear();
                                                std::cin.ignore(INT_MAX,'\n');
    
                                                if (ChamberFour == 1)
                                                {
                                                    if (!Battle(player, CMQGen()))
                                                    {
                                                        return false;
                                                    } // player loses exit quest
    
                                                    else
                                                    {
                                                        int ChamberSeven = -1;
                                                    } // player wins and moves on
                                                } // player enter battle
                                            } // end chamber four loop, player goes back to the dining hall
                                            break;
                                        } // player enter chamber four
    
                                        case 2:
                                        {
                                            int ChamberThree = -1;
                                            while (ChamberThree != 1)
                                            {
                                            } // end chamber four loop, player goes back to dining hall
                                            break;
                                        } // player enters chamber three
    
                                        case 3:
                                        {
                                            int ChamberSix = -1;
                                            while (ChamberSix != 2)
                                            {
                                            } // end chamber six loop, players goes back to dining hall
                                            break;
                                        } // player enters chamber six
    
                                        case 4:
                                        {
                                            std::cout << "You turn around and head back to the first chamber.\n" << std::endl;
                                            Pause();
                                            break;
                                        } // player goes back to chamber one
    
                                        case 5:
                                        {
                                            player->GetStats();
                                            Pause();
                                            break;
                                        } // show player stats
    
                                        case 6:
                                        {
                                            player->PlayerInventory();
                                            Pause();
                                            break;
                                        } // show player inventory
    
                                        case 0:
                                        {
                                            std::cout << "You run away!\n" << std::endl;
                                            Pause();
                                            return false;
                                            break;
                                        } // player runs away
    
                                        default:
                                        {
                                            std::cout << "Invalid Selection, please try agian!\n" << std::endl;
                                            Pause();
                                            break;
                                        }
                                    } // End Dining Hall switch statement
                                } // end Dining Hall while loop
                            } // player wins and moves on
                        } // player enters battle
    
                        else if (ChamberOne == 2)
                        {
                            player->GetStats();
                            Pause();
                        } // show player stats
    
                        else if (ChamberOne == 3)
                        {
                            player->PlayerInventory();
                            Pause();
                        } // show player inventory
    
                        else if (ChamberOne == 0)
                        {
                            std::cout << "You run away!!|n" << std::endl;
                            Pause();
                            return false; 
                        } // player runs away
    
                        else
                        {
                            std::cout << "Invalid Selection, please try agian.\n" << std::endl;
                            Pause();
                        } // wrong selection
                    } // end first nested while loop
                } // player wins and moves on
            } // player decides to enter casle
    
            else if (CastleEntrance == 2)
            {
                player->GetStats();
                Pause();
            } // show player stats
    
            else if (CastleEntrance == 3)
            {
                player->PlayerInventory();
                Pause();
            } // show player inventory
    
            else if (CastleEntrance == 0)
            {
                std::cout << "You run away!\n" << std::endl;
                Pause();
                return false;
            } // player runs away
    
            else
            {
                std::cout << "Invalid Selection, please try agian.\n" << std::endl;
                Pause();
            }
        } // end while loop
    
        return true;
    }
    I guess i could go the easy route and use goto statements...but from reading tutorials on this site and others i guess that is not considered good programming...so really dont want to do that.
    Yeah its a horrible story line lol

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> while (DiningHall !=0 || DiningHall !=4)
    This will always evaluate to true. No matter what value DiningHall is, it will always be either not equal to 0 or not equal to 4.

    You want the loop to run while DiningHall does not equal 0 and DiningHall does not equal 4.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    [quote] while (DiningHall !=0 || DiningHall !=4)[/code]
    Think logically. In text, the above line says:
    "While DiningHall is not 0 or DiningHall is not 4, execute the body."

    There's a flaw in that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    89

    Red face

    thanks...
    I was going about backwards...I think staring at the code all day long put my mind in a loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. having trouble using loops
    By wolverine12345 in forum C++ Programming
    Replies: 34
    Last Post: 11-28-2007, 06:30 PM
  4. a little trouble with for loops
    By melee in forum C Programming
    Replies: 6
    Last Post: 10-19-2004, 01:46 AM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM