Beginner: help?

This is a discussion on Beginner: help? within the C Programming forums, part of the General Programming Boards category; So... I'm feeling really retarded that it's giving me so much trouble. I'm working my way through a book and ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    Beginner: help?

    So... I'm feeling really retarded that it's giving me so much trouble.
    I'm working my way through a book and I had an idea maybe I could figure
    something out on my own. It's a simple calculator I want it to allow you to input:

    hourly wage, hours per week, and tax(for state).

    So my problem. I'm using gets(); to collect char and atoi to convert to float and
    somewhere along the way when multiplying the floats by each other the product
    is wrong for the first answer and won't show for the second.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	float monthly,monthlyt,h,t,p;	
    
    	char hours[3],pay[8],tax[5];
    
    	char key;
    
    	printf("Hours worked weekly:");
    	gets(hours);
    	h=atoi(hours);
    	
    	printf("Pay per hour:");
    	gets(pay);
    	p=atoi(pay);
    
    	printf("State tax decimal(or what you have taken out.)\n");
    	gets(tax);
    	t=atoi(tax);
    	monthlyt=(t)*(monthly);
    	
    	monthly = atoi(hours)*p*4;
    
    	printf("Your monthly pay is %.4f before taxes.\n",monthly);
    	printf("Your monthly pay after taxes is %.4f\n",monthlyt);
    
    	printf("Press the \"Enter\" key to exit the program.");
    	key=getchar();
    	
    
    return(0);
    }

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    atoi stands for ASCII to integer. Reconsider the user of gets(); it is (1) not an appropriate solution for this problem and (2) generally a bad practice.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    I see I feel even more retarded now. May I ask how I'd go about making a char a float?

  4. #4
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    atof() -- ASCII to float.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    You rock dude thanks for that helped me a ton with that. I feel a lot better, and kind of worse, almost. Better to have gotten it now and worse for not having been able to find that out by googling. Anyways... final result....

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      char a[5],b[5],c[5];
      float p,t,q,ttl,qtl;
    
    	printf("hourly...:\n");
    	gets(a);
    	p=atof(a);
    
    	printf("hours...:\n");
    	gets(b);
    	t=atof(b);
    
    	printf("tax...:\n");
    	gets(c);
    	q=atof(c);
    	
    	ttl=p*t;
    	qtl=q*p*t;
    
    	printf("so... ttl is... %.4f\n",ttl);
    	printf("after... qtl is... %.4f\n",ttl-qtl);
    return(0);
    }
    Last edited by Baltz; 11-12-2010 at 02:56 AM. Reason: Asked a worse question then this thread was originally opened for

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ADO.NET in VS2008 or gdi+ (beginner question)
    By robgar in forum C# Programming
    Replies: 3
    Last Post: 10-04-2009, 02:40 AM
  2. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  3. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 10:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21