Thread: While loop not working as expected

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    101

    While loop not working as expected

    In the following loop I use a menu to select a choice from 1 to 6; 1 to 5 work as expected, ie call their appropriate functions and then go back to the menu, but when I enter 6 to end the probram I expect it to drop through to the next instructions after the while loop but the program just hangs. The menu is returning 'choice' as 6.

    Sorry it is not so much hanging as just ging back to the menu display called with 'int choice=menu()'

    Code:
    while (choice >6 || choice<6)
        {
        system ("CLS");
    
        int choice=menu();
    
        switch (choice)
            {
                case 1 : opennewfile (); break;
                case 2 : addrecord (); break;
                case 3 : deleterecord (); break;
                case 4 : readrecord (); break;
                case 5 : readwholefile (); break;
            }
    
        choice=0;
        }
    
    instructions to printout thanks and goodbye
    
    Sorry everybody I have figured it, it is the last choice=0; that is causing the problem, when I take that out it works.
    Many apologies for wasting your time.
    Last edited by JayCee++; 06-30-2012 at 05:29 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, you reset choice to 0 after every loop, so if you enter 6, by the time it comes back to the loop, it's 0 again.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. && not working as expected
    By TonyBalony in forum C Programming
    Replies: 4
    Last Post: 12-14-2011, 12:30 PM
  2. Program not working as expected
    By perrrson in forum C Programming
    Replies: 3
    Last Post: 10-02-2010, 01:49 PM
  3. Pipes not working as expected
    By Shadow_Fiend in forum C Programming
    Replies: 1
    Last Post: 05-16-2010, 05:55 AM
  4. srand() no working as expected
    By guesst in forum C++ Programming
    Replies: 2
    Last Post: 01-15-2009, 12:24 PM
  5. IntersectRect() not working as expected?
    By dxfoo in forum Windows Programming
    Replies: 1
    Last Post: 09-05-2006, 04:52 PM