coins program, need some help with crashing

Discussion in 'C Programming' started by Shark1, Jan 20, 2005.

  1. Shark1

    Shark1 Guest

    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');
    }
    }
    Shark1, Jan 20, 2005
    #1
    1. Advertising

  2. Shark1

    Lew Pitcher Guest

    -----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.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-----
    Lew Pitcher, Jan 20, 2005
    #2
    1. Advertising

  3. 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.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
    =?ISO-8859-1?Q?Bj=F8rn_Augestad?=, Jan 20, 2005
    #3
  4. Shark1

    gooch Guest

    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.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.
    gooch, Jan 20, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daragoth
    Replies:
    4
    Views:
    403
    Dave Vandervies
    Aug 26, 2004
  2. why this program is not crashing

    , Feb 11, 2005, in forum: C Programming
    Replies:
    20
    Views:
    650
    Dave Thompson
    Feb 21, 2005
  3. James

    why this program is crashing

    James, Apr 17, 2005, in forum: C Programming
    Replies:
    6
    Views:
    292
    Barry Schwarz
    Apr 18, 2005
  4. santosh

    Help with program crashing...

    santosh, Jan 19, 2006, in forum: C Programming
    Replies:
    9
    Views:
    313
    Chris Torek
    Jan 20, 2006
  5. Stef Mientki

    is there some error crashing reporting

    Stef Mientki, Sep 29, 2009, in forum: Python
    Replies:
    0
    Views:
    212
    Stef Mientki
    Sep 29, 2009
Loading...

Share This Page