Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

one scanf statement not taking value please help me.

–2 votes
asked Apr 11, 2021 by Shalivahan (100 points)
#include<stdio.h>

main()

{

  int amount,age;

  char health,place,sex;

            printf("enter the health status if healthy enter h  for unhealthy p \n");

    scanf("%c",&health);

   

   

printf("Enter the sex for male m   and for female f\n");

scanf("%c",&sex);

printf("for city enter the c  and for village enter   v\n");

scanf("%c",&place);

printf("enter the amount\n");

scanf("%d",&amount);

printf ("enter the age\n");

scanf("%d",&age);

if(health=='h'&&  place=='c'  &&age>25 )

{

  if (sex=='m' && amount<200000)

   {

      printf(" you will get  remium is Rs. 4 per thousand  ");

   }

   else if(amount<300000)

     printf(" you will get  remium is Rs. 3 per thousand  ");

}

else if(health=='p'  && sex=='m'&&   place=='v'  && amount<10000 )

  printf("premium is Rs. 6 per thousand");

else

printf("SRY you will not insured");

}

1 Answer

0 votes
answered Apr 19, 2021 by Peter Minarik (86,640 points)
edited Apr 20, 2021 by Peter Minarik

When reading a character, you should add an extra space before the %c format specifier to tell scanf to ignore any whitespace character (e.g. ENTER from last input):

    printf("enter the health status if healthy enter h  for unhealthy p \n");
    scanf(" %c", &health);
    printf("Enter the sex for male m   and for female f\n");
    scanf(" %c", &sex);
    printf("for city enter the c  and for village enter   v\n");
    scanf(" %c", &place);
    printf("enter the amount\n");
    scanf("%d", &amount);
    printf("enter the age\n");
    scanf("%d", &age);

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...