Thread: new to c. problem with exercise.

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    7

    new to c. problem with exercise.

    I just started working with C and I'm reading C How to program. I really enjoy this book over all other programming books that I've read simply because this book has numerous exercises at the end of the chapters. This is helping me to really make sure that I have learned what the chapter was trying to teach. However, I've now run into a problem with some of the exercises. This one in particular has me stumped. The basic problem is that you are first supposed to find out what type of employee the person is (number from 1 to 4). If 1 (a manager) you receive a flat weekly salary. No problem there. If 2 (hourly employee) you also have to input your hours for the week and hourly rate. Then the program is supposed to output their weekly pay based off hours plus any possible overtime. There are 2 other employee types, but I haven't even gotten there yet because I'm having trouble calculating the hourly wage for employee 2. It keeps giving me back the results that I made 0.00 this week. Can anyone see from my code what I'm doing wrong? Thanks.

    #include <stdio.h>

    int main()
    {

    int empType = 0, hoursWorked = 0;
    double hourlyRate = 0, overTime = 0;

    printf("What type of employee are you?");
    scanf("%d",&empType);

    switch (empType){

    case 1:
    //manager
    printf("You have made $500.00 this week.\n");
    break;
    case 2:
    //hourly
    printf("Please enter the number of hours you worked.");
    scanf("%d", &hoursWorked);
    printf("Please enter your hourly rate.");
    scanf("%f", &hourlyRate);
    if ( hoursWorked > 0 )
    {
    overTime = (double)((hoursWorked - 40) * (hourlyRate * 1.5));
    }
    printf("You made %.2f this week.\n", overTime);
    break;
    case 3:
    //commission
    break;
    case 4:
    //pieceworkers
    break;
    default:
    printf("Sorry, I don't have functions written for that type of employee.");
    break;
    }

    return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    7
    case 2:
    //hourly
    printf("Please enter the number of hours you worked.");
    scanf("%d", &hoursWorked);
    printf("Please enter your hourly rate.");
    scanf("%f", &hourlyRate);
    if ( hoursWorked > 0 )
    {
    overTime = (double)((hoursWorked - 40) * (hourlyRate * 1.5));
    }
    printf("You made %.2f this week.\n", overTime);
    break;

    If you enter 40 as the number of hours worked, you'll end up with zero because of your multiplication. You need to first test to see if the hours worked is over 40 hours, something like:
    if (hoursWorked>40){
    overTime = (double)((hoursWorked - 40) * (hourlyRate * 1.5));
    totalTime = double((40*hourlyRate)+overTime);
    }
    else {totalTime = double(hoursWorked*hourlyRate);}

    and then print out totalTime instead of overTime. Hope that helps.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    7

    I tried to modify it but it didn't change the results

    I made your recommended modifications, but it didn't change the output. I also started working on the 3rd type and it's also not outputting correct data, so now I'm really stumped. Here's my revised code.

    #include <stdio.h>

    int main()
    {

    int empType = 0, hoursWorked = 0;
    double hourlyRate = 0, overTime = 0, commissionRate = 250.00, salesAmount = 0, totalAmount = 0;

    printf("What type of employee are you?");
    scanf("%d",&empType);

    switch (empType){

    case 1:
    //manager
    printf("You have made $500.00 this week.\n");
    break;
    case 2:
    //hourly
    printf("Please enter the number of hours you worked.");
    scanf("%d", &hoursWorked);
    printf("Please enter your hourly rate.");
    scanf("%f", &hourlyRate);
    if ( hoursWorked > 40 )
    {
    overTime = (double)((hoursWorked - 40) * (hourlyRate * 1.5));
    totalAmount = (double)((40* hourlyRate) + overTime);
    }
    else
    {
    totalAmount = double(hoursWorked * hourlyRate);
    }
    printf("You made %.2f this week.\n", totalAmount);
    break;
    case 3:
    //commission
    printf("Please enter your total amount of sales for the week.");
    scanf("%f", &salesAmount);
    printf("Your pay is %.2f\n", 250.00 + (salesAmount * .057));
    break;
    case 4:
    //pieceworkers
    break;
    default:
    printf("Sorry, I don't have functions written for that type of employee.");
    break;
    }

    return 0;
    }

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    7

    Nevermind

    I figured out what the problem was. I was using the %f format specifier in the scanf function call when I was assigning a value to a double. I learned later in the next chapter that I have to use the %lf specifier to correctly assign the value to a double. Thanks.

  5. #5
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    Please use code tags.

    Code tags make code easier to read for those who might help you.

    [ c o d e ]

    .. place code here

    [ / c o d e ]

    Example:
    Code:
    #include <stdio.h>
    
    int main()
    {
            printf("Hello, world.");
            return 0;
    }
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Problem with extremely beginner exercise
    By Molokai in forum C++ Programming
    Replies: 11
    Last Post: 05-08-2007, 09:05 AM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. C++ Primer 4th Edition, Problem with Exercise
    By Kaidao in forum C++ Programming
    Replies: 4
    Last Post: 07-15-2006, 11:13 AM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM