Thread: Loop Query

  1. #1
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339

    Loop Query

    Hey,

    Im having a problem with the below code:

    Code:
                AddressBook addr = new AddressBook();
    
                do
                {
                    
                    menu_choice = addr.DisplayMenu();
                 ....
                 } while (cont_menu != false);
    In that code menu_choice (int) is always the same value as is set in the first time of the loop, as if the DisplayMenu() method is never called after the 1st iteration.

    However if i place AddressBook addr = new AddressBook(); inside the loop it works and displays the menu and sets the correct value of menu_choice.

    This to me didnt seem the correct way of doing it though, Becase surly creating a new object/instance to a class for every iteration of the loop is inefficent?

    Please can someone explain how this should be done.

    Thanks
    TNT
    You Can Stop Me, But You Cant Stop Us All

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    What is an AddressBook and what does DisplayMenu do ? Google didn't find anything and I don't remember such a class.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339
    Sorry i should have said, its a custom class i made, DisplayMenu() method displays a simple menu for the address book e.g add new address. The menu is shown again at the top of the loop when the requested option has been chosen and completed from the menu. I use a switch statement in the loop to flick through menu_choice and perform the desired operation.

    I have attached the full code if it helps. Im new to C#, and im just questioning if its correct or not to create a new object to the addressbook class inside every iteration of the loop.
    Last edited by (TNT); 02-12-2006 at 08:29 AM.
    TNT
    You Can Stop Me, But You Cant Stop Us All

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> while (gotchoice == false)

    you need to reinitialize the variable at the beginning of the function. in general, whenever you have an object behave as it did in your example, one of the class members is in an indeterminate state.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339
    Thanks for that it fixed the problem, I was under the impression the function would need to be static to retain its value and need reinitializing like that.
    TNT
    You Can Stop Me, But You Cant Stop Us All

  6. #6
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Im new to C#, but I was reading somewhere that is bad practice to compare ints to bools.

    If if cont_menu is only going to be used to determine if it should quit out of the loop, then change it to bool.
    My Website
    010000110010101100101011
    Add Color To Your Code!

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. While Loop Query - code displayed
    By darren78 in forum C Programming
    Replies: 13
    Last Post: 02-17-2009, 10:44 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. Stuck in a loop!.....Get me out of here!!
    By rabmaz in forum C Programming
    Replies: 3
    Last Post: 09-01-2002, 09:16 AM