C programming help

Terpinator

Prominent
Apr 12, 2017
2
0
510
I am having an issue getting part of this code to work the way I would like. (This is a week 2 assignment in an Intro class so the code is probably less than efficient all around. But the line in question is this...

if (hourly < 1)
printf ("Get a job!\n");

This works fine, however I want this to really be

if (hourly =0)
printf ("Get a job!\n");

However, when I attempt this, it skips the line of code entirely. When I change it back to <1, it runs fine. Any input on why this is happening? I loaded the entire code below if anyone needs to see it.

C++:
#include <stdio.h>

int main ()

{
    float hourly , hours_worked;

    printf ("How much money do you make per hour?\n");
    scanf ("%f" , &hourly);

        if (hourly < 1)
            printf ("Get a job!\n");

        else
            printf ("How many hours per week do you normally work?\n");
            scanf ("%f" , &hours_worked);

    /* Calculating weekly pay*/
    float week_pay;
    week_pay = (hourly * hours_worked);
    printf ("You make %f dollars each week.\n" , week_pay);

    /* Calculating monthly pay*/
    float month_pay;
    month_pay = week_pay * 4;
    printf ("You make %f dollars each month\n" , month_pay);

    /* Calculating yearly pay*/
    float year_pay;
    year_pay = month_pay * 12;
    printf ("You make %f dollars each year before tax\n" , year_pay);

    /* Calculating how long it would take to reach the users money goal*/
    float save;
    printf ("How much money would you like to have?\n");
    scanf ("%f" , &save);
    printf ("You would need to work %f years and save all of the money earned to reach this goal. This does not include tax\n" , save / year_pay);

    return 0;
}
 
Solution
if (hourly == 0)

'hourly =0' is not comparison, it's assignment, making hourly equal to zero, so it becomes 'if(0)' which is untrue, so 'printf' is not executed.

DRagor

Estimable
Jan 23, 2017
26
1
4,615
if (hourly == 0)

'hourly =0' is not comparison, it's assignment, making hourly equal to zero, so it becomes 'if(0)' which is untrue, so 'printf' is not executed.
 
Solution