Problem with looping a "switch" in C programming

This is a discussion on Problem with looping a "switch" in C programming within the C Programming forums, part of the General Programming Boards category; Hello and greetings to everyone. (I'm new here) Anyways......... I have a problem with my coding in C. What I'm ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    5

    Unhappy Problem with looping a "switch" in C programming

    Hello and greetings to everyone. (I'm new here)
    Anyways.........
    I have a problem with my coding in C.

    What I'm trying to do is to let a person pick a number between 1 to 3. (the switch function checks your answer and tells you what you have picked)

    Then, if the answer is not 1, 2, or 3, the program loops and asks again.

    Here is my code
    Code:
    #include <stdio.h>
    
    int main()
    {
    int a, b, c;
    
    printf("Pick between 1, 2 or 3\n");
    printf("INPUT A NUMBER:");
    scanf("%d", &a);
    
    while((a!=1)||(a!=2)||(a!=3))
    {
       switch (a)
       {
          case 1:
          printf("You picked 1");
          break;
    
          case 2:
          printf("You picked 2");
          break;
    
          case 3:
          printf("You picked 3");
          break;
    
          default:
          printf("Your answer is not valid");
          break;
       } 
    
    
    }
    
    getchar();
    
    }
    Here's what happens when the program is run.
    If you input 1, the line "You picked 1" appears infinitely. (this also happens with 2, 3 with their respective answers)
    Finally, if i type in any number (which will lead to default) it shows the line "Your answer is not valid"
    Can anyone point out to me what went wrong with my code?
    I also tried to do the same thing without using the "switch" function

    Code:
    #include <stdio.h>
    
    int main()
    {
    int a, b, c;
    
    printf("Pick a number from 1, 2 or 3\n");
    printf("Input a number:");
    scanf("%d", &a);
    
    while(a != 1) /* ,a != 2, a != 3*/
       {
          printf("INPUT AGAIN:");
          scanf("%d", &a);
       }
    
    if(a = 1)
    {
       printf("You inputted 1");
    }
    else if(a = 2)
    {
       printf("You inputted 2");
    }
    else if(a = 3)
    {
       printf("You inputted 3");
    }
    
    }
    but whenever i put the things in the comment box (beside the while function)
    the program doesnt work properly.

    I would really appreciate it if someone could point out my mistakes because i wanna get creative and improve my programming. Thanks.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Look at the condition for your while loop. You're telling it to loop for as long as a != 1, or a != 2, or a != 3. One of those is always going to be true since a can't be 1, 2 and 3 at the same time. Besides, how are you ever going to break out of the while loop if you never change the value of a once you enter the loop?
    Last edited by Memloop; 06-22-2010 at 10:33 AM.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    while((a!=1)||(a!=2)||(a!=3))
    This condition will always be true, since if a == 1 of those numbers, it does not equal the other two. You should use && instead of ||. That way, if "a" equals one number, the condition will not be true (AND requires all conditions be met, OR requires only one).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Think like this:
    What is the condition for the looping? What do you want it to do?
    You want to loop AS LONG as the input is invalid.
    When is the input invalid?
    As long as the input is NOT 1 and NOT 2 and NOT 3.

    Or if you want, here is an easier way not using inverted logic.
    When is the input right?
    It's correct if it's one of the following: 1, 2, 3.
    That is, if input is 1 or 2 or 3.
    Now, invert that logic: !(input == 1 || input == 2 || input == 3) and you basically get the inverted logic. That is, when the input is not right.
    Now you can go in and change the expressions inside if you want. Every equal becomes inequal (!=) and every or becomes and.
    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.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Your second example didn't work because this:
    Code:
    if(a = 1)
    assigns 1 to a.
    You wanted ==
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    27
    Select the do..while loop will be ok!

    Code:
    int flag=0;
    do
    {
        ...........
        if(a!=1&&a!=2&&a!=3)
          flag=1;
       else
         flag=0;
    }
    while(flag);

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Doing the check in the if statement is the same as doing it in the actual while segment. You don't actually gain anything there, unless you just happen be checking to do something for a special case inside the loop anyway. As mentioned by others, the main thing needing changing was the || to &&.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Quote Originally Posted by Steve Cao View Post
    Select the do..while loop will be ok!

    Code:
    int flag=0;
    do
    {
        ...........
        if(a!=1&&a!=2&&a!=3)
          flag=1;
       else
         flag=0;
    }
    while(flag);
    do .. while can be an option but that is not how to do it!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Jun 2010
    Posts
    5
    I see now.
    So the main problem lied in the while conditions.
    I would like to thank everyone for their valuable input and ideas. It really broadened my views on C programming concepts. Btw, thanks for the C programming links.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looping problem
    By main() in forum C Programming
    Replies: 4
    Last Post: 05-27-2010, 02:28 AM
  2. endless looping problem
    By SoulMagician in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2009, 01:21 AM
  3. Getting illegal case error
    By scmurphy64 in forum C Programming
    Replies: 2
    Last Post: 09-17-2009, 10:35 AM
  4. looping problem
    By chris285 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 11:03 AM
  5. Looping problem
    By sketchit in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 02:19 PM

Tags for this Thread


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