Thread: Loop not termiating

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Loop not termiating

    This sounds so silly, but I do not get why such simple code refuses to work.

    Basically I am in the middle of a very big assignment and my main menu loop isnt working. All I want to do to test it is check if the QUIT_SYSTEM works. The program compiles ok and there is no errors, ( I can post it all if you want ) but this is the part that isnt working. When I enter 4, it should read the command and exit the program. But it just continually prints the loop over and over each time I press it, even though the choice isnt true ( or 1 ).

    Can you spot what I am doing wrong? If you need the full code I can post it.

    Code:
    int Theatre::run ( void )
    {
       std::cout << "\t" << getTheatreName() << "\n\n";
       
       int choice = true;
       
       enum
       {
          PURCHASE_ON_ARRIVAL = 1,
          PURCHASE_RESERVED,
          RESERVE_TICKETS,
          QUIT_SYSTEM
       };
       
       do
       {
          std::cout << " 1 Purchase Tickets On Arrival\n"
                    << " 2 Purchase Reserved Tickets\n"
                    << " 3 Reserve Tickets\n"
                    << " 4 Exit System\n"
                    << " > ";
          std::cin >> choice;
          
          switch ( choice )
          {
          PURCHASE_ON_ARRIVAL:
             break;
             
          PURCHASE_RESERVED:
             break;
             
          RESERVE_TICKETS:
             reserveTickets();
             break;
             
          QUIT_SYSTEM:
             return 0;  // this is not executing
             break;
             
          default:
             break;
          }
       } while ( choice );      
    }
    Double Helix STL

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Strange, it looks about right to me. Try adding a
    Code:
    cout << "Exiting..." << endl;
    before return 0, and perhaps:
    Code:
    cout << "Choice = " << choice << endl;
    just after you cin >> choice.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Thanks mats but still no luck, the cout didnt even execute... this is the full code

    Code:
    #include <iostream>
    #include <cstring>
    #include <string>
    #include <vector>
    
    class Theatre
    {
    public:
       static const int MAX_ROWS = 50;
       static const int MAX_COLS = 50;
       static const int MAX_FILM = 10;
       static const int MAX_CUST = 4;
       
       Theatre();
       ~Theatre();
       
       int run ( void );
       void initalizePlan ( char[][ 50 ] );
       std::string getTheatreName() const;
       void setCustomerNames ( std::vector<std::string>& );
       void getCustomerNames ( std::vector<std::string>& ) const;
       void reserveTickets();
       
    private:
       char m_TheatrePlan[ MAX_ROWS ][ MAX_COLS ];
       std::vector<std::string> m_FilmNames;
       std::vector<std::string> m_CustomerNames;
       std::string *pm_TheatreName;
    };
    
    Theatre::Theatre() : m_FilmNames ( MAX_FILM, "" ), 
                        m_CustomerNames ( MAX_CUST, "" )
    {
       initalizePlan ( m_TheatrePlan );
       
       pm_TheatreName = new std::string("PICTO-PLEX");
    }
    
    Theatre::~Theatre()
    {
       delete pm_TheatreName;
       pm_TheatreName = NULL;
    }
    
    int Theatre::run ( void )
    {
       std::cout << "\t" << getTheatreName() << "\n\n";
       
       int choice = true;
       
       enum
       {
          PURCHASE_ON_ARRIVAL = 1,
          PURCHASE_RESERVED,
          RESERVE_TICKETS,
          QUIT_SYSTEM
       };
       
       do
       {
          std::cout << " 1 Purchase Tickets On Arrival\n"
                    << " 2 Purchase Reserved Tickets\n"
                    << " 3 Reserve Tickets\n"
                    << " 4 Exit System\n"
                    << " > ";
          std::cin >> choice;
          
          switch ( choice )
          {
          PURCHASE_ON_ARRIVAL:
             break;
             
          PURCHASE_RESERVED:
             break;
             
          RESERVE_TICKETS:
             reserveTickets();
             break;
             
          QUIT_SYSTEM:
             std::cout << "\nExiting....";
             return 0;
             break;
             
          default:
             break;
          }
       } while ( choice );      
    }
    
    void Theatre::initalizePlan ( char m_TheatrePlan[][ 50 ] )
    {
       for ( int i = 0; i < 50; i++ )
       {
          for ( int j = 0; j < 50; j++ )
             m_TheatrePlan[ i ][ j ] = 0 ;
       }
    }
    
    std::string Theatre::getTheatreName() const
    {
       return *pm_TheatreName;
    }
    
    void Theatre::setCustomerNames ( std::vector<std::string> &rm_CustomerNames )
    {
       std::string str;
       int amount = 0;
       
       std::cout << "\nHow many people are purchasing?: ";
       std::cin >> amount;
       
       for ( int i = 1; i <= amount; i++ )
       {
          std::getline ( std::cin, str );
          rm_CustomerNames.push_back ( str );
       }
    }
    
    void Theatre::getCustomerNames ( std::vector<std::string> &rm_CustomerNames ) const
    {
       std::vector<std::string>::const_iterator iter;
       
       for ( iter = rm_CustomerNames.begin(); iter != rm_CustomerNames.end(); ++iter )
       {
          std::cout << *iter << std::endl;
       }
    }
    
    void Theatre::reserveTickets()
    {
       setCustomerNames ( m_CustomerNames );
       
       int totalTickets = 0;
       
       for ( int i = 0; i < m_CustomerNames.size(); )
       {
          std::cout << "\nHow any tickets for " << m_CustomerNames[ i ]
                    << ": ";
          std::cin >> totalTickets;
          
          while ( totalTickets <= 0 )
          {
             std::cout << "\nERROR! Cannot hold a negative amount!\n\n"
                       << "How many tickets for " << m_CustomerNames[ i ]
                       << ": ";
             std::cin >> totalTickets;
          }
          i++;
       }
    }
       
    // main function - driver //////////////////////////////////////////////////////
    //
    int main ( void )
    {
       Theatre theatre;
       
       theatre.run();
       
       return 0; // return value from int main
    }
    Double Helix STL

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I would expect you should be getting a compiler warning because your don't have a "return n" at the end of your method.

    Perhaps the code the calls ::run is repeated calling ::run?

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Try adding:
    Code:
    cout << "Choice = " << choice << endl;
    after your choice.

    Edit: And add the << endl to your original printout, otherwise it may not come out until "later", since it's line buffered.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Try this:
    Code:
          switch ( choice )
          {
          case PURCHASE_ON_ARRIVAL:
             break;
             
          case PURCHASE_RESERVED:
             break;
             
          case RESERVE_TICKETS:
             reserveTickets();
             break;
             
          case QUIT_SYSTEM:
             std::cout << "\nExiting....";
             return 0;
             break;
             
          default:
             break;
          }
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Todd Burch View Post
    Try this:
    Code:
          switch ( choice )
          {
          case PURCHASE_ON_ARRIVAL:
             break;
             
          case PURCHASE_RESERVED:
             break;
             
          case RESERVE_TICKETS:
             reserveTickets();
             break;
             
          case QUIT_SYSTEM:
             std::cout << "\nExiting....";
             return 0;
             break;
             
          default:
             break;
          }
    That was a good catch. The compiler allows enum labels and goto labels to be the same name!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I stepped through it in the debugger and the SWITCH was never even executed. I observed when 0 was entered, the loop would exit. Then, I pulled out the ref. manual for SWITCH and saw the missing CASE label.

    Wish I could say it jumped out at me without opening a book!
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by Todd Burch View Post
    I stepped through it in the debugger and the SWITCH was never even executed. I observed when 0 was entered, the loop would exit. Then, I pulled out the ref. manual for SWITCH and saw the missing CASE label.

    Wish I could say it jumped out at me without opening a book!
    Heh. I compiled it and scratched my head at why it wasn't working. It's been said that the best place to hide something is in plain view. :-)

    To the OP, just a nit, but I wouldn't overload choice. Use choice to catch the input, and declare a separate bool (e.g. "finished") that signals the code to break from the loop and exit.

  10. #10
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Thanks guys.

    How I forgot the keyword "case" is beyond me! I thought it was somthing simple

    I would of assumed my compiler would of said "hey, look no case in your switch structure"
    Double Helix STL

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by swgh View Post
    Thanks guys.

    How I forgot the keyword "case" is beyond me! I thought it was somthing simple

    I would of assumed my compiler would of said "hey, look no case in your switch structure"
    Yes, but it takes it as a goto-label in this case.

    We had a post a few months ago that contained something like this:
    Code:
    case {
       ...
    
    deflaut:
       ... 
    }
    And that compiles fine too - there are very few rules as to what you must name your goto-labels. Possibly the compiler should give a warning for "unused goto label".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  3. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM