program wont run, always aborts

This is a discussion on program wont run, always aborts within the C Programming forums, part of the General Programming Boards category; oh yeah, and ur porgram didnt work the second time, when i tried to continue and enter more items. I ...

  1. #16
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    oh yeah, and ur porgram didnt work the second time, when i tried to continue and enter more items.
    I forgot to clear out the total variable after each loop which is causing the above problem.

    Please add the total = 0.0 statement as indicated in the area of the code as indicated below


    Code:
      if (ch=='X') {
                printf("Program finished");
                break;
            }
        total = 0.0;
    }
        while (TRUE);

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Let's use pennies and nickles only.
    Code:
    int pennies = 0, nickles = 0;
    Now some where in your program you ask someone how many pennies they have and how many nickles, and you make those assignments. Then you ask them how much change they want back, or whatever.

    As long as you have nickles remaining, and as long as the total change you need to give still is over 5 (the number of pennies in a nickle), you do something like:
    Code:
    if( change_to_give > 5 )
    {
        nickles_to_give ++; /* increment the number of nickles we're handing back */
        change_to_give -= 5; /* subtract 5, the size of a nickle, from change to give */
    }
    If however, you run into a scenario where you're out of nickles, you just see if you have enough pennies to cover the remaining change. If you do, subtract the remaining change from pennies, and set pennies to give to whatever that ammount was.

    Now there are many ways to do this whole problem, but there's a hopefully simple illustration.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    Code:
    Now some where in your program you ask someone how many pennies they have and how many nickles, and you make those assignments. Then you ask them how much change they want back, or whatever.
    im just going to set it to where i have 10 of each coin in the bank so would i do that like this?
    Code:
    int quarters = 10, dimes = 10, nickels = 10, pennies = 10;
    Last edited by stormfront; 10-24-2005 at 09:55 PM.

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sure. Just make sure you initialize all of your variables to something meaningful before you do any calculations before them.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    I cant get it to compile, the errors are confusing me. i know i need to declare what returned change is. i tried returned change = change, but if i do that then the if statements for my counter will mess up my change returned, im lost. heres what ive started... i added values for the change at the beginning added counter = 0; at the beginning and tried working an if statement for quarters but im not sure how and where to declare what change returned is. C is so confusing...
    Code:
    #include <windows.h>  // Needed for TRUE define
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>  // Needed for modff
    #include <conio.h> // Needed for getche
    int main()
    { 
        char ch;
        int numItems;
        float total = 0.0; 
        float price = 0.0, amtPaid = 0.0,  change = 0.0;
        int centsLeft = 0;       // cents left to pay in change
        float dollars, cents;
    	int quarters = 10, dimes = 10, nickels = 10, pennies = 10;
    	int returned_change;
    	int counter = 0;
    
        do
        {
            printf("Please enter the number of grocery items>");
            scanf("%d", &numItems);
            while (numItems != 0)
            {
                /* Begin while loop*/
                while (numItems != 0)
                {
                    printf("\nPlease enter the price for your item>");
                    scanf("%f", &price);
                    total += price;
                    numItems--;
                }
                printf("\nPlease enter the amount paid>");
                scanf("%f", &amtPaid);
                change = amtPaid - total;
                printf("\nchange is %0.02f\n", change);
                cents = modff( change, &dollars); // Separate Dollars and cents
                centsLeft = (dollars * 100) + (cents * 100);
                printf("dollars = %0.02f cents = %0.02f centsLeft = %d\n", dollars, cents*100, centsLeft);
    
                if( change > 0 )                 // optional denomination breakdown 
                {  
                    
                    if( centsLeft >= 25 )
                    {  
                        printf("  25%c - %d\n", 155, centsLeft / 25);     // 155 is ASCII code
                        centsLeft -= 25 * ( centsLeft / 25 );             // for cent symbol
                    }                                                    // in Win TERMINAL
                    if( centsLeft >= 10 )                                // character set
                    {  
                        printf("  10%c - %d\n", 155, centsLeft / 10);
                        centsLeft -= 10 * ( centsLeft / 10 );
                    }
                    if( centsLeft >= 5 )
                    {  
                        printf("   5%c - %d\n", 155, centsLeft / 5);
                        centsLeft -= 5 * ( centsLeft / 5 );
                    }
                    if( centsLeft > 0 )
                    {  
                        printf("   1%c - %d", 155, centsLeft);
                    }
    				if( returned change > 25 )
    				{
    				returned change ++; /* increment the number of nickles we're handing back */
    				returned change -= 25; /* subtract 25, the size of a quarter, from change to give */
    				}
                }
                printf("\n\n");   
            }
            printf("Hit the  0 key to e(X)it, any other key to repeat procedure ");
            ch=toupper(getche());
            printf("\n");
            if (ch=='0') {
                printf("Program finished");
                break;
            }
    	total = 0.0;
    }
        while (TRUE);
        return 0;
    }
    Last edited by stormfront; 10-25-2005 at 01:25 PM.

  6. #21
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Code:
    int returned change;
    You can't have spaces in C identifiers. The compiler treats it as two seperate functions. Use returned_change instead.

    Also, you need to delcare counter.

    And, you need to define TRUE. But this isn't really much point. while(1) is just as readable.

  7. #22
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    1. assign an amount of bills/coins for each denomination
    2. create a counter that keeps track of the numbers used and subtracts that amount.
    3. create an (if)?? statement that tells the user the bank is out of coins and terminates the program
    This would be my approach to your problem...

    Create another function with a boolean return type, pass centsLeft to the
    function as an unsigned long int. In the function create a static array or
    struct to hold your starting currency/coinage values. Also, I would move all of
    your output printing to this function.

    Use the following if statement:

    Starting with 20 dollars;

    Verify that the static $20 variable and centLeft are both positive. If so, then
    verify that the $20 static variable is >= centsLeft. If so, printout the
    $20 value of centsLeft divided by 2000 and reduce the $20 static variable by
    centsLeft divided by 2000 and reduce centsLeft by 2000
    multiplied ( centsLeft divided by 2000 ). If not, reduce
    centsLeft by $20 static variable multiplied by 2000, print out $20 static variable
    and finally ZERO out the $20 static variable.

    The above process will have to be done for $10, $5, $1, quarter, dime, nickel and penny
    If centsleft variable is positive at the end of the process, then there was not
    enough money in the bank to cover the payout. Otherwise, if centsLeft is ZERO, then
    payout was made successfully. Return bool FALSE if successful.

    I'll translate this to C code and eventually post it in this thread to help you.

    Good luck

    Bob

  8. #23
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    I've cut it down to just change...25,10,5,1.....it helps me see more of the program on screen and helps me to visualize what i need to do and how to implement it better. Bob i understood fo the most part what you said and i am trying to hack away at it lol.

    Code:
    #include <windows.h>  // Needed for TRUE define
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>  // Needed for modff
    #include <conio.h> // Needed for getche
    int main()
    { 
        char ch;
        int numItems;
        float total = 0.0; 
        float price = 0.0, amtPaid = 0.0,  change = 0.0;
        int centsLeft = 0;       // cents left to pay in change
        float dollars, cents;
    	int quarters = 10, dimes = 10, nickels = 10, pennies = 10;
    	int returned_change;
    	int counter = 0;
    
        do
        {
            printf("Please enter the number of grocery items>");
            scanf("%d", &numItems);
            while (numItems != 0)
            {
                /* Begin while loop*/
                while (numItems != 0)
                {
                    printf("\nPlease enter the price for your item>");
                    scanf("%f", &price);
                    total += price;
                    numItems--;
                }
                printf("\nPlease enter the amount paid>");
                scanf("%f", &amtPaid);
                change = amtPaid - total;
                printf("\nchange is %0.02f\n", change);
                cents = modff( change, &dollars); // Separate Dollars and cents
                centsLeft = (dollars * 100) + (cents * 100);
                printf("dollars = %0.02f cents = %0.02f centsLeft = %d\n", dollars, cents*100, centsLeft);
    
                if( change > 0 )                 // optional denomination breakdown 
                {  
                    
                    if( centsLeft >= 25 )
                    {  
                        printf("  25%c - %d\n", 155, centsLeft / 25);     // 155 is ASCII code
                        centsLeft -= 25 * ( centsLeft / 25 );             // for cent symbol
                    }                                                    // in Win TERMINAL
                    if( centsLeft >= 10 )                                // character set
                    {  
                        printf("  10%c - %d\n", 155, centsLeft / 10);
                        centsLeft -= 10 * ( centsLeft / 10 );
                    }
                    if( centsLeft >= 5 )
                    {  
                        printf("   5%c - %d\n", 155, centsLeft / 5);
                        centsLeft -= 5 * ( centsLeft / 5 );
                    }
                    if( centsLeft > 0 )
                    {  
                        printf("   1%c - %d", 155, centsLeft);
                    }
    	if( returned change > 25 )
    	{
    	returned change ++; /* increment the number of nickles we're handing back */
    	returned change -= 25; /* subtract 25, the size of a quarter, from change to give */
    	}
                }
                printf("\n\n");   
            }
            printf("Hit the  0 key to e(X)it, any other key to repeat procedure ");
            ch=toupper(getche());
            printf("\n");
            if (ch=='0') {
                printf("Program finished");
                break;
            }
    	total = 0.0;
    }
        while (TRUE);
        return 0;
    }
    Last edited by stormfront; 10-25-2005 at 01:30 PM.

  9. #24
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Complete code listed below. I haven't really tested it thoroughly. I'll let that up to you. Let me know if you find any bugs.

    Have fun
    Bob

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>  // Needed for modff
    #include <conio.h> // Needed for getche
    
    #define FALSE 0
    #define TRUE !FALSE
    #define DEBUG
    
    int bDenominationCalculation(unsigned long ulCentsLeft);
    
    int main(void)
    { 
        char ch;
        int numItems = 0;
        float total = 0.0; 
        float price = 0.0, amtPaid = 0.0,  change = 0.0;
        unsigned long centsLeft = 0;       // cents left to pay in change
        float dollars, cents;
    
        do
        {
            printf("Please enter the number of grocery items>");
            scanf("%d", &numItems);
            while (numItems != 0)
            {
                printf("\nPlease enter the price for your item>");
                scanf("%f", &price);
                total += price;
                numItems--;
            }
            printf("\nPlease enter the amount paid>");
            scanf("%f", &amtPaid);
            change = amtPaid - total;
            printf("\nchange is %0.02f\n", change);
            cents = modff( change, &dollars); // Separate Dollars and cents
            centsLeft = (dollars * 100) + (cents * 100);
            printf("dollars = %0.02f cents = %0.02f centsLeft = %d\n", dollars, cents*100, centsLeft);
            if( change > 0.0 )                 // optional denomination breakdown 
            {  
                if(bDenominationCalculation(centsLeft))
                {
                    printf("program ending, the bank is broke\n");
                    break;
                }
            }
            printf("Hit the  0 key to e(X)it, any other key to repeat procedure ");
            ch=toupper(getche());
            printf("\n");
            if (ch=='0') {
                printf("Program finished");
                break;
            }
            total = 0.0;
        }
        while (TRUE);
        return 0;
    }  
    
    
    int bDenominationCalculation(unsigned long ulCentsLeft)
    {
        unsigned long uliTempCalc = 0L;
        static struct SBank
        {
            unsigned long uli20Dollar;
            unsigned long uli10Dollar;
            unsigned long uli5Dollar;
            unsigned long uli1Dollar;
            unsigned long uliQuarter;
            unsigned long uliDime;
            unsigned long uliNickel;
            unsigned long uliPenny;
        }SBankDenom = {10,10,10, 10, 10, 10, 10, 10};
    
        if(SBankDenom.uli20Dollar  && (ulCentsLeft / 2000))
        {
            if(SBankDenom.uli20Dollar  >=  ulCentsLeft / 2000)
            {
                printf("$20 - %u\n", ulCentsLeft / 2000);
                SBankDenom.uli20Dollar -= ulCentsLeft/ 2000;
                ulCentsLeft -= 2000 * ( ulCentsLeft / 2000 );
            }
            else
            {
                ulCentsLeft -= (SBankDenom.uli20Dollar * 2000 );
                printf("$20 - %u\n", SBankDenom.uli20Dollar);
                SBankDenom.uli20Dollar  = 0;
            }
        }
        if (SBankDenom.uli10Dollar  &&  (ulCentsLeft /1000) )
        {
            if(SBankDenom.uli10Dollar  >=  ulCentsLeft / 1000)
            {
                printf("$10 - %u\n", ulCentsLeft / 1000);
                SBankDenom.uli10Dollar -= ulCentsLeft/ 1000;
                ulCentsLeft -= 1000 * ( ulCentsLeft / 1000 );
            }
            else
            {
                ulCentsLeft -= (SBankDenom.uli10Dollar * 1000 );
                printf("$10 - %u\n", SBankDenom.uli10Dollar);
                SBankDenom.uli10Dollar  = 0;
            }
        }
        if( SBankDenom.uli5Dollar && (ulCentsLeft / 500) )
        {  
            if(SBankDenom.uli5Dollar  >=  ulCentsLeft / 500)
            {
                printf("$5 - %u\n", ulCentsLeft / 500);
                SBankDenom.uli5Dollar -= ulCentsLeft/ 500;
                ulCentsLeft -= 500 * ( ulCentsLeft / 500 );
            }
            else
            {
                ulCentsLeft -= (SBankDenom.uli5Dollar * 500 );
                printf("$5 - %u\n", SBankDenom.uli5Dollar);
                SBankDenom.uli5Dollar  = 0;
            }       
        }
        if( SBankDenom.uli1Dollar &&  (ulCentsLeft / 100) )
        {  
            if(SBankDenom.uli1Dollar  >=  ulCentsLeft / 100)
            {
                printf("$1 - %u\n", ulCentsLeft / 100);
                SBankDenom.uli1Dollar -= ulCentsLeft/ 100;
                ulCentsLeft -= 100 * ( ulCentsLeft / 100 );
            }
            else
            {
                ulCentsLeft -= (SBankDenom.uli1Dollar * 100 );
                printf("$1 - %u\n", SBankDenom.uli1Dollar);
                SBankDenom.uli1Dollar  = 0;
            }
        }
        if( SBankDenom.uliQuarter &&  (ulCentsLeft / 25))
        {  
            if(SBankDenom.uliQuarter  >=  ulCentsLeft / 25)
            {
                printf("25%c - %u\n", 155, ulCentsLeft / 25); 
                SBankDenom.uliQuarter -= ulCentsLeft / 25;
                ulCentsLeft -= 25 * ( ulCentsLeft / 25 );
            }
            else
            {
                ulCentsLeft -= (SBankDenom.uliQuarter * 25 );
                printf("25%c - %u\n", 155,SBankDenom.uliQuarter ); 
                SBankDenom.uliQuarter  = 0;
            }
        }                                                    
        if( SBankDenom.uliDime && (ulCentsLeft / 10) )         
        {  
            if(SBankDenom.uliDime  >=  ulCentsLeft / 10)
            {
                printf("10%c - %u\n", 155, ulCentsLeft / 10);
                SBankDenom.uliDime -= ulCentsLeft/ 10;
                ulCentsLeft -= 10 * ( ulCentsLeft / 10 );
            }
            else
            {
                ulCentsLeft -= (SBankDenom.uliDime * 10 );
                printf("10%c - %u\n", 155,SBankDenom.uliDime );
                SBankDenom.uliDime  = 0;
            }
        }
        if( SBankDenom.uliNickel && (ulCentsLeft / 5) )
        {  
            if(SBankDenom.uliNickel  >=  ulCentsLeft / 5)
            {
                printf("5%c - %u\n", 155, ulCentsLeft / 5);
                SBankDenom.uliNickel -= ulCentsLeft/ 5;
                ulCentsLeft -= 5 * ( ulCentsLeft / 5 );
            }
            else
            {
                ulCentsLeft -= (SBankDenom.uliNickel * 5 );
                printf("5%c - %u\n", 155, SBankDenom.uliNickel);
                SBankDenom.uliNickel  = 0;
            }
        }
        if( ulCentsLeft && SBankDenom.uliPenny )
        {  
            if(SBankDenom.uliPenny  >=  ulCentsLeft )
            {
                printf("1%c - %u\n", 155, ulCentsLeft);
                SBankDenom.uliPenny -= ulCentsLeft;
                ulCentsLeft -=  ulCentsLeft ;
            }
            else
            {
                ulCentsLeft -= (SBankDenom.uliPenny );
                printf("1%c - %u\n", 155,SBankDenom.uliPenny );
                SBankDenom.uliPenny  = 0;
            }
        }
    
        #ifdef DEBUG
        printf("Static struct variables:\n");
        printf("$20 %u\n",SBankDenom.uli20Dollar);
        printf("$10 %u\n",SBankDenom.uli10Dollar);
        printf("$5  %u\n",SBankDenom.uli5Dollar);
        printf("$1  %u\n",SBankDenom.uli1Dollar);
        printf("Q  %u\n",SBankDenom.uliQuarter);
        printf("D  %u\n",SBankDenom.uliDime);
        printf("N  %u\n",SBankDenom.uliNickel);
        printf("P  %u\n",SBankDenom.uliPenny);
        #endif
        if(ulCentsLeft)
            return TRUE;
        else
            return FALSE;
    }

  10. #25
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    WOW.....thats confusing haha.
    Code:
    int bDenominationCalculation(unsigned long ulCentsLeft)
    {
        unsigned long uliTempCalc = 0L;
        static struct SBank
        {
            unsigned long uli20Dollar;
            unsigned long uli10Dollar;
            unsigned long uli5Dollar;
            unsigned long uli1Dollar;
            unsigned long uliQuarter;
            unsigned long uliDime;
            unsigned long uliNickel;
            unsigned long uliPenny;
    my programming class is intro to programming so we havent gotten to alot of this yet. what exactly are the above statements used for , and what do they do??

  11. #26
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    A struct (structure) is a data type that is for lack of a better desription a container of identical objects. In this case a container of unsigned long ints. The notation such as uli20Dollar is just Hungarian notation created and promoted by a former Microsoft Chief Engineer by the name of Joseph Simonyi, a Hungarian. This type of notation is primarily found in the Windows GUI environment. Essentially, the variable name is prefixed with its data type. In this case Unsigned Long Integer.

    This was just one way of solving your problem. I can think of at least three other approaches.

    Hey, I gotta get outa here before the ANSI Standard Gestapo arrest me for espousing heresy.

    Have fun

    Bob

  12. #27
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    I want to thank you for helping me with that cash register program, the way you explained things and showed them to me visually helped me tremendously. My professor was amazed when i started spouting out answers to problems he wanted us to solve in class. . But now im in a little bind, let me show you what he wants andf then ill explain. He wants us to use the cash register program and:

    you are to implement the cash register program
    using multiple functions. this is not homework, if possible ask an experienced class mate or relative to demonstrate these to you before class on tuesday.
    using the following functions:
    • Implement a main function that inputs the number of grocery items and the amount paid by the
    user.
    • Implement a function that inputs the price of each individual grocery item. This function
    should accept the number of grocery items as an input argument. In addition this function
    should return the total cost of the items.
    • Implement a function for computing the number of quarters that are returned. This function
    must accept the amount of change returned as an input argument. It must also accept the
    number of quarters remaining in the bank as another argument. Finally, this function should
    return the number of quarters that can be returned.
    • Implement functions for computing the number of dimes, nickels, and pennies that are similar
    to the previous function. By similar, I mean that they must accept similar types of arguments
    and return similar types of values as the above function. The exception to this is the “pennies”
    function which is explained in the next point.
    • The “pennies” function should also be able to indicate if it is possible to return the exact
    amount to the user (new requirement). If there are not enough pennies in the bank to cover the
    amount of change needed to be returned to the user, your program should display an error
    message. The pennies function can indicate this situation by returning a -1 back to its calling
    function.
    • Implement a function whose sole purpose is to display output to the user. It should accept four
    arguments which are the numbers of quarters, dimes, nickels, and pennies, respectively. It
    should output the information to the screen. This function should not return any value back to
    the user.

    IT'S NOT HOMEWORK, lol people seem to be big on not doing homework here. He just started telling us of function on friday and he completely lost me. His teaching style is more verbal ,and i am a visual learner. If you could possibly find the time could you demonstrate these functions for me. I dont normally ask for help this often,....but like i said his teaching style doesnt suit me. Ive researched function on my own and can do basic ones, but some of these seem a little advanced for a beginner like me. thx

  13. #28
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    How about showing us some code on how you would approach this problem?

    In other words, you show us yours and we'll show you ours.

    Later

    Bob

  14. #29
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    I need a little clarification here

    • Implement a function for computing the number of quarters that are returned. This function
    must accept the amount of change returned as an input argument. It must also accept the
    number of quarters remaining in the bank as another argument. Finally, this function should
    return the number of quarters that can be returned
    .

    Is the number of quarters based on how the payout is made? In other words, change returned is $6.25 and the bank has 2 5s, 3 1s and 4 quarters then 1 quarter is returned. If the bank only has 3 quarters left then 3 quarters are returned

    I'm confused on why the number of quarters remaining is used as an input to the function.

    Please be detailed in your clarification.

    Thanx

    Bob
    Last edited by BobS0327; 10-30-2005 at 09:36 PM. Reason: spelling error and request clarification

  15. #30
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    well, the program no longer deals with anything above a quarter now, i took all that out cause he said he just wanted change. But as far as that function he said, it must take the amount of change to be returned, the amount of quarters in the bank and then determine how many quarters need to be returned. say that change is $0.76, the function need to take that number as an argument along with how many quarters are in the bank, compute how many quarters can be used to help return correct change wich in this case would be 3 , and return that number while also subtracting the number used from the bank, and i believe he wants the same type of function for all change types. i hope that was clear enough?? im having trouble understanding the directions as well as the function too.lol

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Re-doing a C program to run in Win2000 or XP
    By fifi in forum C Programming
    Replies: 5
    Last Post: 08-17-2007, 06:32 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  4. Replies: 3
    Last Post: 07-11-2005, 04:07 AM
  5. plz help me run this program!!!
    By galmca in forum C Programming
    Replies: 8
    Last Post: 02-01-2005, 01:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21