What's wrong with my code

This is a discussion on What's wrong with my code within the C Programming forums, part of the General Programming Boards category; Hi, I am new to this forum and to programming for that matter. I am following the CS50 (computer science) ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    5

    What's wrong with my code

    Hi,

    I am new to this forum and to programming for that matter.
    I am following the CS50 (computer science) online course
    from last year and I have problems with one of the assignments.
    I am supposed to write a code to calculate the amount of coins
    it takes to give change from certain amount of dollars inputed.
    There are only 4 coins used (quarter, dime, nickel and penny).
    I have found some other similar programs online but I would really
    like to know what's wrong with my code.

    And I can't believe that somebody asked identical question a few
    minutes before me.

    Code:
    #include <math.h>
    #include <stdio.h>
    
    int main (void)
    {
        // asks user to enter the amount and veryfies the input
        
        float dollars;
        do
        {
            printf("Enter the amount to get change for:");
            scanf("%f", &dollars);
        }
        while (dollars <= 0);
        
        // converts dollars into cents
        
        int cents = round(dollars * 100);
        
        // calculates the number of coins needed to give change
        
        int quarter = 25;
        int dime = 10;
        int nickel = 5;
        int penny = 1;
        int coins = 0;
        
        while (cents >= quarter)
        {
            cents -= quarter;
            coins++;
        }
        
        while (cents >= dime)
        {
            cents -= dime;
            coins++;
        }
        
        while (cents -= nickel)
        {
            cents -= nickel;
            coins++;
        }
        
        while (cents -= penny)
        {
            cents -= penny;
            coins++;
        }
        
        printf("%d\n", coins);
    
    return 0;
    }
    Thanks for any help

    Kamil

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Code:
     while (dollars <= 0);
    This has no effect.It is equivalent to this
    Code:
     while (dollars <= 0)
      {
            
       }
    Maybe this is not what you wanted to do.
    Good for you that you help the other guy with the same post.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    It was supposed to be the part of do-while loop

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Oh you are right.Sorry :/

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    first your declaration is wrong.
    int quarter = 25; int dime = 10;
    int nickel = 5;

    int penny = 1;
    int coins = 0;
    you have to declarate the type at the beginning, and if you want to give your variable new values just write (varible name) = (new value);
    same thing with
    int cents = round(dollars * 100);

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by c-da View Post
    first your declaration is wrong.

    you have to declarate the type at the beginning, and if you want to give your variable new values just write (varible name) = (new value);
    same thing with
    Not in C99.He can in the new standard

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    Quote Originally Posted by std10093 View Post
    Not in C99.He can in the new standard
    thanks for the info.

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    while (cents -= nickel)
    {
        cents -= nickel;
        coins++;
    }
        
    while (cents -= penny)
    {
        cents -= penny;
        coins++;
    }
    Typos?

    Bye, Andreas

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    Are you certain that the assignment only asks how many coins, and not how many of each type?

  10. #10
    Dweeb dojha00's Avatar
    Join Date
    Feb 2012
    Location
    Global
    Posts
    23
    Quote Originally Posted by kamil77 View Post
    Code:
       while (cents -= nickel)
        {
            cents -= nickel;
            coins++;
        }
        
        while (cents -= penny)
        {
            cents -= penny;
            coins++;
        }
    Think about your conditions in while loop....

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    Thanks Andreas and dojha00.

    Thank you guys for taking your time to go through my code and pointing out the silly mistake that I have made.
    Everything is working fine now. I guess what I needed was another set of eyes on it.

    Regards
    Kamil

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is there anything wrong with this code?
    By rightbrainer in forum C Programming
    Replies: 7
    Last Post: 04-23-2009, 03:58 AM
  2. if anyone can tell me what's wrong with this code
    By roier in forum C Programming
    Replies: 2
    Last Post: 08-02-2008, 01:20 AM
  3. something wrong with code
    By dankassdann in forum C++ Programming
    Replies: 5
    Last Post: 10-27-2006, 12:27 PM
  4. what is wrong with this code?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 08:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21