'how to verify a string and double in c
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.
Solution 1:[1]
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;
}
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |