-
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);
}
-
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.
-
I see I feel even more retarded now. May I ask how I'd go about making a char a float?
-
atof() -- ASCII to float. :)
-
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);
}