S
Shark1
Hello.
I'm suppose to write a program that will match the price given by the user
using the least number of coins. There are 5 different type of coins.
I get this output, the count is actually fine (4 quarters and 1 penny =
101), but I don't know why I'm getting garbage afterwards, right before the
program crashes.
101
quarter: 4
dime: 0
nickel: 0
penny: 1
á ": 2293664
: 1
└ ": 1
1$?: 4198553
#include <stdio.h>
#include <stdlib.h>
struct Coins
{
char *name;
int value;
int count;
};
void main()
{
struct Coins coins[] = {
{"quarter", 25, 0},
{"dime", 10, 0},
{"nickel", 5, 0},
{"penny", 1, 0},
};
char line[100];
int price;
int i;
while (gets(line))
{
price = atoi(line);
for (i = 0; price > 0 && i < sizeof coins; ++i)
{
coins.count = price / coins.value;
price = price % coins.value;
}
for (i = 0; i < sizeof coins; ++i)
{
printf("%s:\t%d\n", coins.name, coins.count);
coins.count = 0;
}
putchar('\n');
}
}
I'm suppose to write a program that will match the price given by the user
using the least number of coins. There are 5 different type of coins.
I get this output, the count is actually fine (4 quarters and 1 penny =
101), but I don't know why I'm getting garbage afterwards, right before the
program crashes.
101
quarter: 4
dime: 0
nickel: 0
penny: 1
á ": 2293664
: 1
└ ": 1
1$?: 4198553
#include <stdio.h>
#include <stdlib.h>
struct Coins
{
char *name;
int value;
int count;
};
void main()
{
struct Coins coins[] = {
{"quarter", 25, 0},
{"dime", 10, 0},
{"nickel", 5, 0},
{"penny", 1, 0},
};
char line[100];
int price;
int i;
while (gets(line))
{
price = atoi(line);
for (i = 0; price > 0 && i < sizeof coins; ++i)
{
coins.count = price / coins.value;
price = price % coins.value;
}
for (i = 0; i < sizeof coins; ++i)
{
printf("%s:\t%d\n", coins.name, coins.count);
coins.count = 0;
}
putchar('\n');
}
}