Thread: If statement being ignored?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    2

    If statement being ignored?

    Hi.

    I'm currently working on a programme for college but am stuck on an IF statement!

    The code seems correct and the programme works fine untill the IF statement where it appears the IF statement is jumped over and the programme quits.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    char surname[30];
    char initial[1];
    char dob[8];
    int choice;
    
    printf("Please enter your surname: ");
    scanf("%s", surname);
    printf("Please enter your initial: ");
    scanf("%s", initial);
    printf("Please enter your date of birth in the DD/MM/YYYY format: ");
    scanf("%d", dob);
    system("cls");
    printf("1 - Arithmetic \n");
    printf("2 - Conversion \n");
    printf("3 - Result \n");
    
    printf("Please choose a number to continue: ");
    scanf("%d",&choice);
    
    if(choice==1)
    {
    printf("You chose 1");
    }
    else if(choice==2)
    {
    printf("You chose 2");
    }
    return(0);
    }
    That's the code. It works fine until the final printf.

    Any ideas?

    Cheers.

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Your problem is that the next line isn't able to read the entire date format:
    Code:
    scanf("&#37;d", dob);
    As you can see, you're using the %d which reads a number instead of %s which reads a string. This leads to leftover values still in the stdin, which are then read by the last scanf. In turn, I don't think that choice is either of these values, why not try to printf() the value in choice to see what it will be like.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    2
    Well I replaced the "&#37;d" with "%s" and it appears to have worked

    You sir are a legend!

    Cheers.

  4. #4
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by FincH View Post
    Well I replaced the "%d" with "%s" and it appears to have worked

    You sir are a legend!

    Cheers.
    I'm always glad to help people with my rather limited knowledge of C. I guess the fact that the questions asked around here are pretty low-level helps a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  4. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM