coins program, need some help with crashing

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');
}
}
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

See my inline comment below...
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.count = price / coins.value;
price = price % coins.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.name, coins.count);
coins.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/2kagVFX4UWr64RAtxaAJ93XnSXesb6HyZMntcLcErc7r3pfgCfabDP
/RM1ReuIeolMDZpMsAHrfX8=
=ZcA1
-----END PGP SIGNATURE-----
 
?

=?ISO-8859-1?Q?Bj=F8rn_Augestad?=

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

for (i = 0; i < sizeof coins; ++i)

See above.
{
printf("%s:\t%d\n", coins.name, coins.count);
coins.count = 0;
}
putchar('\n');
}
}


HTH
Bjørn
 
G

gooch

Shark1 said:
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');
}
}

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.count = price / coins.value;
price = price % coins.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.name, coins.count);
coins.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top