Like Tree5Likes
  • 2 Post By Click_here
  • 3 Post By AndiPersti

Help with simple do while function in C?

This is a discussion on Help with simple do while function in C? within the C Programming forums, part of the General Programming Boards category; hey guys, so here is how the program works. when the user enters any letter besides m or f, it ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    5

    Question Help with simple do while function in C?

    hey guys, so here is how the program works.
    when the user enters any letter besides m or f, it should ask the question again. if the user enters m or f, then the program proceeds. In this situation, when the user enters m, the program proceeds, but when the user enters f, the program repeats the question again. Why is that, and how do i fix this? I tried using the OR ( the two vertical bars) but that didnt work either.

    Code:
    do{
    printf("What's your gender? Enter m or f >>> ");
    scanf(" %c", &gender);
    }
            
    while (gender != 'm' && 'f');

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,902
    Wrong; does not work in C the way you think.
    Code:
    while (gender != 'm' && 'f');
    Code:
    while (gender != 'm' && gender != 'f');
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    5
    Thanks Tim! quick question: if i wanted to add more variables, such as x, y, z, etc, then i would have to type out gender !='x' && gender !='y' && gender !='z' ? or is there a short cut to this that i am unaware about?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,902
    Quote Originally Posted by yoyoyo View Post
    Thanks Tim! quick question: if i wanted to add more variables, such as x, y, z, etc, then i would have to type out gender !='x' && gender !='y' && gender !='z' ? or is there a short cut to this that i am unaware about?
    That is the way I do it. I know of no short cut.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,511
    Quote Originally Posted by yoyoyo View Post
    Thanks Tim! quick question: if i wanted to add more variables, such as x, y, z, etc, then i would have to type out gender !='x' && gender !='y' && gender !='z' ? or is there a short cut to this that i am unaware about?
    Sometimes you can simplify logical expressions with Boolean Algebra, but not in this case -> You can make it look different:
    Code:
    g!=x && g!=y && g!=z
    is the same as
    !(g==x || g==y || g==z)
    stahta01 and yoyoyo like this.
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by yoyoyo View Post
    if i wanted to add more variables, such as x, y, z, etc, then i would have to type out gender !='x' && gender !='y' && gender !='z' ? or is there a short cut to this that i am unaware about?
    Using strchr() you could write it as:
    Code:
    while (!strchr("xyz", gender));
    Bye, Andreas
    Crossfire, stahta01 and yoyoyo like this.

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    5
    Thank you everyone for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Function
    By DeanWinchester in forum C Programming
    Replies: 3
    Last Post: 02-02-2012, 10:37 AM
  2. simple function
    By barilla in forum C Programming
    Replies: 2
    Last Post: 02-08-2010, 11:09 AM
  3. Replies: 5
    Last Post: 10-17-2006, 08:54 AM
  4. Need help with simple c++ function
    By Ricochet in forum C++ Programming
    Replies: 7
    Last Post: 11-13-2003, 10:46 AM
  5. Simple Function but......I NEED HELP
    By justin69enoch in forum C Programming
    Replies: 7
    Last Post: 09-29-2002, 09:56 PM

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