Thread: Need help with C program

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 09: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.

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, 01: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, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM