#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    HueHueBrBr land
    Posts
    2
    Rep Power
    0

    Question Could you lead a hand to a beginner?


    Well, first of all, it's nice to meet everyone. This is my first time on the forums so sorry if i derp something up. :)

    Allright, here's the deal.
    I needed to make a program where the user would give a positive value in dollars, and then it would tell the user the minimum amount possible of coins needed to reach that value, in the American coin system (0.25,0.10,0.05,0.01)

    Here is my code:
    Code:
    #include <stdio.h>
    #include <cs50.h>
    #include <math.h>
    
    int main(void)
    {
        printf("How much change is owed?");
        float total = GetFloat();
        
        if (total <= 0)
         {
          printf("That`s not a valid number! \n");
          total = GetFloat();
         }
        //Obtains a value
        
        int value = roundf(total * 100);
        //Changes the total from a float to a int, also transforms it into cents
        
        int coins = 0;
        //Just defining some values and stuff
        
        do
        {
            while (value >= 25);
             {
              value = value - 25;
              coins++;
             }
             
             while (value >= 10);
             {
              value = value - 10;
              coins++;
             }
             
             while (value >= 5);
             {
              value = value - 5;
              coins++;
             }
          
             while (value >= 1);
             {
              value = value - 1;
              coins++;
             }
        }
        while (value > 0);
        
        //this substracts the value bit by bit, keeping track of the coins.
        
        printf("Here are the %d coins i owe you\n", coins);
        //prints the number of coins
        
        return 0;
    }
    For some reason, the only value that works is 0.1 and it says 4 coins. I have no idea what's going wrong! :brrr:

    Im using a compiler that came with the virtual machine, i think it's clang but instead of typing -stuff you just type "make (program)" and it compiles it.

    Thanks in advance :)
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    You have about 4-5 bugs, which are all the same type of issue:
    Code:
            while (value >= 25); <--- REMOVE THIS SEMICOLON
             {
              value = value - 25;
              coins++;
             }
    The ; ends the while loop right there. To the compiler, this looks like you typed:
    Code:
            while (value >= 25) {
                // DO NOTHING HERE
            }
         // REST OF CODE CONTINUES HERE 
             {
              value = value - 25;
              coins++;
             }
    Therefore, it never really loops through the block that you think it should be looping through. If you lose the ; then it will loop through the block.

    You have the same error with the rest of the while loops as well.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    HueHueBrBr land
    Posts
    2
    Rep Power
    0
    Originally Posted by Scorpions4ever
    You have about 4-5 bugs, which are all the same type of issue:
    Code:
            while (value >= 25); <--- REMOVE THIS SEMICOLON
             {
              value = value - 25;
              coins++;
             }
    The ; ends the while loop right there. To the compiler, this looks like you typed:
    Code:
            while (value >= 25) {
                // DO NOTHING HERE
            }
         // REST OF CODE CONTINUES HERE 
             {
              value = value - 25;
              coins++;
             }
    Therefore, it never really loops through the block that you think it should be looping through. If you lose the ; then it will loop through the block.

    You have the same error with the rest of the while loops as well.
    Ahahah that worked. Thanks so much man, i didn't know about that.

IMN logo majestic logo threadwatch logo seochat tools logo