Thread: whats wrong with this code?

  1. #1
    Unregistered
    Guest

    Question whats wrong with this code?

    /* The program determines the gross pay and ouputs the following
    format:

    --------------------------------------------------------------------------
    Clock# Wage Hours Gross
    --------------------------------------------------------------------------
    98401 10.60 51.0 540.60

    */

    #include <stdio.h>

    main ()
    {
    /* declare variables */

    int clock;
    float wage;
    float hours;
    float gross;

    /* set values */
    gross = wage * hours;

    /* begin output */

    printf ("--------------------------------------------------------------\n");
    printf ("Clock# Wage Hours Gross \n");
    printf ("--------------------------------------------------------------\n");
    printf ("Please enter a Clock#");
    scanf("%i",&clock);
    printf ("Please enter wage");
    scanf("%i",&wage);
    printf ("Please enter hours worked");
    scanf("%i",&hours);
    printf ("%i %f %f %f\n", clock, wage, hours, gross);
    }

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You'll need to do -

    /* set values */
    gross = wage * hours;

    after the variables have meaningful values. This expression will only be evaluated once, gross won't be updated everytime wage or hours is.

  3. #3
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    ...and main should return an int.

  4. #4
    Unregistered
    Guest

    Question hmmmm

    I tried moveing the gross formula later in the problem but I still don't seem to get the entered values for wage or hours to display or gross. all 3 come up as zeros. But clock# works....

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Check your format specifications in scanf, you're trying to read floats as integers.

  6. #6
    Unregistered
    Guest

    thats fixed.

    thnx for the help. changeing those %i to %f fixed it.

    one question though. How do i limit the decimal places too 2 places?

  7. #7
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Change your output statement to -

    printf ("%i %g %g %g\n", clock, wage, hours, gross);

  8. #8
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    ...sorry should be -

    printf ("%i %.2f %.2f %.2f\n", clock, wage, hours, gross);

  9. #9
    Unregistered
    Guest

    Cool cool all set. thnx again :)


  10. #10
    Unregistered
    Guest
    so how did you solve the problem.i am having the same thing with this code.it came out as 0.0 0.0 0.0.show me you code?

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM