0

I wrote this following code as solution to the pset1 cash problem, it compiles and runs but once the user inputs the owed amount it replies with 0 coins to give back, which is false.

#include <stdio.h>
#include <cs50.h>
#include <math.h>
//declaration of global variables
float owed;
int coins;




//main function for gathering user input and outputting amount of owed coins
int main(void)
{
do 
{
owed = get_float("How much do i owe ya'?\n");
}
while (owed < 0);

printf("Here's your change! %i coin(s)!\n", coins);
}
// here i'm checking owed value and comparing to each coins' value, and then //comparing to the amount of coins to give back
int math(void)
{
int cents = owed * 100;
    while (cents % 25 == 0)
    {
        cents = cents - 25;
        coins++;
    }

    while (cents % 10 == 0)
    {
        cents = cents - 10;
        coins++;
    }

    while (cents % 5 == 0)
    {
        cents = cents - 5;
        coins++;
    }

    while (cents % 1 == 0)
    {
        cents = cents - 1;
        coins++;
    }
return 0;
}

What am i doing wrong? I'm thankful for any tips, been thinking about this for a day or two now.

1 Answer 1

1

Simply put, the code is printing the output without calculating the answer.

The code has a nice function called math(), but it is never called by main, so the calculation is never done.

Kudos for trying to implement a function at this point, but it's a bit early for that. At this point in your learning, I recommend doing all the coding inside main and wait until you've developed a little more skill before trying to implement a function. Learn to walk before you run!

Programming tips:

GLOBAL VARS: The code has two GLOBAL variables, owed and coins. Globals can be useful tools, but are more often a dangerous thing! A global should ONLY be created when there's a clearly justified reason for having them. First and foremost, they must be needed in main and/or MULTIPLE functions. If they are used only in a single function, they should be declared locally, inside the function or in main, where they are used. The most common problem with global vars is that they will also be declared as a local variable inside a function, creating something called a shadow variable (google it). Best practice is to avoid global variables unless absolutely necessary.

Functions: Same kind of logic applies to functions. If something is done more than once in a program, it is best practice to create a function so that the code that executes this activity will be contained in one place and can will only need to be modified in one place, should the need arise (debugging or code updates).

If something is done only once, as is the case here, then a function will have unneeded overhead and should not be created. Instead, it should just be written where it is needed.

Having said that, if you can get the code above to work correctly, it will be good experience in implementing a function. So, go ahead and fix it! ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .