Can anyone tell me why this keeps resulting to 0? (C program) [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Can anyone tell me why this keeps resulting to 0? (C program) [closed]


case 2:
if(hrswrk <= 40)
pay = wage * hrswrk;
else
pay = (40 * wage) + (hrswrk - 40) * (wage * 1.5);
printf("Hourly Workers is selected. n");
printf("Enter number of hours worked: n");
scanf("%i",&hrswrk);
printf("Enter hourly wage: n");
scanf("%i",&wage);
printf("Your pay is Php%.2f",pay);
break;



This is just a part of my program and its frustrating that I can't even solve this, maybe the mistake is that I placed an if else statement within a switch? I'm so lost.



This question appears to be off-topic. The users who voted to close gave this specific reason:





This isn't a complete example, and you're not explaining just what is "resulting to 0". For one, you're not checking any return values from scanf() so you don't know if they worked at all.
– Andrew Henle
Aug 10 at 9:43


scanf()





Most likely you are mixing integers and float. This can't be answered without the variable declarations.
– Lundin
Aug 10 at 9:46




4 Answers
4



Do your operations after you get required data:


case 2:
printf("Hourly Workers is selected. n");
printf("Enter number of hours worked: n");
scanf("%i",&hrswrk);
printf("Enter hourly wage: n");
scanf("%i",&wage);

if(hrswrk <= 40)
pay = wage * hrswrk;
else
pay = (40 * wage) + (hrswrk - 40) * (wage * 1.5);

printf("Your pay is Php%.2f",pay);
break;



You need to compute pay after the values for hrswrk and wage have been established, i.e. move the if block after the final scanf call.


pay


hrswrk


wage


if


scanf



Although some languages work the way you have written it, C doesn't.



Note also that you should always check the return value of scanf, in order to check if the reading has been successful.


scanf





curious to know what languages work this way.
– Gaurav Sehgal
Aug 10 at 9:44





@GauravSehgal: You can get C++ to do this using actors. See Boost Phoenix for an established use case.
– Bathsheba
Aug 10 at 9:44



case 2:
printf("Hourly Workers is selected. n");
printf("Enter number of hours worked: n");
scanf("%i",&hrswrk);
printf("Enter hourly wage: n");
scanf("%i",&wage);
if(hrswrk <= 40)
pay = wage * hrswrk;
else
pay = (40 * wage) + (hrswrk - 40) * (wage * 1.5);
printf("Your pay is Php%.2f",pay);
break;



Execute your if-condition after scanf()


if-condition


scanf()



In your code you are executing if-condition before reading hrswrk and wage. Read them first and execute the if-logic.


if-condition


hrswrk


wage


if-logic



Try this modified code :-


case 2:
printf("Hourly Workers is selected. n");
printf("Enter number of hours worked: n");
scanf("%i", &hrswrk);
printf("Enter hourly wage: n");
scanf("%i", &wage);

if (hrswrk <= 40) // if condition after scanning wage and hrswrk
pay = wage * hrswrk;
else
pay = (40 * wage) + (hrswrk - 40) * (wage * 1.5);
printf("Your pay is Php%.2f", pay);
break;



This will work.





if is not a loop.
– interjay
Aug 10 at 9:46


if

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard