Need help with C program

This is a discussion on Need help with C program within the C Programming forums, part of the General Programming Boards category; Hi again, Your code does look good for starters, though there is much to be changed. For instance: Code: void ...

  1. #16
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hi again,

    Your code does look good for starters, though there is much to be changed. For instance:
    Code:
    void makechange(float cost, float payment, float *change,
    int *twentiesInTill, int *tensInTill, int *fivesInTill
    int *dollarsInTill, int *quartersInTill, int *dimesInTill
    int *nickelsInTill, int *penniesInTill, int *twentiesInChange,
    int *tensInChange, int *fivesInChange, int *dollarsInChange,
    int *nickelsInChange, int *penniesInChange);
    That in itself can be remedied into something simple.

    I will display my revised code step by step to help you learn. Here comes the local variables. These are declared before doing any work:
    Code:
    int main() {
    	// The file
    	FILE *transactions;
    	// The numbers
    	float cost, payment, change;
    	// The remainder in depth
    	int penniesInTill = 0, nickelsInTill = 0, dimesInTill = 0, quartersInTill = 0;
    	int dollarsInTill = 0, fivesInTill = 0, tensInTill = 0, twentiesInTill = 0;
    Quite simple. We declare our file called transactions, then we make three floating-point variables. This is to ensure we get precision. Like decimal points etc... Then we make our 8 integers to calculate how many quaters, tens, etc... All of these are initialized to 0 for a reason. Take an unintialized variable and add one to it, it wont equal 1.

    Next part:
    Code:
    	transactions = fopen ("transactions.dat", "r");
    	if (transactions == NULL) {
    		printf ("File, transactions, could not be opened\n");
    	}
    Nothing major here. You did just fine

    Next:
    Code:
    	fscanf(transactions, "%f\n", &cost);
    	printf ("The cost is %g\n", cost);
    	fscanf(transactions, "%f\n", &payment);
    	printf("The customer's payment was %g\n", payment);
    	change = cost - payment;
    	printf("The change due is %g\n", change);
    This scans the file for a number. That is why we have "%f\n" so the next time we scan we look at the next line. \n means new line. Here we do something close to:
    Scan the file for cost
    Print cost
    Scan the file for payment
    Print payment
    Equate difference [change]
    Print difference [change]
    We know change is a float since we are taking the difference of two floats.

    Next:
    Code:
     while (change >= 20.0f) {
    		change -= 20.0f;
    		twentiesInTill++;
    	}
    	while (change >= 10.0f) {
    		change -= 10.0f;
    		tensInTill++;
    	}
    	while (change >= 5.0f) {
    		change -= 5.0f;
    		fivesInTill++;
    	}
    	while (change >= 1.0f) {
    		change -= 1.0f;
    		dollarsInTill++;
    	}
    	while (change >= 0.25f) {
    		change -= 0.25f;
    		quartersInTill++;
    	}
    	while (change >= 0.10f) {
    		change -= 0.10f;
    		dimesInTill++;
    	}
    	while (change >= 0.05f) {
    		change -= 0.05f;
    		nickelsInTill++;
    	}
    	while (change >= 0.01f) {
    		change -= 0.01f;
    		penniesInTill++;
    	}
    This has been modified. On the old code you had "if change > 1) change -= 5;" That personall would have taken your program off in the wrong direction to calculate. Also, instead of doing ">", it would be better to do ">=" because if we run into 0.01 at the end, it wont take that as a penny because 0.01 is not greater than 0.01. Though 0.01 is greater than or equal to 0.01.

    Next, and Lastly:
    Code:
    	printf("In Detail\n\nTwenties: %d\nTens: %d\nFives: %d\nOnes: %d\nQuarters: %d\nDimes: %d\nNickels: %d\nPennies: %d\n",
    		twentiesInTill, tensInTill, fivesInTill, dollarsInTill, quartersInTill, dimesInTill, nickelsInTill, penniesInTill);
    
    	return 0;
    }
    That is how your code ends. It should compile as long as you include stdio.

    Also, my fscanf() calls look into a file and look for:
    Code:
    83.45
    75.20
    Just for example. Since 83.45 will be the cost, and 75.20 is what you paid.

    If you have questions please feel free to ask.


    - Stack Overflow
    Last edited by Stack Overflow; 11-11-2004 at 08:30 PM.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  2. #17
    Registered User
    Join Date
    Nov 2004
    Posts
    16
    So, if I copy all of the revised code you just posted, it should compile?

  3. #18
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Yes, it should. From my "int main() {" line and on. Let me know if it doesn't.

    Edit: If it doesn't, maybe this will:
    Code:
    #include <stdio.h>
    
    int main() {
    	// The file
    	FILE *transactions;
    	// The numbers
    	float cost, payment, change;
    	// The remainder in depth
    	int penniesInTill = 0, nickelsInTill = 0, dimesInTill = 0, quartersInTill = 0;
    	int dollarsInTill = 0, fivesInTill = 0, tensInTill = 0, twentiesInTill = 0;
    	
    	transactions = fopen ("transactions.dat", "r");
    	if (transactions == NULL) {
    		printf ("File, transactions, could not be opened\n");
    	}
    
    	fscanf(transactions, "%f\n", &cost);
    	printf("The cost is %g\n", cost);
    	fscanf(transactions, "%f\n", &payment);
    	printf("The customer's payment was %g\n", payment);
    	change = cost - payment;
    	printf("The change due is %g\n", change);
    
    	while (change >= 20.0f) {
    		change -= 20.0f;
    		twentiesInTill++;
    	}
    	while (change >= 10.0f) {
    		change -= 10.0f;
    		tensInTill++;
    	}
    	while (change >= 5.0f) {
    		change -= 5.0f;
    		fivesInTill++;
    	}
    	while (change >= 1.0f) {
    		change -= 1.0f;
    		dollarsInTill++;
    	}
    	while (change >= 0.25f) {
    		change -= 0.25f;
    		quartersInTill++;
    	}
    	while (change >= 0.10f) {
    		change -= 0.10f;
    		dimesInTill++;
    	}
    	while (change >= 0.05f) {
    		change -= 0.05f;
    		nickelsInTill++;
    	}
    	while (change >= 0.01f) {
    		change -= 0.01f;
    		penniesInTill++;
    	}
    
    	printf("In Detail\n\nTwenties: %d\nTens: %d\nFives: %d\nOnes: %d\nQuarters: %d\nDimes: %d\nNickels: %d\nPennies: %d\n",
    	twentiesInTill, tensInTill, fivesInTill, dollarsInTill, quartersInTill, dimesInTill, nickelsInTill, penniesInTill);
    
    	return 0;
    }
    - Stack Overflow
    Last edited by Stack Overflow; 11-11-2004 at 08:41 PM.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  4. #19
    Registered User
    Join Date
    Nov 2004
    Posts
    16
    Just to double check, before I insert it, you said the following code
    fscanf(transactions, "%f\n", &cost);
    printf ("The cost is %g\n", cost);
    fscanf(transactions, "%f\n", &payment);
    printf("The customer's payment was %g\n", payment);
    change = cost - payment;
    printf("The change due is %g\n", change);
    was fine to use?

  5. #20
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Yes, that code is fine.

    fscanf(transactions, "%f\n", &cost);
    > Gets the cost from the file

    printf ("The cost is %g\n", cost);
    > Prints the cost that we got from file

    fscanf(transactions, "%f\n", &payment);
    > Gets the payment from file

    printf("The customer's payment was %g\n", payment);
    > Prints the payment we got from file

    change = cost - payment;
    > Gets the difference between the two

    printf("The change due is %g\n", change);
    > Prints that difference we just calculated.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  6. #21
    Registered User
    Join Date
    Nov 2004
    Posts
    16
    Thank you so much for the help. I cannot believe my code was even close to correct, LOL. I have coped the revised code into a text file in notebook, and tomorrow will copy it to my compiler and try to compile it. I will let you know if it works.

    Thanks again!!!!!!!

  7. #22
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Glad to be of assistance I'll keep in touch.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  8. #23
    Registered User
    Join Date
    Nov 2004
    Posts
    16
    I went ahead and tried to compile it and got the following errors;
    changer.c:30:28: warning: multi-line string literals are deprecated
    changer.c:31:22: missing terminating ' character
    changer.c:31:22: warning: character constant too long
    changer.c:35: invalid operands to binary >=
    changer.c:36: invalid operands to binary -
    changer.c:39: invalid operands to binary >=
    changer.c:40: invalid operands to binary -
    changer.c:43: invalid operands to binary >=
    changer.c:44: invalid operands to binary -
    changer.c:47: invalid operands to binary >=
    changer.c:48: invalid operands to binary -
    changer.c:51: invalid operands to binary >=
    changer.c:52: invalid operands to binary -
    changer.c:55: invalid operands to binary >=
    changer.c:56: invalid operands to binary -
    changer.c:59: invalid operands to binary >=
    changer.c:60: invalid operands to binary -
    changer.c:63: invalid operands to binary >=
    changer.c:64: invalid operands to binary -
    changer.c:68:16: warning: multi-line string literals are deprecated

  9. #24
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    The code compiles well. See if your not slicing strings through multiple lines.

  10. #25
    Registered User
    Join Date
    Nov 2004
    Posts
    16
    Quote Originally Posted by xErath
    The code compiles well. See if your not slicing strings through multiple lines.
    what do you mean?

  11. #26
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by Prelude
    Code:
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define STAGE_LEFT EXIT_FAILURE
    #define ENCORE EXIT_SUCCESS
    #define TOO_SHORT NULL
    #define toilet stdout
    #define BLING 1
    #define EMPTY_WALLET 0
    
    double jinglie[] = {
      100.00, 50.00, 20.00, 10.00, 5.00, 1.00, .50, .25, .10, .05, .01
    };
    
    int main ( void )
    {
      char thingie[BUFSIZ];
      double all_zat, da_dough, leftovas;
    
      printf ( "Price: " );
      fflush ( toilet );
      if ( fgets ( thingie, sizeof thingie, stdin ) == TOO_SHORT
        || sscanf ( thingie, "%lf", &all_zat ) != BLING )
      {
        exit ( STAGE_LEFT );
      }
    
      printf ( "Amount: " );
      fflush ( toilet );
      if ( fgets ( thingie, sizeof thingie, stdin ) == TOO_SHORT
        || sscanf ( thingie, "%lf", &da_dough ) != BLING )
      {
        exit ( STAGE_LEFT );
      }
    
      leftovas = da_dough - all_zat;
    
      printf ( "Change Due:\n" );
      if ( leftovas >= EMPTY_WALLET ) {
        int stuff = 0;
    
        while ( stuff < sizeof jinglie / sizeof *jinglie ) {
          if ( leftovas >= jinglie[stuff] ) {
            printf ( "%.2f\n", jinglie[stuff] );
            leftovas -= jinglie[stuff];
          }
          else
            ++stuff;
        }
      }
      else
        printf ( "You still owe %.2f\n", fabs ( leftovas ) );
    
      return ENCORE;
    }
    Oh, I kill me.
    really girl. you just all dat and a big bag 'O chips wit da Dips!!! LOL. seriously, your a goddess. I can't wait till I'm where you are. Kudos.
    flawless!

  12. #27
    Registered User
    Join Date
    Nov 2004
    Posts
    16
    It is only giving me one warning now;
    changer.c:63:16: warning: multi-line string literals are deprecated
    I have looked the code over, and do not see what the problem is.

  13. #28
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by ChrisH
    It is only giving me one warning now;


    I have looked the code over, and do not see what the problem is.
    why don't you just copy and paste his EXACT code. it'll save alot of time and effort

  14. #29
    Registered User
    Join Date
    Nov 2004
    Posts
    16
    Quote Originally Posted by caroundw5h
    why don't you just copy and paste his EXACT code. it'll save alot of time and effort
    That is exactly what I did. It still gives me that one warning/error.

  15. #30
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    ChrisH, you don't happen to be from LSU do you?

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

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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