为什么不编程提示其他答案

时间:2012-07-06 15:41:44

标签: c

以下是问题

为什么以下程序在提示第一个答案后没有提示其他答案?

以下是其他一些细节

(c)保险公司遵循以下规则来计算保费。 (1)如果一个人的健康状况良好且该人年龄在25至35岁之间并且居住在一个城市并且是男性,那么保险费是卢比。 4‰,他的政策金额不能超过卢比。 2万卢比。 (2)如果一个人满足所有上述条件,除了性别是女性,那么保险费是卢比。 3‰,她的政策金额不能超过卢比。 1万卢比。 (3)如果一个人的健康状况不佳且该人年龄在25至35岁之间并且住在一个村庄并且是男性,那么保险费是卢比。 6‰和他的政策不能超过卢比。 10,000。 (4)在所有其他情况下,该人没有投保。 编写一个程序来输出该人是否应该投保,他/她的保险费率和他/她可以投保的最高金额。

这是我的代码

/* pg 88 G-c
06/07/2012 6:14pm */

#include<stdio.h>
#include<conio.h>
void main() {
    char health,live,sex;
    int age,insured=0,policy=0,premium;

    printf("where is the person living? C or c for city OR V or v for village");
    scanf("%c",&live);
    printf("enter the health of the person: E or e for excellent OR P or p for poor");
    scanf("%c",&health);
    printf("what's the Sex of the person? M or m for Male OR F or f for Female");
    scanf("%c",&sex);
    printf("enter the age of the person");
    scanf("%d",&age);

    if((health=='E'||health=='e')&&(age>=25&&age<=35)&&(live=='C'||live=='c')&&(sex=='M'||sex=='m')) {
        insured=1;
        premium=4;
        policy=200000;
    }
    else if((health=='E'||health=='e')&&(age>=25&&age<=35)&&(live=='C'||live=='c')&&(sex=='F'||sex=='f')) {
        insured=1;
        premium=3;
        policy=100000;
    }
    else if((health=='P'||health=='p')&&(age>=25&&age<=35)&&(live=='V'||live=='v')&&(sex=='M'||sex=='m')) {
        insured=1;
        premium=6;
        policy=10000;
    }

    if(insured==1) {
        printf("the person is insured");
        printf("the premium of the person is %d Rs. per thousand",premium);
        printf("the policy cannot exceed Rs. %d", policy);
    }
    else
        printf("the person is not insured");
}

这是问题 当屏幕要求人居住的地方时,我输入C,c或V,v,当我按下输入时,它显示第二个问题,即人的健康状况,并立即询问第三个问题,即人的性别。

它没有给我输入第二个问题的值的地方或选项:(

我想知道为什么会这样......请帮助我 感谢致敬 Saksham

1 个答案:

答案 0 :(得分:5)

当您按Enter键时,您还会添加\n字符,scanf()很乐意接受该字符作为下一个输入。在您的情况下,最简单的解决方案是告诉scanf()读取下一个非空白字符。这可以这样做:

scanf(" %c",&health); /* note the added space before %c! */

格式中的这个空格会使scanf()吃掉它找到的任何前导空白字符。

相关问题