这个c程序有什么问题?(如果是其他的计算方法)

时间:2017-05-21 16:35:32

标签: c

当这个程序在Eclipse中构建并运行时,它不会按预期运行:它不会要求我输入性别,它会运行并快速退出。

代码:

#include <stdio.h>

int main() {
    char edu,gen;
    int exi;
    float yos;
    printf("This program finds out the salary of an employee\nnow if you are graduate then enter g and if you post graduate then enter pg here :-  ");
    scanf("%c",&edu);
    puts("now enter the yers of service of an employee here :- ");
    scanf("%f",&yos);
    puts("now if you are female then enter f and if you are male then enter m here :- ");
    scanf("%c",&gen);
    puts("salary of an employee is ");
    if(gen=='m' && edu=='pg' && yos>=10)
        printf("1500");
    else if(gen=='m' && edu=='g' && yos>=10)
        puts("1000");
    else if(gen=='m' && edu=='pg' && yos<10)
        puts("10000");
    else if(gen=='m' && edu=='g' && yos<10)
        puts("7000");
    else if(gen=='f')
    {
        if(edu=='pg' && yos>=10)
            puts("12000");
        else if(edu=='g' && yos>=10 )
            puts("9000");
        else if(yos <10 && edu=='pg')
            puts("10000");
        else if(edu=='g' && yos <10)
            puts("6000");
        else
            puts("i dont know!!!!! ");
    }
    puts("\nnow enter any digit to exit\n");
    scanf("%d",&exi);
    printf("you enterd %d , thus good bye",exi);

    return 0;
}

输出:

  

如果您是,该程序现在可以找到员工的工资   毕业然后输入g,如果你毕业,那么在这里输入pg: -   g现在输入员工的服务年限: - 10现在如果   你是女性,然后输入f,如果你是男性,那么在这里输入: -   员工的工资是

     

现在输入任何数字退出

有没有办法解决?

3 个答案:

答案 0 :(得分:0)

您的代码中存在多个问题:

  • 列表项if(gen=='m' && edu=='pg' && yos>=10) edu是一个字母,&#39; pg&#39;不是一个字符。你应该找到另一种不当行为(比如&#39; g&#39;),或扫描一个字符串。
  • 然后您的scanf还将读取&#39; \ n&#39;,以修复:scanf(&#34; \ n%f&#34;,[..])

摘要:

#include <stdio.h>
int main(){

char edu,gen;
int exi;
float yos;
printf("This program finds out the salary of an employee\nnow if you are graduate then enter g and if you post graduate then enter p here :-  ");
scanf("%c",&edu);
puts("now enter the yers of service of an employee here :- ");
scanf("\n%f",&yos);
puts("now if you are female then enter f and if you are male then enter m here :- ");
scanf("\n%c",&gen);
puts("salary of an employee is ");
if(gen=='m' && edu=='p' && yos>=10)
   printf("1500");
else if(gen=='m' && edu=='g' && yos>=10)
puts("1000");
else if(gen=='m' && edu=='p' && yos<10)
puts("10000");
else if(gen=='m' && edu=='g' && yos<10)
puts("7000");
else if(gen=='f')
{
if(edu=='p' && yos>=10)
puts("12000");
else if(edu=='g' && yos>=10 )
puts("9000");
else if(yos <10 && edu=='p')
puts("10000");
else if(edu=='g' && yos <10)
puts("6000");
else
puts("i dont know!!!!! ");

}
puts("\nnow enter any digit to exit\n");
scanf("%d",&exi);
printf("you enterd %d , thus good bye",exi);

return 0;
}

答案 1 :(得分:0)

在您的代码中,您使用edu作为char,但“pg”不是char而是字符串。 所以你必须将edu声明为一个字符串,然后你就可以使用strcmp()函数比较edu和“pg”,如下面的代码所示。

当你使用函数puts时,你的字符串必须用新行完成,否则它不起作用。因此,您可以使用puts在字符串末尾写“\ n”,也可以在puts:scanf(“\ n ...”,...)之后在scanf中写入; \ n 或者您可以使用printf()来打印字符串,因此它可以在没有“\ n”

的情况下工作
.*?<attribute id="(\w+)" order="(\d+)".*?keyMapping.*?columnName="(\w+)".*?<\/attribute>

您还可以使用标志-Wall -Wextra -Werror进行编译,以查看错误并纠正错误。

答案 2 :(得分:0)

您的代码可以通过三种不同的方法解决:

  1. 使用单个关键字代替'pg'。 (例如 - 使用p而不是pg)
  2. 使用String
  3. 使用Array
  4. 尝试第一个,如果你知道字符串和数组,请尝试实现它们。

相关问题