Thread: Could you help me with my code?

  1. #1
    Registered User
    Join Date
    Jul 2019
    Posts
    2

    Question Could you help me with my code?

    I am currently making a program to return change in the lowest amount of coins. I thought I did everything right, but when I input something, it only takes me back to the original input question. Can anyone see where I went wrong? I am a super noob when it comes to programming. Thanks!


    Code:
    #include <cs50.h>
    #include <stdio.h>
    #include <math.h>
    
    
    int main(void)
        
    {
        //Variables for Money Given, Quarters, Dimes, Nickels, and Pennies
        float money;
        int quarter;
        int dime;
        int nickel;
        int penny;
        int change;
        int changez;
        int remainQ;
        int remainD;
        int remainN;
       
        
          
        do
        {
            money = get_float("Change owed: ");
            change = round(money * 100);   
        }
            
        while (change > 0);
        int i = 25;
        quarter = (change/i);
        remainQ = (change%i);
        dime = (remainQ/10);
        remainD = (i%10);
        nickel = (remainD/5);
        remainN = (i%5);
        penny = (remainN/1);
        changez = (quarter+dime+nickel+penny);
        {
        printf("%i", changez);
        }
       return money;
    
    }

  2. #2
    Registered User
    Join Date
    May 2019
    Posts
    214
    Well, this:

    Code:
    do    {
    
            money = get_float("Change owed: ");
    
            change = round(money * 100);   
    
        }
    
        while (change > 0);
    Will repeat as a long as the input produces a positive, non-zero result.

    You'd have to enter a negative value for this loop to stop.

    Perhaps you meant change <= 0, refusing a zero or negative value

  3. #3
    Registered User
    Join Date
    Jul 2019
    Posts
    2
    Quote Originally Posted by Niccolo View Post
    Well, this:

    Code:
    do    {
    
            money = get_float("Change owed: ");
    
            change = round(money * 100);   
    
        }
    
        while (change > 0);
    Will repeat as a long as the input produces a positive, non-zero result.

    You'd have to enter a negative value for this loop to stop.

    Perhaps you meant change <= 0, refusing a zero or negative value
    Thank you!! That was exactly it. I changed one or two more things to tweak it, but good eyes!

    new code, if you care haha
    Code:
    do
        {
            money = get_float("Change owed: ");
            change = round(money * 100);   
        }
            
        while (change <= 0);
        int i = change;
        quarter = (change/25);
        remainQ = (change%i);
        dime = (remainQ/10);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  3. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  4. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  5. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM

Tags for this Thread