Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Problems with scanf and loops

Options
  • 11-11-2010 3:31pm
    #1
    Registered Users Posts: 1,586 ✭✭✭


    hi all,

    Another noob question, I would appreciate some advice before I smash my monitor :D

    do {
    printf("Input a letter\n");


    scanf("%c",&choice);
    }

    while(choice != 'a' || 'b' );

    printf("\n");

    Should this not keep printing "input a letter" until either a or b is selected ?
    At the moment it keeps going in a loop no matter what is input.

    Appreciate your help.

    thanks


Comments

  • Registered Users Posts: 981 ✭✭✭fasty


    Where are you reading in the letter? sscanf doesn't read inputs it scans a string.

    Also, you don't check sscanf's return code to see if it actually did anything. Get into the habit of checking return codes or you'll forever be chasing simply problems.


  • Registered Users Posts: 2,002 ✭✭✭bringitdown


    tip: Look at the logic in your while clause.

    What happens if you do an if instead ...? How does OR (||) evaluate?

    i.e.
    if(choice != 'a' || 'b' ) { 
        printf("%c", choice); 
    }
    

    Learn how to debug ... it is essential, you can start with adding in printf's or 'trace' and migrate to GDB or similar.


  • Registered Users Posts: 1,586 ✭✭✭Gaz


    We are not using sscanf yet and it must be a do while loop.

    Ive changed it to the following >

    do {
    printf("Enter:\n");
    printf("A or a for an Aisle seat\n");
    printf("W or w for a Window seat\n");
    printf("Y or y for anY seat\n");
    printf("X to exit\n");


    scanf("%c",&choice);

    }

    while(choice != 'A');


    Now this works, it loops until A is input then it goes further in my code and I have used a switch to change what I want A to do , this all works fine.

    however when I put in while(choice != 'A' || 'a');
    This stays in the loop , so the problem is my logical operator but for the life of me I can figure out whats wrong.

    HELP !


  • Registered Users Posts: 981 ✭✭✭fasty


    while(choice != 'A' || choice != 'a')
    

    I usually just convert to upper case in a situation like this. Looks cleaner and is easier to maintain.


  • Registered Users Posts: 1,586 ✭✭✭Gaz


    yeah I tried that ... same behavior , its doing my head in.


  • Advertisement
  • Registered Users Posts: 981 ✭✭✭fasty


    Right, I'm totally confused about what you want to do.
    while(choice != 'A' || 'a');
    
    This will always be true, since any none zero value is considered true, and the character 'a' is non zero so it'll loop forever.

    Are you having problems handling what to do after a certain key is selected or what?


  • Registered Users Posts: 1,586 ✭✭✭Gaz


    I want the printf statements to print on screen until one of the available options is selected.

    So ...

    do {
    printf("Enter:\n");
    printf("A or a for an Aisle seat\n");
    printf("W or w for a Window seat\n");
    printf("Y or y for anY seat\n");
    printf("X to exit\n");

    Now take the users input scanf("%c",&choice); and assign this to the variable choice.

    while(choice != 'A' || 'a' || 'W' || 'w');

    So if A is input continue into the code , I have switch statements further down and this works correctly , the idea being if A is input continue , if say P is input , thats not in the while statement so printf again.

    It works If I just have while( choice != 'A'); loops when its not an A and continues when it is. So I assumed it would just be a case of using || to make the same true for A or a etc.


  • Registered Users Posts: 981 ✭✭✭fasty


    So you want it to print a list of options until a valid option is selected.

    As I said,
    while(choice != 'A' || 'a' || 'W' || 'w');
    
    will not work since 'a', 'W' and 'w' all evaluated to true, it'll loop forever.

    The while loop will continue while all the conditions within are true, so you're on the right track. Are you sure you tried
    while(choice != 'A' || choice != 'a' || choice != 'W' || choice != 'w');
    
    ?


  • Registered Users Posts: 515 ✭✭✭NeverSayDie


    Gaz wrote: »
    So I assumed it would just be a case of using || to make the same true for A or a etc.

    The problem is, you're using that operator incorrectly. Re-read fasty's last post above yours.

    Expressed in English, an if block with an OR (||) operator in it would resemble;
    "If this is true OR that is true, then XYZ"
    Now you need to think about what the "this" and "that" parts are in your code, and what they correspond to in terms of true or false.


  • Registered Users Posts: 981 ✭✭✭fasty


    Wait, if you want to break out of the loop when one of the required characters is hit, then the boolean logic is different from what you'd expect

    It's this:
    while(choice != 'A' && choice != 'a' && choice != 'W' && choice != 'w');
    

    This means that when the input is not equal to any of those things, it'll evaluate to true, but if choice is equal to any of them, it evaluates to false.

    What you probably had in mind was:
    while(!(choice == 'A' || choice == 'a' || choice == 'W' == choice == 'w'));
    

    Sometimes, the simple stuff escapes me until I actually run the code and use a debugger!

    I think where you were going wrong was with how boolean logic gets evaluated.


  • Advertisement
  • Registered Users Posts: 1,586 ✭✭✭Gaz


    You absolute legend ! This fixed it.
    fasty wrote: »
    while(choice != 'A' && choice != 'a' && choice != 'W' && choice != 'w');
    

    Ive been at that for hours, have all the other complicated bits of the program working but stuck on this for ages.

    I owe you a beer.

    Cheers :D


Advertisement