Thread: Problem with some "while" and float stuff

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    17

    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;
    
    }
    Last edited by khskenny; 09-22-2006 at 03:00 PM.

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    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.
    Last edited by Kennedy; 09-22-2006 at 02:29 PM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    Quote 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"
    Last edited by khskenny; 09-22-2006 at 02:31 PM.

  4. #4
    Registered User cDev's Avatar
    Join Date
    Jun 2006
    Posts
    26
    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)
    Last edited by cDev; 09-22-2006 at 02:36 PM.

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    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. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    Quote 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. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    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. #8
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Using cDev's example, you'd want to use while (1).

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    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. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    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. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Or you can add a cast in front: (float) 1.5

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM