Thread: Characters in if statements

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    2

    Question Characters in if statements

    My goal here is to define 'target' yet when running the program, entering either m or f yields an undefined 'target'. Can anyone identify my problem?

    int age, gender, num_rates = -1, hit_rates = 0, rate, target;

    printf("What is your gender? (m or f)\n");
    scanf("%c", &gender);
    printf("What is your age?\n");
    scanf("%d", &age);
    printf("Enter your recorded heart rates:\n");
    if (gender == 'f')
    target = 220-age;
    else if (gender == 'm')
    target = 226-age;
    Thanks

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You are declaring "gender" as an integer, but assigning a character to it with "scanf()."

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by Matticus View Post
    You are declaring "gender" as an integer, but assigning a character to it with "scanf()."
    I dont think that would be problem. There would be implicit coversion.
    @OP: you dont read the heart rate, have you noticed that. And what is the expected out for the 'm' and age 25? I dont see any issues.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2
    Quote Originally Posted by ssharish2005 View Post
    I dont think that would be problem. There would be implicit coversion.
    @OP: you dont read the heart rate, have you noticed that. And what is the expected out for the 'm' and age 25? I dont see any issues.

    ssharish
    Actually, that was the problem, and I only posted a portion of the entire program. The entire thing is functional now. Thanks, Matticus.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Nigel... you really should check the return values of scanf() to be sure the conversions succeeded... It returns the number of successful conversions so in your case anything but 1 is an error.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    No problem.

    @ ssharish, my compiler doesn't like this, either.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int x;
    
    	printf("Enter value for 'x':  ");
    	scanf("%c",&x);
    
    	printf("%d\t%c\n\n",x,x);
    
    	if(x=='m') printf("This won't print.\n\n");
    
    	return 0;
    }

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by Matticus View Post
    No problem.

    @ ssharish, my compiler doesn't like this, either.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int x;
    
    	printf("Enter value for 'x':  ");
    	scanf("%c",&x);
    
    	printf("%d\t%c\n\n",x,x);
    
    	if(x=='m') printf("This won't print.\n\n");
    
    	return 0;
    }
    What compiler do you use? have no issue compiler and running it. See my output

    Code:
    Enter value for 'x':  m
    109     m
    
    This won't print.
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ssharish2005 View Post
    What compiler do you use? have no issue compiler and running it. See my output
    The real issue is what compiler do YOU use?

    Since you are assigning a char to an int the conversion in scanf() could fail.


    From Pelles C...
    Building main.obj.
    E:\C_Code\Experiments\testprog\main.c(8): warning #2234: Argument 2 to 'scanf' does not match the format string; expected 'char *' but found 'int *'.
    Building testprog.exe.
    Done.
    Last edited by CommonTater; 06-22-2011 at 09:40 AM.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    It has to do with native machine's byte order. When scanf assigns just one byte into a 4-byte integer, the remaining bytes may still contain garbage. The assigned byte may be placed in the lowest order byte or highest. Then when you attempt to do a comparison with 'a', as long as the other bytes aren't zero or the expected byte is in any place other than the lowest, it will always fail.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Just change x to be of char type, and it will work.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch statements and characters
    By jbauserm in forum C Programming
    Replies: 1
    Last Post: 03-07-2011, 07:43 PM
  2. help with if and else statements
    By kburyanek in forum C Programming
    Replies: 7
    Last Post: 09-25-2009, 01:28 AM
  3. If statements
    By DJ_Steve in forum C Programming
    Replies: 12
    Last Post: 08-13-2009, 12:43 PM
  4. IF statements
    By Viperz0r in forum C++ Programming
    Replies: 11
    Last Post: 03-05-2006, 11:08 AM
  5. Characters in If Statements
    By Padawan in forum C Programming
    Replies: 10
    Last Post: 04-04-2004, 02:31 PM