# Thread: Problem with some "while" and float stuff

1. ## Problem with some "while" and float stuff

I've been getting some strange problems with my code so far. You need to input hrs as an integer, rate as a float, and then compute salary as a float. If >40 hrs are eneterd it is time and a half. Make sense? Where am I going wrong? Instructor has not gone over float stuff, so I gathered what I could.

Code:
```# include <stdio.h>

int main()

{

int hrs;
float rate;
float salary;

while(1){

printf("Enter # of hours worked (-1 to end):");
scanf("%d",&hrs);

if(hrs == -1){
return 0;
}

printf("Enter hourly rate of worker (\$00.00):");
scanf("%f",&rate);

if(hrs<=40){
salary = rate*hrs;
}

if(hrs>40){
salary = (rate*40)+((hrs-40)*rate*1.5);
}

printf("Salary is %.2f \n", salary);
}

return 0;

}```

2. If you want to use scanf() for reading in the floats, you'll probably want to use %f in place of %d. You will be told by members of this forum to not use scanf(), but in place use fgets() for controled keyboard input.

Edit: After seeing your post, I want to warn you to only use %f for the floats. . . maybe try the %i for the int.

3. Originally Posted by Kennedy
If you want to use scanf() for reading in the floats, you'll probably want to use %f in place of %d. You will be told by members of this forum to not use scanf(), but in place use fgets() for controled keyboard input.
Thanks for the advice, but I must use scanf for keyboard input due to class restrictions ATM. I will update the code above and put in %f in place of the %d

Right now the program half ass works. Inputting -1 will not terminate...

edit: it will terminate now, but not right when I enter -1. It will terminate after I put in the second value. Odd.

Also, I get this warning when compiling "warning C4244: '=' : conversion from 'double' to 'float', possible loss of data"

4. It does terminate actually, after you enter anything for rate. Try it.

edit: try this:

Code:
```{
printf("Enter # of hours worked (-1 to end):");
scanf("%d",&hrs);

if(hrs == -1){
return 0;
}```
edit: also, you need to get rid of
Code:
`while(hrs != -1)`

5. BTW, khskenny, can I work for you when you get rich? I like your style of giving time and a half for overtime work. I cannot recall a time when I haven't worked more than 40 hrs in one week.

6. Originally Posted by Kennedy
BTW, khskenny, can I work for you when you get rich? I like your style of giving time and a half for overtime work. I cannot recall a time when I haven't worked more than 40 hrs in one week.

Hah, I just saw that too. I'll fix it I was just throwing stuff together to get the overall thing to work and did not pay much attention to detail.

Yeah, you can work for me. If you put in over 40hrs you'll be loaded...

7. I may not have mentioned that it needs to loop as many times as you want it to before putting in -1. If that is the case, do I still get rid of the while statement and just throw it in the if part?

8. Using cDev's example, you'd want to use while (1).

9. Well, I think it works like the question in my book states! Thanks for the help.

However, I thought my teacher mentioned using:

Code:
`while(hrs != -1)`
Anybody see a way to do this? The program DOES work as intended now, I was just curious.

10. You'd have to do it like this:
Code:
```printf("Enter # of hours worked (-1 to end):");
scanf("%d",&hrs);
while (hrs != -1){
printf("Enter hourly rate of worker (\$00.00):");
scanf("%f",&rate);

if(hrs<=40){
salary = rate*hrs;
}

if(hrs>40){
salary = (rate*40)+((hrs-40)*rate*1.5);
}

printf("Salary is %.2f \n", salary);

printf("Enter # of hours worked (-1 to end):");
scanf("%d",&hrs);
}```
And you can get rid of the warning by changing the floats to doubles. But you'd also have to change the scanf for rate so it uses %lf.

11. Thanks swoopy that's exactly what I was looking for. So simple, don't know why I missed it.

Could I just use all floats and keep things the way I have it?

12. >Could I just use all floats and keep things the way I have it?
Yes, and if you still have the warning, you could eliminate the warning by changing your constants to float. For example change 1.5 to 1.5f. The default for a floating point constant is double.

13. Or you can add a cast in front: (float) 1.5

14. Or you could use an uppercase F: 1.5F Uppercase is often preferred, simply because when you go to add an 'L' (for a long literal) a lowercase 'l' looks like a numeric 1 (one). (That being said, I usually use a lowercase 'f' myself.)

15. Originally Posted by dwks
Or you could use an uppercase F: 1.5F Uppercase is often preferred, simply because when you go to add an 'L' (for a long literal) a lowercase 'l' looks like a numeric 1 (one). (That being said, I usually use a lowercase 'f' myself.)
I have never even tried the upper "f". Considering that the folks that made C used some *nix I figured CaSe was important.