0

I'm new programming i started with c a month ago. I was writing code for my school homework and i was unable verify the gender and salary in the following code that i wrote. Can someone please help me with this.

Following is my question. Q3. A company decides to give bonus to all its employees on New Year. It is decided that 5% bonus will be given to all male employees and 10% bonus will be given to female employees.Further, if the salary of an employee is less than Rs. 10,000, then the employee gets an extra 2% bonus on salary. Write a C program to enter the salary and gender of an employee and calculate the bonus that has to be given to an employee.

following is my code.

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

The code i wrote reads gender and salary but whatever you type the bonus is displayed as zero it does not read the if and else if statements.

Nick Gz
  • 17
  • 4
  • Please do not post code and text as images - [reasoning](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question). Copy it as formatted text into the question. – kaylum May 29 '21 at 11:43
  • 1
    Does this answer your question? [How do I properly compare strings in C?](https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c) – kaylum May 29 '21 at 11:43
  • Also, strings in C are terminated by a NUL (`'\0'`) character. This means a 6 character word like `"female"` requires a buffer of 7 chars. Storing past the end of a buffer results in Undefined Behaviour. – kaylum May 29 '21 at 11:44
  • Did your compiler not produce warnings or errors when you compiled the code? I would have expected it to, but it's hard for me to verify since you didn't provide plain-text code in the question! – Paul Hankin May 29 '21 at 11:47
  • Adding to @kaylum, in these cases it's better to represent things with numbers. A single int will suffice - 0 for male and 1 for female. Add on for other genders if that's part of the assignment as well. This is much more efficient and performant. – Shahe Ansar May 29 '21 at 12:05
  • 1
    Read the compiler warnings. – Cheatah May 29 '21 at 13:44

2 Answers2

1

C uses strings are an array of char (bytes raging from 0 to 255, each one corresponds to an ASCII character). To compare two string you can use strcmp() in the <string.h> library. C-strings are zero-terminated: it means that after the N character there's a 0:

+-------+-----+-----+-----+-----+-----+-----+-----+
| index |  0  |  1  |  2  |  3  |  4  |  5  |  6  |
+-------+-----+-----+-----+-----+-----+-----+-----+
| char  | 'f' | 'e' | 'm' | 'a' | 'l' | 'e' | NUL |
+-------+-----+-----+-----+-----+-----+-----+-----+

Please note: the final length is 7 character, if you consider this last "NUL", and not 6!

Btw, you are using a bad if-else logic. I suggest re-writing your code like this:

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

}

PS: It's quite common for beginners to have difficulties with pointers, arrays, C-strings, etc. I know it's going to be boring, but it can really help to read carefully references like dummies or Tutorialspoint. In my opinion they're easy to understand and are a good place to start learning.

DadiBit
  • 769
  • 10
  • 22
0

I modified your code a bit, I guess this is what you're looking for

#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); //`gender` is already a pointer to a char, you don't need the &
    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;
}