Thread: Arithmetic not calculating, though program runs

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    5

    Question Arithmetic not calculating, though program runs

    Hello,

    Super new to C.

    Can someone explain why the following runs, but does not perform the arithmetic operators? (Note: some of the variables initialized are for a later part of the program.)

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        float hoursWorked;
        float hourlyWage;
        float basePay;
        float otPay;
        float grossPay;
        float netPay;
        float taxBurden;
        float baseTax = 0.15;
        float progressiveTax = 0.20;
    
        basePay = hoursWorked * hourlyWage;
        otPay = (hoursWorked - 40) * hourlyWage * 0.5;
        grossPay = basePay + otPay;
    
        printf("This program will calculate your Gross Pay, Net Pay, and Tax Burden based on your hours worked and wage.\n\n");
        printf("Please enter hours worked this week and your hourly wage separated by a comma.\n\n");
        scanf("%.2f , %.2f", hoursWorked, hourlyWage);
    
        if (hoursWorked == 0)
            printf("No pay for you, slacker!");
    
        if (hoursWorked <= 40 && !0)
            printf("Your gross pay is: %.2f", basePay);
    
        if (hoursWorked >40)
            printf("Your gross pay is: %.2f", grossPay);
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    195
    For C, the statements are run in the same order they appear in the code. So you should do the calculations after you ask for the inputs.


    Also this doesn't mean what you think it does:

    Code:
    if (hoursWorked <= 40 && !0)

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    5
    Hmm. I changed the order and same results.

    It prints the "Your gross pay is: ", but all results are 0.00, no matter the inputs.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,542
    Code:
    $ gcc -Wall main.c
    main.c: In function ‘main’:
    main.c:21:11: warning: unknown conversion type character ‘.’ in format [-Wformat=]
         scanf("%.2f , %.2f", hoursWorked, hourlyWage);
               ^
    main.c:21:11: warning: unknown conversion type character ‘.’ in format [-Wformat=]
    main.c:21:11: warning: too many arguments for format [-Wformat-extra-args]
    main.c:13:11: warning: unused variable ‘progressiveTax’ [-Wunused-variable]
         float progressiveTax = 0.20;
               ^
    main.c:12:11: warning: unused variable ‘baseTax’ [-Wunused-variable]
         float baseTax = 0.15;
               ^
    main.c:11:11: warning: unused variable ‘taxBurden’ [-Wunused-variable]
         float taxBurden;
               ^
    main.c:10:11: warning: unused variable ‘netPay’ [-Wunused-variable]
         float netPay;
               ^
    main.c:15:13: warning: ‘hoursWorked’ is used uninitialized in this function [-Wuninitialized]
         basePay = hoursWorked * hourlyWage;
                 ^
    main.c:15:13: warning: ‘hourlyWage’ is used uninitialized in this function [-Wuninitialized]
    One big problem is that you're not passing variable addresses to scanf.
    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
    Nov 2020
    Posts
    5
    One big problem is that you're not passing variable addresses to scanf.
    Thank you for responding. Could you please explain more? Why isn't my scanf line sufficient? In my thinking, it scans for what the user will input. The other variables are calculations to be created. Where am I thinking incorrectly?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,542
    Like this.
    Code:
    scanf("%f , %f", &hoursWorked, &hourlyWage);
    Also, if you leave the comma in the format string, then you REALLY do have to type in a comma yourself.

    So most people generally go with
    Code:
    scanf("%f %f", &hoursWorked, &hourlyWage);
    Also, C doesn't revise the past, so you need your calculations AFTER the input.

    Code:
        scanf("%f %f", &hoursWorked, &hourlyWage);
    
        basePay = hoursWorked * hourlyWage;
        otPay = (hoursWorked - 40) * hourlyWage * 0.5;
        grossPay = basePay + otPay;
    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.

  7. #7
    Registered User
    Join Date
    Nov 2020
    Posts
    5
    Quote Originally Posted by Salem View Post

    Also, C doesn't revise the past, so you need your calculations AFTER the input.
    All of that actually made sense to me! :-D

    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 01-25-2020, 05:06 PM
  2. Program runs on mac but not on windows
    By Eric Le in forum C Programming
    Replies: 2
    Last Post: 11-11-2016, 04:16 AM
  3. Program in c that runs on a PC but not on another
    By first100 in forum C Programming
    Replies: 2
    Last Post: 03-17-2015, 09:13 AM
  4. Get an Error as soon as the program runs.
    By sara.stanley in forum C++ Programming
    Replies: 5
    Last Post: 05-28-2006, 11:47 PM
  5. multiple runs of a program
    By unregistered in forum Linux Programming
    Replies: 5
    Last Post: 03-15-2002, 07:18 AM

Tags for this Thread