wilde1 wrote:Hey , im trying to really get a feel for Programming. I realllly enjoy it,i've been looking for a programming community to get familiar with , so as to not become detered from repeditive coding from solving porblem issues.I would be grateful to any1 who can help. Im taking a cs50 course online, one of the criteria is to convert change into coin amounts (IE quarter, nickel, dime, pennies). Im having issues in c proggramming, when using a "float" i canno't display the change in the hndredth's place? promise your not wasting your time, im putting a real effort into this , just i don't knw where else oto turn. thanks. My code so far:
#include<stdio.h> #include<cs50.h> #include<math.h>
int
main (void)
{ float x,i;
x = .25;
do
{ printf("How much change is owed? \n") ;scanf("%f", &i); }
while (i < .01);
{ i = (i) / (x); printf("%f \n",i);}
}
-- Mon Sep 24, 2012 1:26 pm --
i just changed the the div to fmod i = fmod (i, x); printf("%.11f \n",i); ,i get an out put number : 0.15999999642 . i need it to round to the hundreths place?
Congrats, this is an easy fix for you! If you want to shorten the length of the float you're printing, simple put "-the minimum number of characters to be printed-%f". I took you program and copied it, line for line, (though I did reorganize it) and just added what I told you above, here's what it looks like:
- Code: Select all
#include<stdio.h>
#include<cs50.h>
#include<math.h>
int main (void)
{
float x,i;
x = .25;
do
{
printf("How much change is owed? \n") ;
scanf("%f", &i);
}
while (i < .01);
{
i = (i) / (x);
printf("%4.2f \n",i); /* This is THE ONLY line I edited!
}
}
return 0;
}
Works just fine one my end. Also, you might want to check this out for more information:
http://www.cplusplus.com/reference/clib ... io/printf/Some sidenotes:
1. You might want to check out this site:
http://www.cplusplus.com . It has great online documentation of both C, and C++ functions
2. You should use code tags surrounding your code when posting code on the forums, it really makes it more readable for us
3. You should organize your code so that others know what's going on immediately, without having to hunt for clues
4. ALWAYS USE RETURN 0; !
5. You don't need to do "int main(void)", int main() works just fine
6. Welcome to HTS!