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

'While' loop confusion

Options
  • 05-12-2004 2:07pm
    #1
    Closed Accounts Posts: 4,943 ✭✭✭


    Right, after i fixed my previous blunder, i decided to start complicating my code slightly.

    Basically, this is a multplication/addition etc program, and the user is given an option at the start to decide whether they want to multiply/divide etc.
    'a' is for addition
    's' is for subtraction
    'm' is for multiplication
    'd' is for division

    If they don't enter one of those values, an error message gets printed on the screen and says "You've entered an invalid value, blah blah, please enter a new one".

    This is where the problem arises. I want to circle this section of code with a while loop, so that this section keeps repeating itself while the "operator" does NOT equal any of those letters, i.e. if i keep entering x, that section will keep asking me to enter a new operator until i enter either a, s, m or d.

    How to i word the loop? I've tried this, but it doesn't work, because as soon as i enter 'a' the loop will still repeat, as operator !=m...etc etc.
    while (operator != 'a' || operator != 's' || operator != 'd' || operator != 'm'){
          printf("\nPress 'a' for addition\nPress 's' for subtraction\nPress 'm' for multiplication\nPress 'd' for division: ");
          scanf(" %c", &operator);
          if(operator == 'a' || operator == 's' || operator == 'd' || operator == 'm'){
    	operator = operator;
          }
          else{
    	printf("\nYou've entered an invalid choice, please choose again.");
          }
    


Comments

  • Registered Users Posts: 1,481 ✭✭✭satchmo


    Use ANDs not ORs!
    while (operator != 'a' && operator != 's' && operator != 'd' && operator != 'm'){

    If you think about the logic you'll see why; if you use ORs and operator is 'a', you'll get <0 || 1 || 1 || 1> which will still result in 1. But with ANDs you'll get <0 && 1 && 1 && 1> which will be 0, breaking out of the loop.


  • Registered Users Posts: 32,136 ✭✭✭✭is_that_so


    Agree with Satchmo, but not the best way to run a while loop here. In this type of situation where you have multiple conditions a Switch/Case structure might be better . Much easier to follow logically and to code

    Set a True flag here or something to let you into the while loop

    flag=true;

    while (true)
    {
    switch( val)
    {
    case 'a' addition;;
    case 's' subtraction
    case 'm' multiplication;;
    case 'd' division ;;
    default " You can't do that" ;flag=false;;
    }
    }

    or whatever the syntax is for the language you're coding in. Plenty of online examples for this type of code.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    satchmo wrote:
    Use ANDs not ORs!
    while (operator != 'a' && operator != 's' && operator != 'd' && operator != 'm')
    gotcha, i did think about using &&, but i just couldn't work out how i could phrase it to work.

    Muchos gracios!


  • Registered Users Posts: 4,287 ✭✭✭NotMe


    By the way, this line is pointless:
    operator = operator;
    
    Why set a variable equal to itself?

    I would do it like this:
    printf("\nPress 'a' for addition\nPress 's' for subtraction\nPress 'm' for multiplication\nPress 'd' for division: ");
    scanf(" %c", &operator);
    while (operator != 'a' && operator != 's' && operator != 'd' && operator != 'm')
    {  
          printf("\nYou've entered an invalid choice, please choose again.");
          scanf(" %c", &operator);
    }
    


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    NotMe wrote:
    By the way, this line is pointless:
    operator = operator;
    
    Why set a variable equal to itself?
    Well, i was bored, and it was a handy thing to write in the if loop, it does nothing, but looks impressive :p I wasn't sure if i could just leave the if loop empty. I thought that if i did that, it would feck up the program. Basically, i didn't want the "if" part to do anything, i just wanted the else part to kick in if the "if" part proved false.

    And yes, your solution to the while problem is exactly what i have to do. and i;ve already implented it, and it works perfectly.


  • Advertisement
  • Registered Users Posts: 7,276 ✭✭✭kenmc


    Well, i was bored, and it was a handy thing to write in the if loop, it does nothing, but looks impressive :p I wasn't sure if i could just leave the if loop empty. I thought that if i did that, it would feck up the program. Basically, i didn't want the "if" part to do anything, i just wanted the else part to kick in if the "if" part proved false.
    If you don't want the "if" part do to anything, but you do want the else part, then why not rewrite the expression so that only the "if" is there (but does what the "else" currently does)
    eg
    if(someVar == SomeConst)
    {
    // do nothing
    }
    else
    {
    // do some work here
    }

    can simply be replaced with....
    if(someVar != SomeConst)
    {
    // do some work here.
    }

    now the code is easier to look at, less to read/write, no empty loops which will almost certainly get optimised away by the compiler anyway, and generally a lot better coding practice.......
    In general, the only reason you should have an empty if/else if /else statement would be an empty else statement after some if/else ifs, to show that you have considered the reprocussions and decided that once you've handled all the previous cases, anything left over is not important, and you should comment it to say so.

    besides, in the original code, you're doing a hell of a lot of checking on the "operator" variable twice - you're testing it for a load of letters in the "while" statement, and then again in the "if" statement, which you're not even using!


  • Registered Users Posts: 1,391 ✭✭✭fatherdougalmag


    And if you're scanning for particular characters, bundle them into a string and use strchr()
     char options[] = "asdm";
     printf("\nEnter 'a' for addition\nEnter 's' for subtraction\nEnter 'm' for multiplication\nEnter 'd' for division\nAny other option quits: ");
     scanf(" %c", &operator);
    
     while (strchr(options,operator))
     {
        /* Handle the option */
        .
        .
        .
        printf("\nEnter 'a' for addition\nEnter 's' for subtraction\nEnter 'm' for multiplication\nEnter 'd' for division\nAny other option quits: ");
        scanf(" %c", &operator);
     }
    


Advertisement