-2

When this program is build and run in Eclipse, it doesn't run as expected: it doesn't ask me to enter a gender and it runs and quits very quickly.

Code:

#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;
}

output:

This program finds out the salary of an employee now if you are graduate then enter g and if you post graduate then enter pg here :- g now enter the yers of service of an employee here :- 10 now if you are female then enter f and if you are male then enter m here :-
salary of an employee is

now enter any digit to exit

Is there a way to fix?

Gabriel
  • 8,990
  • 6
  • 57
  • 101
  • 1
    [You have a sizable stack of warnings](https://pastebin.com/yUmtSw4i) not to be ignored. Start by turning on your compiler warnings to pedantic levels and *fix* (not just mask) what is flagged. After that, single step through your program with a *debugger*. – WhozCraig May 21 '17 at 16:42
  • @WhozCraig my ide is not giving any error or warning,its ellipse cdt on linux –  May 21 '17 at 16:49
  • 1
    I assumed that as the case, and the reason I said you should turn up your warning levels. – WhozCraig May 21 '17 at 16:51

3 Answers3

0

There are multiple problems in your code:

  • List item if(gen=='m' && edu=='pg' && yos>=10) edu is a char, 'pg' is not a char. You should find another abrevation (like 'g'), or scanf a string.
  • Then your scanf will read also the '\n', to fix : scanf("\n%f", [..])

summary:

#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;
}
Riccardo Bonafede
  • 610
  • 1
  • 9
  • 17
0

In your code, you use edu as a char, but "pg" isn't a char but a string. So you have to declare edu as a string, and then you will be able to compare edu and "pg" using the function strcmp() like in the code below.

When you use the function puts, your string has to be finished by a new line, or it doesn't work. So you can either write "\n" at the end of your string with puts, or you can write it in the scanf after a puts : scanf("\n...", ...); Or you can just use printf() to print strings, so it will works without "\n"

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

int main(){

char gen;
char edu[3];
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("%s",edu);
printf("now enter the yers of service of an employee here :- ");
scanf("%f",&yos);
printf("now if you are female then enter f and if you are male then enter m here :- ");
scanf(" %c",&gen);
printf("salary of an employee is ");
if (gen == 'm')
{
    if((strcmp(edu, "pg") == 0) && yos>=10)
       printf("1500");
    else if((strcmp(edu, "g") == 0) && yos>=10)
        printf("1000");
    else if((strcmp(edu, "pg") == 0) && yos<10)
        printf("10000");
    else if((strcmp(edu, "g") == 0) && yos<10)
        printf("7000");
}
else
{
    if((strcmp(edu, "pg") == 0) && yos>=10)
        printf("12000");
    else if((strcmp(edu, "g") == 0) && yos>=10 )
        printf("9000");
    else if(yos <10 && (strcmp(edu, "pg") == 0))
        printf("10000");
    else if((strcmp(edu, "g") == 0) && yos <10)
        printf("6000");
    else
        printf("i dont know!!!!! ");
}
printf("\nnow enter any digit to exit\n");
scanf("%d",&exi);
printf("you enterd %d , thus good bye",exi);

return 0;
}

You can also compile using the flags -Wall -Wextra -Werror to see your errors and to be able to correct them.

MIG-23
  • 511
  • 3
  • 11
0

Your code can be solved with three different methods :

  1. By using a single keyword instead of 'pg'. (e.g. - Use p instead of pg)
  2. By using String
  3. By using Array

Try 1st one and if you know strings and arrays, try to implement them.