Thread: A simple question

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    18

    A simple question

    Hello!

    I want to write a program that it writes to the screen

    --if the driver is married

    --if the driver is unmarried,male,above 30 years of age

    --if the driver is unmarried,female,above 25 years of age.

    i wrote this when i compile it gives abnormal results i can't understand

    please find my faults

    Code:
    #include <stdio.h>
    
    int main()
    {
        char ms,sex;
        int age;
        printf("Enter Marry or Unmarry Status\nSex\nAge\n");
        scanf("%c%c%d",&ms,&sex,&age);
        
        if(ms=='M')
           printf("Insured\n");
        else if(ms=='U'&&sex=='m'&&age>30)
           printf("Insured\n");
        else if(ms=='U'&&sex=='f'&&age>25)
           printf("Insured\n");
        else
           printf("Uninsured\n");
        main();
    }
    Code:
    #include <stdio.h>
    
    int main()
    {
        char ms,sex;
        int age;
        printf("Enter Marry or Unmarry Status\nSex\nAge\n");
        scanf("%c%c%d",&ms,&sex,&age);
        
        if(ms=='M')
           printf("Insured\n");
        else if(ms=='U'&&sex=='m'&&age>30)
           printf("Insured\n");
        else if(ms=='U'&&sex=='f'&&age>25)
           printf("Insured\n");
        else
           printf("Uninsured\n");
        main();
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First of all C is case sensitive... Look at the way you're mixing cases in your if... else stack...

    You need to either...
    a) switch to all lower case
    b) all upper case (requires caps lock)
    c) account for both upper and lower cases

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    18
    i cant understand anything.I fix them to low case but it gives the wrong result again

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    18
    when i write u then f i cant write age write uninsured all of a sudden

  5. #5
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    i would have to run the program, but just a thought.
    How do you input the values?
    I am not sure about characters, but scanf returns when it detects whitespace or newline?
    So if you entered
    U f
    into the cmd you get an error..because it evaluates 'u' and ' ' which is wrong..?

    I will try it now

    EDIT:
    And don't call main! You can't!!!
    how did that not give you an error??
    Last edited by Eman; 01-16-2011 at 05:44 PM.
    You ended that sentence with a preposition...Bastard!

  6. #6
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    ok, just remove the newline from the buffer using getchar()
    try this:
    Code:
    #include <stdio.h>
    
    int main()
    {
        char ms,sex;
        int age=0;
        printf("Enter Marry or Unmarry Status\nSex\nAge\n");
        scanf("%c",&ms);
        getchar() ;
        scanf("%c",&sex);
        getchar() ;
        scanf("%d", &age) ;
    
        
        if(ms=='m')
           printf("Insured\n");
        else if((ms=='u'&&sex=='m'&&age>30) || (ms=='u'&&sex=='f'&&age>25)) 
               
           printf("Insured\n");
        else
           printf("Uninsured\n");
        
        
        getchar() ;
    }
    You ended that sentence with a preposition...Bastard!

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    18
    thanks eman a lot i fix my error with this line

    scanf("%c\n%c\n%d",&ms,&sex,&age);

    and i make the inputs like this

    f
    u
    30

    and it gave the correct result
    thanks again

  8. #8
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    even better..
    but do you understand why that works?
    You ended that sentence with a preposition...Bastard!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM