Endless loop!

This is a discussion on Endless loop! within the C Programming forums, part of the General Programming Boards category; Why this do-while loop never ends?? even if I type s or n, it doesnt end and return the value ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    75

    Endless loop!

    Why this do-while loop never ends?? even if I type s or n, it doesnt end and return the value of eleccion.

    char otravez()
    {
    char eleccion;

    do{
    printf("Jugar otra vez?(s/n)");
    eleccion=getche();
    }while(eleccion!='s'||eleccion!='n');

    return eleccion;
    }
    ---Programming is like roaming, you never know where you'll end at------

    If 'here' is pronounced as 'hear', why 'there' isnt pronounced as 'dear'??

    paninaro3000@yahoo.com

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    while(eleccion!='s'||eleccion!='n');

    This line should be using the logical AND operator

    while( eleccion != 's' && eleccion != 'n' );

    What you are saying is while character doesn't equal s or doesn't equal n continue. Well one of those is always going to be true, therefore OR lets one condition true and one false = true in the end. AND requires both conditions be true to return true. Hope that makes sense.

  3. #3
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172
    Hi, try this:
    Code:
    char otravez() 
    { 
            char eleccion; 
    
            do{ 
                    printf("Jugar otra vez?(s/n)"); 
                    eleccion=getchar(); 
            }while(eleccion='s'|| eleccion='n'); 
    
    return eleccion; 
    }
    This would end the loop if 's' or 'n' is inputted. Someone correct me if i'm wrong so as i may learn.
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by loopy
    Hi, try this:
    Code:
    char otravez() 
    { 
            char eleccion; 
    
            do{ 
                    printf("Jugar otra vez?(s/n)"); 
                    eleccion=getchar(); 
            }while(eleccion='s'|| eleccion='n'); 
    
    return eleccion; 
    }
    This would end the loop if 's' or 'n' is inputted. Someone correct me if i'm wrong so as i may learn.
    Well first of all in your while condition you are assigning the variable eleccion to the character s and n. You need to equal signs for comparison ( == ). If you add those you are saying if your choice equals s OR your choice equals n , then continue. I don't think that is what he wants.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fscanf endless loop
    By krum in forum C Programming
    Replies: 3
    Last Post: 12-04-2007, 11:14 PM
  2. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 02:48 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 04:39 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. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM

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