0

So when I run my code it asks for the amount but then it doesn't print the number of coins... Help!

#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void)
{


    /* Get amount */
    printf("Enter amount: $");

    float amount;

    do
    {
        amount = GetFloat();

        if (amount < 0)   
        {
        printf("Invalid entry. Please try again: $");
        }
    } 
    while (amount < 0);



    //cents converted
    float cents = round(amount * 100);
    int coins = 0;

    do
    {
        while(cents >= 25) //Quarters
        {
            cents -= 25;
            coins++;
        }

        while(cents >= 10) //Dimes
        {
            cents -= 10;
            coins++;
        }

        while(cents >= 5) //Nickles
        {
            cents -= 5;
            coins++;
        }

        while(cents >= 1) //Pennies
        {
            cents -= 1;
            coins++;
        }
    }
    while(cents > 0);  

    printf("%d", coins); //Output

}
1

It does print it, but as you haven't included any '\n' after it, it's printing at the beginning of your command line.

printf("%d\n", coins); //Output

That ought to fix it.

HTH


If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark

2
  • Oh my gosh! I am so dumb! hahaha yep that was it. Thanks!! – RicochetRiot Sep 21 '15 at 5:41
  • No problem; a friend just helped me out of a similarly simple problem. It's nice when problems aren't too tough! :) BTW, please click the grey checkmark, so this is removed from unanswered question queue. Thanks! – Sam Gerber Sep 21 '15 at 6:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .