Thread: Help with a problem (Im new to this)

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    2

    Exclamation Help with a problem (Im new to this)

    Hi everyone, I have decided to start a course on programming called CS50, and one of the tasks Im asked to do is program in gedit using C, a simple program that asks for the change owed to the customer and then gives back the number of coins needed to satisfy said change. Nevertheless, the cashier is greedy, and likes to have many coins, so he has to give the bigger amount coins first, starting with 0.25, then 0.10 then 0.05 and then 0.01.

    For example, if I input that the change owed is 0.41, the program must return a value of 3 coins, a 0.25, a 0.05 and a 0.01.

    I have written the following:
    insert
    Code:
    #include <stdio.h>
    #include <cs50.h>
    
    
    int main(void)
    {
    float change;
    int coins;
    coins = 0;
    printf("How much change is owed\n");
    change = GetFloat();
    for (;change>0.00;)
    {
        if (change>=0.25)
        {
            change-=0.25;
            coins+=1;
        }
        else if (change>=0.10 && change<0.25)
        {
            change-=0.10;
            coins+=1;
        }
        else if (change>=0.05 && change<0.10)
        {
            change-=0.05;
            coins+=1;
        }
        else if (change>=0.01 && change<0.05)
        {
            change-=0.01;
            coins+=1;
        }
    }
    printf("%d\n",coins);
    }
    


    But for some reason it is answering to the numbers divisible by 0.25, as soon as i say 0.41, or 0.01 it just jumps a line and does nothing.

    Please help me solve this problem not by offering me a better code completely, but by working from this code. Thanks in advance

    I really am new to this so please bear with my negligence xD

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    My preference would be to multiply everything by 100 and do everything in pennies. If you do it in pennies, then you can use the % (modulus) operator, and it becomes much simpler.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    2
    Quote Originally Posted by Elkvis View Post
    My preference would be to multiply everything by 100 and do everything in pennies. If you do it in pennies, then you can use the % (modulus) operator, and it becomes much simpler.
    Ok I've changed it and now it works for al the values I try except for 4.2. I've read that 4.2 as a float is 4.1999999999 and when converting it into an int it becomes 419, so how can I fix this using the code I have because its just one damn value. Here is the code:

    insert
    Code:
    #include <stdio.h>
    #include <cs50.h>
    
    
    int main(void)
    {
    int coins;
    int change25;
    int change10;
    int change5;
    int intchange;
    int change1;
    coins=0;
    printf("How much change is owed\n");
    intchange = GetFloat()*100;
    if (intchange>0)
    {
    if (intchange/25>=1)
    {
    change25 = intchange/25;
    coins+=change25;
    intchange=intchange%25;
    }
    if (intchange/10>=1)
    {
    change10 = intchange/10;
    coins+=change10;
    intchange=intchange%10;
    }
    if (intchange/5>=1)
    {
    change5 = intchange/5;
    coins+=change5;
    intchange=intchange%5;
    }
    if (intchange/1>=1)
    {
    change1 = intchange/1;
    coins+=change1;
    intchange=intchange%5;
    }
    printf("%d\n",coins);
    }
    }
    Thanks in advance

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    You could try a double instead, but I suspect that will have the same effect.

    Another option could be to do something like this to get your initial amount:

    Code:
    int dollars = 0, pennies = 0;
    do
    {
        printf("Enter amount: ");
        int result = scanf("%d.%2d", &dollars, &pennies);
    } while (result != 2);
    intchange = (dollars * 100) + pennies;
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Try this
    Code:
    intchange = GetFloat()*100+0.5
    to round it to nearest cent.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-09-2014, 06:46 PM
  2. Problem passing argument into function, basic problem
    By tsdad in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2013, 12:09 PM
  3. Replies: 2
    Last Post: 01-06-2013, 07:49 AM
  4. Replies: 1
    Last Post: 12-07-2012, 10:00 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM