2021年3月24日星期三

How to create an automatic change machine in C

I am new to programming. I've been trying to create something that would give me the minium amount of us cents coints required to a specific amount of dollars. For example, minimum amount of coins needed for 16 cents would be 3 (10 + 5+ 1)

#include <stdio.h>  #include <cs50.h>  #include <math.h>    float get_positive_change(void);    int main (void)  {   //Get owed dollar amount   float change = get_positive_change();    //Round float to int   int cents = round(change*100);    //Initialize counter for coins needed)   for(int i = 0; cents < 0; i++)   {       if(cents >= 25)     {         cents = cents - 25;         }     else if(cents >= 10)     {         cents = cents - 10;       }     else if(cents >= 5)     {         cents = cents - 5;       }     else if(cents >= 1)     {         cents = cents - 1;       }     return cents;     return i;   }      printf("%d\n", i);  }        float get_positive_change(void)  {  float n;  do  {      n = get_float("Change owed: ");  }  while (n < 1); //Express need for number to be < 1  return n;  }  

I do realize that "int i" does not exist outside of that function so, I'd like to know how to fix that. Also, I am not sure how return works yet, I was trying to play around and see if it worked. Any help to improve this code lines would be MUCH appriciated. I've been trying to solve this for a while now. Wish you all a great day

https://stackoverflow.com/questions/66792627/how-to-create-an-automatic-change-machine-in-c March 25, 2021 at 11:27AM

没有评论:

发表评论