By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,286 Members | 2,364 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,286 IT Pros & Developers. It's quick & easy.

coins program, need some help with crashing

P: n/a
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[i].count = price / coins[i].value;
price = price % coins[i].value;
}

for (i = 0; i < sizeof coins; ++i)
{
printf("%s:\t%d\n", coins[i].name, coins[i].count);
coins[i].count = 0;
}
putchar('\n');
}
}
Nov 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Shark1 wrote:
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)) Don't use gets(), fgets() is better.

{
price = atoi(line);

for (i = 0; price > 0 && i < sizeof coins; ++i) i < sizeof(coins)/sizeof(coins[0])
will probably work a lot better.

{
coins[i].count = price / coins[i].value;
price = price % coins[i].value;
}

for (i = 0; i < sizeof coins; ++i) See above.
{
printf("%s:\t%d\n", coins[i].name, coins[i].count);
coins[i].count = 0;
}
putchar('\n');
}
}


HTH
Bjrn
Nov 14 '05 #2

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

See my inline comment below...

Shark1 wrote:
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) What is the sizeof coins? What does this represent? Are you sure?
(This is a hint, not a question)
{
coins[i].count = price / coins[i].value;
price = price % coins[i].value;
}

for (i = 0; i < sizeof coins; ++i) What is the sizeof coins? What does this represent? Are you sure?
(This is a hint, not a question) {
printf("%s:\t%d\n", coins[i].name, coins[i].count);
coins[i].count = 0;
}
putchar('\n');
}
}

- --
Lew Pitcher
IT Consultant, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFB7/2kagVFX4UWr64RAtxaAJ93XnSXesb6HyZMntcLcErc7r3pfgCf abDP
/RM1ReuIeolMDZpMsAHrfX8=
=ZcA1
-----END PGP SIGNATURE-----
Nov 14 '05 #3

P: n/a

Shark1 wrote:
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[i].count = price / coins[i].value;
price = price % coins[i].value;
}

for (i = 0; i < sizeof coins; ++i)
{
printf("%s:\t%d\n", coins[i].name, coins[i].count);
coins[i].count = 0;
}
putchar('\n');
}
}

There are a few things wrong here. See the notes in my code below.

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

//I am defining the number of different types of coins here
// The problem you were having was using the sizeof and expecting to
get the
//number of elements in the coins array.
#define NUM_COINS 4

struct Coins
{
char *name;
int value;
int count;
};

//Main must have a return type of int
int main()
{
//use the NUM_COINS value here to set the size of the array
//this is technically not needed but IMHO it makes your code more
readable
struct Coins coins[NUM_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);

//no need for the and condition in this loop
for (i = 0; price > 0; ++i)
{
coins[i].count = price / coins[i].value;
price = price % coins[i].value;
}

//use NUM_COINS value as the conditional here. see explanation
below
for (i = 0; i < NUM_COINS; ++i)
{
printf("%s:\t%d\n", coins[i].name, coins[i].count);
coins[i].count = 0;
}
putchar('\n');
}

return 0;

}

The sizeof operator is used to determine the number of bytes in a data
type. The value obtained from sizeof coins is dependent on the machine
you are using but on my machine char* are 4 bytes and int is 4 bytes.
This means that your Coins structure size in bytes is 12 and the coins
array of 4 Coins is 12 * 4 = 48 bytes. You can try this out on your
machine by using printf to output the value of sizeof (int) and sizeof
(char*). So what was happenning was your second loop was executing 48
times instead of the 4 times you were expecting.

Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.