Thread: Problem with the code

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    3

    Problem with the code

    Hi

    I writing a salary calculation with C with different pay range, but it is keep show me the error or can't proceed to the next line when come to if else method.

    I try to make it output like thisProblem with the code-1-png

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main()
    {
    char workerName[100];
    char workerNric[14];
    char payCategory[2];
    float totalHour;
    float a1 = 5;
    float a2 = 7;
    float m1 = 10;
    float m2 = 15;
    float bb = 20;
    float grossPay, overTime, nettPay;
    
    
    printf("Name \t:\t");gets(workerName);
    printf("NRIC \t:\t");gets(workerNric);
    printf("Category A1, A2, M1, M2 or BB \t:\t");gets(payCategory);
    printf("Total Hours\t:\t");
    scanf("%f",&totalHour);
    printf("\n\t\tSyarikat Smart Store Hypermarket Sdn. Bhd.");
    printf("\n\t\t==========================================");
    printf("\nName : %s", workerName);
    printf("\nNRIC : %s", workerNric);
    printf("\nCategory : %s", payCategory);
    printf("\nTotal Hours : %.2f", totalHour);
    #define maxHour 44
    if(*payCategory == a1)
    {
    if(totalHour <= 44)
    {
    grossPay = 44 * a1;
    overTime = 0;
    nettPay = grossPay;
    }
    else if(totalHour >= 44)
    {
    grossPay = 44 * a1;
    overTime = ((totalHour - 44) * a1 * 1.5);
    nettPay = grossPay + overTime;
    }
    }
    printf("\nGross Pay : RM %.2f", grossPay);
    printf("\nOver Time : RM %.2f", overTime);
    printf("\nNett Pay : RM %.2f", nettPay);
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2019
    Posts
    213
    This:

    Code:
    if(*payCategory == a1)
    Note: payCategory is a character array (of length 2)

    a1 is a double, when I think you mean "A1", the text you expect the user to enter

    The problem is also that you must use "strcmp" or something related to compare strings.

    Further, the length of payCategory is only 2, but if you expect to store a string that HAS 2 letters (which you do), there isn't enough room. Strings must include room for a 0 (zero) to terminate the string. If you need to store 2 letters, you must make at least room for 3.

  3. #3
    Registered User
    Join Date
    Jun 2019
    Posts
    3
    Thank you Niccolo, you got me hits. I will try and error for this again.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,537
    Never EVER use gets()
    Why gets() is bad / Buffer Overflows - Cprogramming.com

    Code:
    #define maxHour 44
    if(*payCategory == a1)
    {
    if(totalHour <= 44)
    If you're going to make a #define, then at least use it in all the places where you've written 44.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2019
    Posts
    3
    Hi Salem

    It it appreciate if you can advise not to use gets instead scanf how to get the result of name with space require e.g "Peter Pan Rushun"

    int payCategory; ??

    Quote Originally Posted by Salem View Post
    Never EVER use gets()
    Why gets() is bad / Buffer Overflows - Cprogramming.com

    Code:
    #define maxHour 44
    if(*payCategory == a1)
    {
    if(totalHour <= 44)
    If you're going to make a #define, then at least use it in all the places where you've written 44.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,537
    The FAQ shows you how to replace gets() with fgets().
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in my code
    By Sietor in forum C Programming
    Replies: 9
    Last Post: 08-16-2016, 03:37 PM
  2. Code problem or compiler problem....?
    By miloki in forum C Programming
    Replies: 4
    Last Post: 03-05-2015, 12:48 AM
  3. code problem..
    By broken_heart in forum C Programming
    Replies: 12
    Last Post: 09-18-2009, 04:45 PM
  4. Problem with my code :( please help
    By yrostran in forum C Programming
    Replies: 4
    Last Post: 11-29-2006, 04:22 PM

Tags for this Thread