Below shown is a simple C Code. The code which is troubling is in commented area (please remove comment symbol to run those lines, comment symbl is just here to highlight them), here there are two printf's in comment area......... the error is that both are getting executed continuously without letting the user to enter anything...............and when i change location into integer then it works perfectly(changing options "A and B" as "1 and 2")

plz try to help me..................... tnx in advance

#include<stdio.h>
#include<conio.h>


int main()
{
    char gender;
    char location;
    char health;
    int age;


    printf("enter ur gender F: Female M:Male");
    scanf("%c", & gender);

    printf("\n Enter ur age");
    scanf("%d", & age);

/*
    printf("Enter your location E for city  F for village ");
    scanf("%c",& location);

    printf("\n Enter Health C for Excellent D for Poor");
    scanf("%c",& health);
*/


if(gender=='M' && health=='C' && location=='A' && (age>=25 &&age<=35))
    {
        printf("\n Allowed to insure");
        printf("\n Premium rate will be 4 per thousand");
        printf("\n Max amount can be 2lacs");
    }

    else if(gender=='F' && health=='C' && location=='A' && (age>=25 &&age<=35))
    {
        printf("\n Allowed to insure");
        printf("\n Premium rate will be 3 per thousand");
        printf("\n Max amount can be 1lacs");
    }

    else if(gender=='M' && health=='D' && location=='B' && (age>=25 &&age<=35))
    {
        printf("\n Allowed to insure");
        printf("\n Premium rate will be 6 per thousand");
        printf("\n Max amount can be 10 Thousand");
    }

    else
    {
        printf("Not allowed to insure");
    }       


    getch();
}

Recommended Answers

All 6 Replies

Embed your program code between code tags...

Below shown is a simple C Code. The code which is troubling is in commented area (please remove comment symbol to run those lines, comment symbl is just here to highlight them), here there are two printf's in comment area......... the error is that both are getting executed continuously without letting the user to enter anything...............and when i change location into integer then it works perfectly(changing options "A and B" as "1 and 2")

plz try to help me..................... tnx in advance

#include<stdio.h>
#include<conio.h>


int main()
{
char gender;
char location;
char health;
int age;


printf("enter ur gender F: Female M:Male");
scanf("%c", & gender);

printf("\n Enter ur age");
scanf("%d", & age);

/*
printf("Enter your location E for city F for village ");
scanf("%c",& location);

printf("\n Enter Health C for Excellent D for Poor");
scanf("%c",& health);

*/


if(gender=='M' && health=='C' && location=='A' && (age>=25 &&age<=35))
{
printf("\n Allowed to insure");
printf("\n Premium rate will be 4 per thousand");
printf("\n Max amount can be 2lacs");
}

else if(gender=='F' && health=='C' && location=='A' && (age>=25 &&age<=35))
{
printf("\n Allowed to insure");
printf("\n Premium rate will be 3 per thousand");
printf("\n Max amount can be 1lacs");
}

else if(gender=='M' && health=='D' && location=='B' && (age>=25 &&age<=35))
{
printf("\n Allowed to insure");
printf("\n Premium rate will be 6 per thousand");
printf("\n Max amount can be 10 Thousand");
}

else
{
printf("Not allowed to insure");
}


getch();
}

scanf function always causes this type of problem.. specially when you are using it for char after using it for integer variable,

use getchar() function for inputting char variable...

try this

#include<stdio.h>
#include<conio.h> //avoid using conio.h


int main()
{
char gender;
char location;
char health;
int age;


printf("enter ur gender F: Female M:Male");
gender=getchar();

printf("\n Enter ur age");
scanf("%d", &age);

fflush(stdin);
printf("Enter your location E for city F for village ");
location=getchar();
fflush(stdin);
printf("\n Enter Health C for Excellent D for Poor");
health=getchar();



if(gender=='M' && health=='C' && location=='A' && (age>=25 &&age<=35))
{
printf("\n Allowed to insure");
printf("\n Premium rate will be 4 per thousand");
printf("\n Max amount can be 2lacs");
}

else if(gender=='F' && health=='C' && location=='A' && (age>=25 &&age<=35))
{
printf("\n Allowed to insure");
printf("\n Premium rate will be 3 per thousand");
printf("\n Max amount can be 1lacs");
}

else if(gender=='M' && health=='D' && location=='B' && (age>=25 &&age<=35))
{
printf("\n Allowed to insure");
printf("\n Premium rate will be 6 per thousand");
printf("\n Max amount can be 10 Thousand");
}

else
{
printf("Not allowed to insure");
}

getch();
return 0;
}

Program works fine with scanf also but fflush(stdin) at 19 n 22 are must ..

Thanks...............................

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.