Thread: Help- While loop

  1. #1
    Registered User LuizCPFL's Avatar
    Join Date
    Dec 2008
    Location
    Louisiana
    Posts
    20

    Help- While loop

    What is wrong with it?
    for some reason if I type a, A, s, S it still does the loop

    Code:
    while (Album != "a" || Album != "A" || Album !="s" || Album != "S")
        {
              printf("Sorry invalid character\n");
              printf("Is it a Album or a Single?\n (Type A for Album or S for Single): ");
              fflush(stdin);
              scanf("%c", &Album);
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Anything in double quotes is a string literal (well, depending on context...) So you're comparing the (read-only) address of the string literal. Use single quotes if you mean a character.

    Don't fflush(stdin), you've already been told it's very, very wrong before (http://cboard.cprogramming.com/showt...php?t=110167#4) -- see the FAQ.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Also, your logic is wrong.
    This:
    Code:
    while (Album != "a" || Album != "A" || Album !="s" || Album != "S")
    is read as this: (and let's pretend Album = 'a'.)
    Code:
    While Album is not equal to 'a'  or     (Album equals 'a', so this is false, which keeps up out of the loop - this is good)
    while Album is not equal to 'A' or      (Album equals 'a', so this is true, which gets us into the loop, which is BAD!!)
    while Album is not equal to 's' or      (Album equals 'a', so this is true, which gets us into the loop, which is BAD!!)
    while Album is not equal to 'S' then do...  (Album equals 'a', so this is true, which gets us into the loop, which is BAD!!)
    What you want is an expression that evaluates to FALSE if any of the above are false, but what you have is an expression that evaluates to TRUE if any of the above are true.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User LuizCPFL's Avatar
    Join Date
    Dec 2008
    Location
    Louisiana
    Posts
    20
    Quote Originally Posted by zacs7 View Post
    Anything in double quotes is a string literal (well, depending on context...) So you're comparing the (read-only) address of the string literal. Use single quotes if you mean a character.

    Don't fflush(stdin), you've already been told it's very, very wrong before (http://cboard.cprogramming.com/showt...php?t=110167#4) -- see the FAQ.
    I know its very very wrong to use fflush(stdin); but my guide telsl me to use it only for now so...

    i tried doing what you told me but it still doesn't work here is my whole source code.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    {   
        // My variebles
        char Title[31],Artist[31],Album;
        int NumberofTracks;
        float price;
        
        
        // Grabbing the variebles
        printf("Welcome to CD data saver!\n");
        printf("Please enter the CD details Below:\n");
        printf("Title? ");
        scanf("%[^\n]", &Title);
        printf("Artist? ");
        fflush(stdin);
        scanf("%[^\n]", &Artist);
        printf("Number of tracks? ");
        fflush(stdin);
        scanf("%d", &NumberofTracks);
        printf("Is it a Album or a Single?\n (Type A for Album or S for Single): ");
        fflush(stdin);
        scanf("%c", &Album);
        while (Album != 'a' || Album != 'A' || Album != 's' || Album != 's')
        {
              printf("Sorry invalid character\n");
              printf("Is it a Album or a Single?\n (Type A for Album or S for Single): ");
              fflush(stdin);
              scanf("%c", &Album);
    }
        
        printf("Price? (e.g 9.98): ");
        fflush(stdin);
        scanf("%f", &price);
        
        // Results    
              printf("\n\nThe CD data you entered are:\n\n");
              printf("==========CD DATA==========\n");
              printf("Title: %s\n", Title);
              printf("Artist: %s\n", Artist);
              if ( Album == "a" || Album == "A")
              {
              printf("Album\n");
              }
              else
              {
              printf("Single\n");
              }
              printf("Number of tracks: %d\n", NumberofTracks);
              printf("Price $ %.2f\n", price);
              printf("==========CD DATA==========\n");
              printf("\nPress ENTER to exit program.");
        // End results
              
        fflush(stdin);
        getchar();
        
    }

  5. #5
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    you are still using double quotes in the if statement.
    Also main returns an int.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  6. #6
    Registered User LuizCPFL's Avatar
    Join Date
    Dec 2008
    Location
    Louisiana
    Posts
    20
    yeah thanks didn't notice that but I solved my problem my using && instead of || and it workd

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > guide telsl me to use it only for now so...
    A guide that goes against the standards? Get a new guide

  8. #8
    Registered User LuizCPFL's Avatar
    Join Date
    Dec 2008
    Location
    Louisiana
    Posts
    20
    lol well its the only video guide I got. It is probably outdated thats why.

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    LOL. Don't tell me it's "Video Professor teaches C" - the same guy that teaches you how to buy and sell on eBay?!! (If you are not in the US, you probably won't get it)
    Mainframe assembler programmer by trade. C coder when I can.

  10. #10
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by Dino View Post
    LOL. Don't tell me it's "Video Professor teaches C" - the same guy that teaches you how to buy and sell on eBay?!! (If you are not in the US, you probably won't get it)
    LOL i do get it, "I'll teach you to buy and sell on ebay or you can have your money back guaranteed"
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  11. #11
    Registered User LuizCPFL's Avatar
    Join Date
    Dec 2008
    Location
    Louisiana
    Posts
    20
    LOL no i'm not that stupid why buy things online when you can use torrents.


    edit: i don't get it
    Last edited by LuizCPFL; 12-11-2008 at 10:44 AM.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I know its very very wrong to use fflush(stdin); but my guide telsl me to use it only for now so...
    Do you want to learn C, or just some compiler specific hacks.
    Your choice, just let us know OK.

    Or put another way, if you think learning C is hard, try unlearning bad C and relearning good C at the same time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  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