简体   繁体   English

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

[英]Why doesn't program prompt for additional answers

Here's the question 这是问题

Why does the following program not prompt for additional answers, after it has prompted for the first? 在提示第一个程序之后,为什么以下程序不提示其他答案?

Here are some additional details 这是一些其他细节

(c) An Insurance company follows following rules to calculate premium. (c)保险公司遵循以下规则来计算保费。 (1) If a person's health is excellent and the person is between 25 and 35 years of age and lives in a city and is a male then the premium is Rs. (1)如果一个人的身体状况良好,并且年龄在25至35岁之间,并且居住在城市并且是男性,则保险费为Rs。 4 per thousand and his policy amount cannot exceed Rs. 千分之四,他的保单金额不能超过卢比。 2 lakhs. 20万。 (2) If a person satisfies all the above conditions except that the sex is female then the premium is Rs. (2)如果一个人满足上述所有条件,但性别是女性,则保险费为Rs。 3 per thousand and her policy amount cannot exceed Rs. 每千分之三,她的保单金额不能超过卢比。 1 lakh. 10万 (3) If a person's health is poor and the person is between 25 and 35 years of age and lives in a village and is a male then the premium is Rs. (3)如果一个人的健康状况不佳,年龄在25至35岁之间,并且居住在一个村庄,并且是男性,则保费为Rs。 6 per thousand and his policy cannot exceed Rs. 千分之六,他的保单不能超过卢比。 10,000. 10,000。 (4) In all other cases the person is not insured. (4)在所有其他情况下,该人没有被保险。 Write a program to output whether the person should be insured or not, his/her premium rate and maximum amount for which he/she can be insured. 编写程序以输出该人是否应该被保险,他/她的保险费率以及他/她可以被保险的最大金额。

Here's my CODE 这是我的密码

/* 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");
}

Here's the Problem when the screen asks for the place where person live i enter C,c or V,v and when i press enter it displays the second question ie person's health and straight away asks the third question ie sex of the person. 这是问题所在,当屏幕要求输入人的居住地时,我输入C,c或V,v,当我按Enter键时,它显示第二个问题,即人的健康状况,并立即问第三个问题,即人的性别。

it doesn't give me the place or option to enter the value for the 2nd question :( 它没有给我输入第二个问题的值的地方或选项:(

i want to know why is this happening... please help me thanks and regards Saksham 我想知道为什么会这样...请帮助我,谢谢并问候萨克瑟姆

When you press enter, you also add a \\n character, which your scanf() is happy to accept as the next input. 当按下回车键时,您还添加了一个\\n字符,您的scanf()很乐意接受该字符作为下一个输入。 The easiest solution in your case, is to tell scanf() to read the next non-whitespace character. 在您的情况下,最简单的解决方案是告诉scanf()读取下一个非空白字符。 This can be done like: 可以这样完成:

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

This space in the format will make scanf() eat any leading whitespace characters that it finds. 格式中的此空格将使scanf()吃掉它找到的任何前导空白字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM