Like Tree3Likes
  • 3 Post By Matticus

C program newbee

This is a discussion on C program newbee within the C Programming forums, part of the General Programming Boards category; Hi, I have written the following code. Which on the basis of few conditions, calculates whether the person is eligible ...

  1. #1
    V17
    V17 is offline
    Registered User
    Join Date
    Jul 2011
    Posts
    5

    C program newbee

    Hi,

    I have written the following code. Which on the basis of few conditions, calculates whether the person is eligible for insurance or not.

    When is execute the code, the program does'nt accept the second input and directly jumps to the third input.

    Could you please suggest what is wrong witht the code.
    ================================================== =======
    CODE:
    ------
    Code:
    #include<stdio.h>
    main()
    
    {
    int age;
    char gender,status;
    	
    	printf("\n enter your gender m/f : ");
    	scanf("%c",&gender);
    	
    	printf("\n enter your status a/u: ");
    	scanf("%c",&status);
    
    	printf("\n enter your age: ");
    	scanf("%d",&age);
    	
    	
    	if((status=='a')||((status=='u')&&(gender=='m')&&(age>30))||((status=='u')&&(gender=='f')&&(age>25)))
    		printf("you are insured");
    	else
    		printf("you are not insured");
    	}
    ================================================== ===========

    OUTPUT
    --------


    $ ./new03.exe

    enter your gender m/f : m

    enter your status a/u: <does'nt wait for the user to enter the value>
    enter your age: 23
    you are not insured
    ================================================== ===========

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Hello friend. Take a look at this thread Same problem.

    Also you might want to look at the tutorials on this site. Good place to start.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    2
    I think the mistake is in the if loop where the braces need to be used correctly.
    For example if you say a || b || c and you want to first compute a combination of two of the three you need to specify it!!! like (a||b) || c.

    I hope it helps.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    134
    Quote Originally Posted by Sasi Srinivas View Post
    I think the mistake is in the if loop where the braces need to be used correctly.
    For example if you say a || b || c and you want to first compute a combination of two of the three you need to specify it!!! like (a||b) || c.

    I hope it helps.
    "If()" is a condition to check, NOT "loop". And there will be no impact on output whether you choose (a||b) || c or a || b || c. The difference is made by Operator Precedence. Read Operator Precedence
    S_ccess is waiting for u. Go Ahead, put u there.

  5. #5
    V17
    V17 is offline
    Registered User
    Join Date
    Jul 2011
    Posts
    5
    Quote Originally Posted by AndrewHunter View Post
    Hello friend. Take a look at this thread Same problem.

    Also you might want to look at the tutorials on this site. Good place to start.


    Thanks! AndewHunter. getchar() did it for me.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sasi Srinivas View Post
    I think the mistake is in the if loop where the braces need to be used correctly.
    For example if you say a || b || c and you want to first compute a combination of two of the three you need to specify it!!! like (a||b) || c.

    I hope it helps.
    1) if() is not a loop
    2) the problem is occuring long before he gets there
    3) it is skipping the second input because of a newline character left behind by the first.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    Per CommonTater's point #3, note the space on the 2nd scanf() now

    Code:
    #include<stdio.h>
    int main()
    
    {
    int age;
    char gender,status;
    
            printf("\n enter your gender m/f : ");
            scanf(" %c",&gender);
    
            printf("\n enter your status a/u: ");
            scanf(" %c",&status);
    
            printf("\n enter your age: ");
            scanf("%d",&age);
    
    
            if((status=='a')||((status=='u')&&(gender=='m')&&(age>30))||((status=='u')&&(gender=='f')&&(age>25)))
                    printf("you are insured");
            else
                    printf("you are not insured");
    
    
            return 0 ;
    }
    
    
    /* OUTPUT
    
    enter your gender m/f : m
    
     enter your status a/u: u
    
     enter your age: 99
    you are insured
    
    
    */

  8. #8
    Registered User
    Join Date
    Jul 2011
    Location
    Chandigarh, India, India
    Posts
    2
    There are many possible solutions for this.

    1) You can use fflush(stdin). This is not recommended though.
    2) You can use getche() instead of scanf.
    Example:
    gender=getche();
    3). Or you can just place a getch() after each scanf.

    There would be many other ways to do this as well...

    All of the above three would make it work.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    2,373
    This problem appears to have already been solved by the OP 5 days ago.

    1) You can use fflush(stdin). This is not recommended though.
    It's not that it's "not recommended." It is actively discouraged as it is technically undefined. Perhaps you should have explained to the self-professed beginner why this is not appropriate to use.

    Why fflush(stdin) is wrong

  10. #10
    Registered User
    Join Date
    Jul 2011
    Posts
    24
    Hi..
    instead of usiing scaf() function use getchar() function the problem will be solved!!!!

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by parimal.s.dave View Post
    Hi..
    instead of usiing scaf() function use getchar() function the problem will be solved!!!!
    I am not sure what you are getting at but the problem was solved 5 days ago. There is no need to bump old threads when you have no new information to provide. If you have a question on something start a new thread or if you like something feel free to use the like link.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help a newbee
    By joker_tony in forum C Programming
    Replies: 2
    Last Post: 08-03-2009, 01:47 PM
  2. Linux newbee
    By geek@02 in forum Linux Programming
    Replies: 8
    Last Post: 11-08-2004, 11:02 PM
  3. Newbee
    By Bio Hazord in forum C++ Programming
    Replies: 2
    Last Post: 10-22-2003, 07:08 PM
  4. Newbee
    By Dbug in forum C Programming
    Replies: 1
    Last Post: 12-13-2001, 11:45 PM
  5. Newbee question
    By d00-asu in forum Windows Programming
    Replies: 4
    Last Post: 08-31-2001, 03:32 AM

Tags for this Thread


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