C programming

C programming

Post by wilde1 on Mon Sep 24, 2012 12:14 pm
([msg=69609]see C programming[/msg])

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?
wilde1
New User
New User
 
Posts: 1
Joined: Thu Sep 20, 2012 1:12 am
Blog: View Blog (0)


Re: C programming

Post by centip3de on Mon Sep 24, 2012 5:01 pm
([msg=69618]see Re: C programming[/msg])

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!
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1425
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests