Home > Enterprise >  I am doing the cs50 pset 1 cash and whenever i am running this code, the terminal is getting blank
I am doing the cs50 pset 1 cash and whenever i am running this code, the terminal is getting blank

Time:05-30

I am doing the cs50 pset 1 cash and whenever i am running this code, the terminal is getting blank with every enter i press. i write a number and it does not take it. i believe there is a problem with my get_cents() function but i cannot understand what it is.

#include <cs50.h>
#include <stdio.h>

int get_cents(void);
int calculate_quarters(int cents);
int calculate_dimes(int cents);
int calculate_nickels(int cents);
int calculate_pennies(int cents);


int main(void)
{
// Ask how many cents the customer is owed
int cents = get_cents();

// Calculate the number of quarters to give the customer
int quarters = calculate_quarters(cents);
cents = cents - quarters * 25;

// Calculate the number of dimes to give the customer
int dimes = calculate_dimes(cents);
cents = cents - dimes * 10;

// Calculate the number of nickels to give the customer
int nickels = calculate_nickels(cents);
cents = cents - nickels * 5;

// Calculate the number of pennies to give the customer
int pennies = calculate_pennies(cents);
cents = cents - pennies * 1;

// Sum coins
int coins = quarters   dimes   nickels   pennies;

// Print total number of coins to give the customer
printf("%i\n", coins);
}

int get_cents(void)
{
int n;
do
{
    n = get_int("Money Owed: ");
}
while (n <= 0);
return n;
}


int calculate_quarters(int cents)
{
int i = 0;
if (cents >= 25)
{
    do
    {
        i  = 1;
    }
    while (cents >= 25);
    return i;
}
return 0;
}

int calculate_dimes(int cents)
{
int i = 0;
if (cents >= 10)
{
    do
    {
        i  = 1;
    }
    while (cents >= 10);
    return i;
}
return 0;
}

int calculate_nickels(int cents)
{
int i = 0;
if (cents >= 5)
{
    do
    {
        i  = 1;
    }
    while (cents >= 5);
    return i;
}
return 0;
}

int calculate_pennies(int cents)
{
if (cents >= 1)
{
    return cents;
}
return 0;
}

This happened when I added the return i line to all the functions, and i dont know what to do.

CodePudding user response:

All your calculate_* functions do not decrement cents as they should. They are infinite loops.

But, all these functions are just division by repetitive subtraction. How about (e.g.):

int
calculate_quarters(int cents)
{

    return cents / 25;
}

Are you required to use functions in this way? You'd probably be better off with a table of denominations (e.g.) int denom[] = { 25, 10, 5, 1 }; and loop on that.


Here is a refactored version. It is annotated:

#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>

#define COUNTOF(_arr)       (sizeof(_arr) / sizeof(_arr[0]))

const int denoms[] = { 25, 10, 5, 1 };
const char *names[] = { "quarters", "dimes", "nickels", "pennies" };

int
get_cents(void)
{
    int n;

    do {
        n = get_int("Money Owed: ");
    } while (n <= 0);

    return n;
}

int
main(int argc,char **argv)
{

    --argc;
      argv;

    int cents = get_cents();
    printf("%d cents\n",cents);

    int coins = 0;
    for (int idx = 0;  idx < COUNTOF(denoms);    idx) {
        int denom = denoms[idx];

        // is amount remaining larger than the denomination of the current coin?
        if (cents >= denom) {
            // get number of the current denomination to dispense
            int count = cents / denom;

            // remember total number of coins
            coins  = count;

            // show how many of this coin to dispense
            printf("%d %s\n",count,names[idx]);

            // reduce total number of cents
            cents %= denom;
        }
    }

    // show the number of coins
    printf("%d coins\n",coins);

    return 0;
}

Here is some program output:

Money Owed: 137
137 cents
5 quarters
1 dimes
2 pennies
8 coins
  • Related