Thread: program wont run, always aborts

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    53

    program wont run, always aborts

    this grocery checkout line simulator compiles and tells me syntax error 'return' i cant figure that out . but the big problem is my program gets all the way to the point where it tells the user the amount of change, but then it aborts because it says the variable centsLeft is being used without being defined...can anyone help me.

    Code:
    #include <stdio.h>
    
    #include <stdlib.h>
    
    
     
    
    int main()
    
    { /* Begin function main*/
    
     
    
          int numItems;
    
          int price, amtPaid, total = 0, change;
    
          int quarters = 10, dimes = 10, nickels = 10, pennies = 10;
    	
            int centsLeft = 100 * change ;       // cents left to pay in change
    
    
     
    
          printf("Please enter the number of grocery items>");
    
          scanf("%d", &numItems);
    
          while (numItems != 0){
    
     
    
          /* Begin while loop*/
    
          while (numItems != 0){
    
                printf("Please 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("change is %.2f\n", change);
    
     
    
      if( change > 0 )                 // optional denomination breakdown 
       {  
              centsLeft = 100 * change;        // convert to integer cents,     
                                                          // and make sure not to
                                                          // truncate last cent      
          if( centsLeft >= 2000 )
          {  printf("  $20 - %d\n", centsLeft / 2000);
             centsLeft -= 2000 * ( centsLeft / 2000 );
          }
          if( centsLeft >= 1000 )
          {  printf("  $10 - %d\n", centsLeft / 1000);
             centsLeft -= 1000 * ( centsLeft / 1000);
          }
          if( centsLeft >= 500 )
          {  printf("  $ 5 - %d\n", centsLeft / 500);
             centsLeft -= 500 * ( centsLeft / 500 );
          }
          if( centsLeft >= 100 )
          {  printf("  $ 1 - %d\n", centsLeft / 100);
             centsLeft -= 100 * ( centsLeft / 100 );
          }
          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);
          }
       }
       printf("\n\n");   
       
    }
    
    
          
    
          total = 0;
    
          printf("\n\nPlease enter the number of grocery items>");
    
          scanf("%d", &numItems);
    
     
    
          } /*end while loop*/
    
     return 0;
    
          
    
     /*end function main*/

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    I'm not sure how you got that code example to compile since the "return" is outside the closing curly brace for the main function. Thus, the reason for the "return error". Also, you're mixing int and float variables which will probably generate some runtime errors. The change variable is declared but not defined which will probably cause you a lot of grief as you use it to compute centsLeft as indicated in the code below. Finally, I'm not sure why you would want to compute centsLeft in that manner.

    Code:
     int price, amtPaid, total = 0, change;
    int centsLeft = 100 * change ;       // cents left to pay in change
    Also, change should be defined with some value. For instance,
    Code:
    int price, amtPaid, total = 0, change = 50;
    Bob
    Last edited by BobS0327; 10-22-2005 at 05:30 PM.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    int price;
    scanf("%f", &price);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    ??im still not getting what i need.

    Code:
    #include <stdio.h>
    
    #include <stdlib.h>
    
    
     
    
    int main()
    
    { /* Begin function main*/
    
     
    
          int numItems;
    
          int price, amtPaid, total = 0, change = 50;
    
          int quarters = 10, dimes = 10, nickels = 10, pennies = 10;
    	
            int centsLeft =  change ;       // cents left to pay in change
    
    
     
    
          printf("Please enter the number of grocery items>");
    
          scanf("%d", &numItems);
    
          while (numItems != 0){
    
     
    
          /* Begin while loop*/
    
          while (numItems != 0){
    
                printf("Please enter the price for your item>");
    
                scanf("%d", &price);
    
                total += price;
    
                numItems--;}
    
     
    
          printf("\nPlease enter the amount paid>");
    
          scanf("%d", &amtPaid);
    
          change = amtPaid - total;
    
          printf("change is %.2f\n", change);
    
     
    
      if( change > 0 )                 // optional denomination breakdown 
       {  
              centsLeft  = change;        // convert to integer cents,     
                                                          // and make sure not to
                                                          // truncate last cent      
          if( centsLeft >= 2000 )
          {  printf("  $20 - %d\n", centsLeft / 2000);
             centsLeft -= 2000 * ( centsLeft / 2000 );
          }
          if( centsLeft >= 1000 )
          {  printf("  $10 - %d\n", centsLeft / 1000);
             centsLeft -= 1000 * ( centsLeft / 1000);
          }
          if( centsLeft >= 500 )
          {  printf("  $ 5 - %d\n", centsLeft / 500);
             centsLeft -= 500 * ( centsLeft / 500 );
          }
          if( centsLeft >= 100 )
          {  printf("  $ 1 - %d\n", centsLeft / 100);
             centsLeft -= 100 * ( centsLeft / 100 );
          }
          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);
          }
       }
       printf("\n\n");   
       
    }
    
    
          
    
          total = 0;
    
          printf("\n\nPlease enter the number of grocery items>");
    
          scanf("%d", &numItems);
    
     
    
          } /*end while loop*/
    
     return 0;
    
          
    
     /*end function main*/

  5. #5
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Indent, use brackets, use newline properly. Your code though very short is very hard to read.

    Dont put couple of newlines for evry staement it makes your coding longer. and also indent it properly.

    Its hard to determine wether that return 0; is outside or not. but by counting the brackets you have you may have a problem with them.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Maybe this snippet will help you out. Keep in mind that floating point arithmetic has a tendency to introduce rounding errors into your calculations. Rounding error is really beyond the scope of this thread. Finally, I would suggest you heed loko's excellent advice.

    Bob

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h> // Needed for modf
    int main()
    { 
    
        int numItems;
        float total = 0.0; 
        float price = 0.0, amtPaid = 0.0,  change = 0.0;
        float centsLeft = 0.0;       // cents left to pay in change
        float dollars, cents;
    
        printf("Please enter the number of grocery items>");
        scanf("%d", &numItems);
    
        /* 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 = %0.02f\n", dollars, cents*100, centsLeft);
    
        // Here is where you break centsLeft down into denominations
    
        return 0;
    }

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    Bob....do you mean place my values under the snippet you supplied ike this? Here it is with the code in place but when i compile and run it returns the change as 25cents - 0. when i enter 2 items for 25 cents and pay with a dollar. i cant get it to return the right change. Second it wont loop and ask the number of items again after returning the change. Ive fiddled with it and changed it a few ways but all of my attempts seem to mess it up worse. heres the base of what i have been working with as of now.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h> // Needed for modf
    int main()
    { 
    
        int numItems;
        float total = 0.0; 
        float price = 0.0, amtPaid = 0.0,  change = 0.0;
        float centsLeft = 0.0;       // cents left to pay in change
        float dollars, cents;
    
        printf("Please enter the number of grocery items>");
        scanf("%d", &numItems);
    
        /* 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 = %0.02f\n", dollars, cents*100, centsLeft);
    
        // Here is where you break centsLeft down into denominations
    
        
    
     if( change > 0 )                 // optional denomination breakdown 
       {  
              centsLeft  = 100 * change;        // convert to integer cents,     
                                                          // and make sure not to
                                                          // truncate last cent      
          if( centsLeft >= 2000 )
          {  printf("  $20 - %d\n", centsLeft / 2000);
             centsLeft -= 2000 * ( centsLeft / 2000 );
          }
          if( centsLeft >= 1000 )
          {  printf("  $10 - %d\n", centsLeft / 1000);
             centsLeft -= 1000 * ( centsLeft / 1000);
          }
          if( centsLeft >= 500 )
          {  printf("  $ 5 - %d\n", centsLeft / 500);
             centsLeft -= 500 * ( centsLeft / 500 );
          }
          if( centsLeft >= 100 )
          {  printf("  $ 1 - %d\n", centsLeft / 100);
             centsLeft -= 100 * ( centsLeft / 100 );
          }
          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);
          }
       }
       printf("\n\n");   
       return 0;
    }

  8. #8
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Well, I guess I was trying to let you figure out the rest of the code. Here's a hint...
    Make the following changes in the snippet you attached:

    Change centsLeft declaration from a float to a int
    That is, change float centsLeft = 0.0 to int centsLeft = 0;

    Also, delete the statement centsLeft = 100 * change;

    I'll let you figure out the outside loop to repeat the process.

    Good luck.

    Bob

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    i tried implementing a large outer loop, but its not working and telling me end of file found before...im guessing i did something really wrong huh lol? Im sorry im acting like such a newb...i am just started this, ive tried for about an hour on this loop and just cant get it. Also can you give any hints as to how to use a type of bank system in this...like, if i have 10 of each coin the program uses the lower denomination if the upper runs out, then terminates the program when i have no more change.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h> // Needed for modf
    int main()
    { 
    
        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;
    
        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 = %0.02f\n", dollars, cents*100, centsLeft);
    
        // Here is where you break centsLeft down into denominations
    
        
    
     if( change > 0 )                 // optional denomination breakdown 
       {  
                          
          if( centsLeft >= 2000 )
          {  printf("  $20 - %d\n", centsLeft / 2000);
             centsLeft -= 2000 * ( centsLeft / 2000 );
          }
          if( centsLeft >= 1000 )
          {  printf("  $10 - %d\n", centsLeft / 1000);
             centsLeft -= 1000 * ( centsLeft / 1000);
          }
          if( centsLeft >= 500 )
          {  printf("  $ 5 - %d\n", centsLeft / 500);
             centsLeft -= 500 * ( centsLeft / 500 );
          }
          if( centsLeft >= 100 )
          {  printf("  $ 1 - %d\n", centsLeft / 100);
             centsLeft -= 100 * ( centsLeft / 100 );
          }
          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);
          }
      
       }
    		printf("\n\n");   
    		return 0;
       }

  10. #10
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    I've attached the completed code. The outer loop are the do .. while statements. As far as the bank system is concerned, I would first try to visualize this process on paper and then write the code. Years ago, we former big iron coders referred to this as flowcharting.

    Hey, I started out as a total newbie. And in some ways, I'm still a total newbie. I've got a Directshow video capture project that I'm working on and it's a totally uphill learning experience since I've never done any type of video/graphics programming before. Unfortunately, I really don't have anyone to help me.

    Good luck
    Bob

    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;
    
        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 >= 2000 )
                    {  
                        printf("  $20 - %d\n", centsLeft / 2000);
                        centsLeft -= 2000 * ( centsLeft / 2000 );
                    }
                    if( centsLeft >= 1000 )
                    {  
                        printf("  $10 - %d\n", centsLeft / 1000);
                        centsLeft -= 1000 * ( centsLeft / 1000);
                    }
                    if( centsLeft >= 500 )
                    {  
                        printf("  $ 5 - %d\n", centsLeft / 500);
                        centsLeft -= 500 * ( centsLeft / 500 );
                    }
                    if( centsLeft >= 100 )
                    {  
                        printf("  $ 1 - %d\n", centsLeft / 100);
                        centsLeft -= 100 * ( centsLeft / 100 );
                    }
                    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);
                    }
                }
                printf("\n\n");   
            }
            printf("Hit the  X key to e(X)it, any other key to repeat procedure ");
            ch=toupper(getche());
            printf("\n");
            if (ch=='X') {
                printf("Program finished");
                break;
            }
        }
        while (TRUE);
        return 0;
    }

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    hmm, so for the bank i would need to do somethin like.

    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 .

    oh yeah, and ur porgram didnt work the second time, when i tried to continue and enter more items.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That depends entirely on what you're trying to do I suppose. If you want to do the above, you could use an array or two for both points 1 and 2 above. For example:
    Code:
    unsigned long int change[ NUM_OF_COIN_TYPES ];
    ...
    
    change[ PENNIES ] ++; /* increment the number of pennies */
    
    ...
    
    if( change[ PENNIES ] == 0 )
        ...ran out of pennies...
    You could also try to make change any way possible. For example, if you were out of quarters and nickles, but you had 2 dimes and five pennies, you could still give back the 25 cents change.


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

  13. #13
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    You could also try to make change any way possible. For example, if you were out of quarters and nickles, but you had 2 dimes and five pennies, you could still give back the 25 cents change.
    ^^ thats what i need exactly, ive never worked with an array before. Is there another way, or is that the easiest?

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, you could just use single integers for them all:
    Code:
    unsigned long int pennies,
        nickles,
        dimes,
        quarters,
        halfdollar,
        dollarcoin,
        dollarbill, ... and so on

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

  15. #15
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    so...put this somewhere at the beginning of my code?
    Code:
    unsigned long int pennies,nickles,dimes,quarters,halfdollar,dollarcoin,dollarbill
    counter = 0

    and somethin like this in the loop??
    Code:
    change[ PENNIES ] ++; /* increment the number of pennies and so on  */
    
    if(nickles==0)
    /* what code would work here that would tell the program to use pennies?? */
    else if(pennies == 0)
     printf("cannot give correct change");

    not real sure how to do this...

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, 05:32 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Replies: 3
    Last Post: 07-11-2005, 03: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