如何在c中验证字符串和双精度

如何在c中验证字符串和双精度,c,C,我是一个月前用c开始编程的新手。 我正在为我的学校作业编写代码,我无法在我编写的以下代码中验证性别和工资。 谁能帮我一下吗 以下是我的问题。 第三季度。公司决定在新年给所有员工发奖金。根据决定,所有男性员工将获得5%的奖金,女性员工将获得10%的奖金。此外,如果员工的工资低于10000卢比,则该员工将额外获得2%的工资奖金。编写一个C程序,输入员工的工资和性别,并计算必须给予员工的奖金 下面是我的代码 int main() { char gender[6]; double salary;

我是一个月前用c开始编程的新手。 我正在为我的学校作业编写代码,我无法在我编写的以下代码中验证性别和工资。 谁能帮我一下吗

以下是我的问题。 第三季度。公司决定在新年给所有员工发奖金。根据决定,所有男性员工将获得5%的奖金,女性员工将获得10%的奖金。此外,如果员工的工资低于10000卢比,则该员工将额外获得2%的工资奖金。编写一个C程序,输入员工的工资和性别,并计算必须给予员工的奖金

下面是我的代码

int main()
{
 char gender[6];
 double salary;
 double bonusSalary;
 printf("enter your gender here:");
 scanf("%s", &gender);
 printf("enter your salary here:");
 scanf("%lf", &salary);
 if( gender == 'male' && salary<10000 )
 {
     bonusSalary=salary*(7.0/100.0);
 }
 else if( gender == 'male' )
 {
     bonusSalary=salary*(5.0/100.0);
 }
 else if( gender == 'female' && salary<10000 )
 {
     bonusSalary=salary*(10.0/100.0);
 }
 else if( gender == 'female' )
 {
     bonusSalary=salary*(12.0/100.0);
 }
 printf("bonus amount you will receive is:%f", bonusSalary);
 return 0;
}
intmain()
{
性别[6];
双薪;
双骨瓣;
printf(“在此处输入您的性别:”);
scanf(“%s”和性别);
printf(“在此处输入您的薪资:”);
scanf(“%lf”和工资);

如果(性别==‘男性’&&salary我修改了你的代码,我想这就是你想要的

#include<stdio.h>
#include<string.h> //needs to be included to use strcmp()
int main(){
    char gender[7]; //gender needs to be of length 7 since "female" is of length 6 and it needs a null terminator at the end
    double salary;
    double bonusSalary;
    printf("enter your gender here:");
    scanf("%s", gender); //%s doesn't need '&'
    printf("enter your salary here:");
    scanf("%lf", &salary);
    if(strcmp(gender,"male")==0 && salary<10000 ) //strcmp() compares two strings, string literal in c needs to be in double quotes
    {
        bonusSalary=salary*(7.0/100.0);
    }
    else if(strcmp(gender,"male")==0)
    {
        bonusSalary=salary*(5.0/100.0);
    }
    else if(strcmp(gender,"female")==0 && salary<10000 )
    {
        bonusSalary=salary*(10.0/100.0);
    }
    else if(strcmp(gender,"female")==0)
    {
        bonusSalary=salary*(12.0/100.0);
    }
    printf("bonus amount you will receive is:%lf", bonusSalary);
    return 0;
}
#包括
#include//需要包含才能使用strcmp()
int main(){
char-gender[7];//gender的长度必须为7,因为“female”的长度为6,并且结尾需要一个空终止符
双薪;
双骨瓣;
printf(“在此处输入您的性别:”);
scanf(“%s”,性别);//%s不需要“&”
printf(“在此处输入您的薪资:”);
scanf(“%lf”和工资);

如果(strcmp(gender,“male”)==0&&salaryC使用的字符串是一个
char
数组(字节从0到255,每个字节对应一个字符)。要比较可以在
库中使用的两个字符串。C字符串以零结尾:这意味着在N个字符之后有一个0:

+-------+-----+-----+-----+-----+-----+-----+-----+
| index |  0  |  1  |  2  |  3  |  4  |  5  |  6  |
+-------+-----+-----+-----+-----+-----+-----+-----+
| char  | 'f' | 'e' | 'm' | 'a' | 'l' | 'e' | NUL |
+-------+-----+-----+-----+-----+-----+-----+-----+
请注意:最后一个长度是7个字符,如果你考虑最后一个“<代码> NUL”,而不是6!

顺便说一句,你使用了一个糟糕的if-else逻辑。我建议你像这样重新编写代码:

#include <stdio.h>
#include <string.h>

int main(){

    char gender[7]; //zero-terminated C-string
    double salary, bonusSalary;

    printf("enter your gender here:");
    scanf("%s", gender);

    printf("enter your salary here:");
    scanf("%lf", &salary);

    if ( strcmp(gender,"male")==0 ) {
        bonusSalary = salary<10000
          ? salary*(7.0/100.0);
          : salary*(5.0/100.0);
    } else if ( strcmp(gender,"female")==0 ) {
        bonusSalary = salary<10000
          ? salary*(10.0/100.0);
          : salary*(12.0/100.0);
    }

    printf("bonus amount you will receive is:%lf", bonusSalary);

    return 0;

}
#包括
#包括
int main(){
字符性别[7];//以零结尾的C字符串
双薪制,福利制;
printf(“在此处输入您的性别:”);
scanf(“%s”,性别);
printf(“在此处输入您的薪资:”);
scanf(“%lf”和工资);
if(strcmp(性别,“男性”)==0){

bonusSalary=salary请不要将代码和文本作为图像发布。将其作为格式化文本复制到问题中。这是否回答了您的问题?此外,C中的字符串以NUL(
'\0'
)字符结尾。这意味着类似于
的6个字符的单词“女性”
需要7个字符的缓冲区。存储超过缓冲区末尾会导致未定义的行为。编译器在编译代码时是否没有产生警告或错误?我本以为会,但我很难验证,因为您在问题中没有提供纯文本代码!请阅读编译器警告。