Thread: Money Issues

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    5

    Money Issues

    This is just a snippet sample of what I'm trying to do, instead of posting my giant long program.

    Code:
    int main(void)
    
    {
    	float amount;
    
    	printf("Enter the amount of the monthly payment > ");
            scanf("%f", &amount);
    
    
    	printf("\n\n %.2f\n\n", amount);
    
    	return(0);
    
    }
    My program, which calculates a payment table works perfectly fine when I have everything set to int - but I need the program output to show amounts as xx.xx The problem is all the data that I need to use are even dollars, so I end up losing the zeros and when I put in ex: 54 (or 54.00) my output is .54 !! It's not even about the output because it doesn't store it correctly either because math-wise it will start calculating at .54 and not 54.

    I have been trying to figure this out for hours and it's probably something simple and stupid that I can't figure out what I'm doing wrong. Any help would be appreciated. I have only been writing code for 2 months so it needs to be something simple. I haven't been taught anything complicated yet.

    ...oh and I know that on scanf you are supposed to use lf for float, but when I use that it doesn't work at all! Arggg!

    I think I've made this sound complicated enough...sorry!! Thanks in advance.

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Just so you can see how the program works with ints - so you can maybe better understand what I am trying to do. What I need is for my output to show the dollar amounts as xx.xx

    Code:
    #include <stdio.h>
    int main(void)
    
    {
    	/* Assignments */
    
    	int account;      /* Account number									*/
    	int month;	/* Date - Month									*/
    	int day;		/* Date - Day										*/
    	int year;		/* Date - Year										*/
    	int fee;		/* Treatment Fee									*/
    	int inipay;		/* Initial Payment Amount							*/
    	int mopay;	/* Monthly Payment Amount							*/
    	int baldue;	/* Initial Balance Due								*/
    	int kount;		/* Payment Number									*/
    	int curbal;		/* Outstanding Balance								*/
    	int paid;		/* Total Currently Paid								*/
    
    	/* Get Input */
    
    	printf("\n\n Enter 4 digit account number > ");
    	scanf("%d", &account);
    	printf("\n Enter consultation date (mm/dd/yy) > ");
    	scanf("%d/%d/%d", &month, &day, &year);
    	printf("\n\n Enter treatment fee amount > ");
    	scanf("%.2d", &fee);
    	printf("\n\n Enter initial payment amount > ");
    	scanf("%.2d", &inipay);
    	printf("\n\n Enter monthly payment amount > ");
    	scanf("%d", &mopay);
    
    	baldue = fee - inipay;
    
    	printf("\n\nPATIENT ACCOUNT NUMBER:	%d", account);
    	printf("\nCONSULTATION DATE:	%02d/%02d/%02d", month, day, year);
    	printf("\n\nTREATMENT FEE:						$%10.2d", fee);
    	printf("\nINITIAL PAYMENT:					 %10.2d", inipay);
    	printf("\nBALANCE DUE:						 %10.2d", baldue);
    	printf("\n\n	PAYMENT	   DUE		PAYMENT		TOTAL		OUTSTANDING");
    	printf("\n	NUMBER	   DATE 	 AMOUNT	    	PAID		BALANCE");
    
    	kount = 1;
    	month = month + 1;
    	day = 1;
    	paid = inipay + mopay;
    	curbal = baldue - mopay;
    
    
    	while (curbal >= mopay)
    		{
    			printf("\n\n	%3d	", kount);
    			kount = kount + 1;
    
    
    			printf(" %02d/%02d/%02d", month, day, year);
    			month = month + 1;
    
    
    
    
    
    			printf("	%d", mopay);
    
    
    
    			printf("		%d", paid);
    			paid = paid + mopay;
    
    
    			printf("			%d", curbal);
    			curbal = fee - paid;
    
    
    
    			if (month > 12)
    				year = year + 1;
    			if (month > 12)
    				month = 1;
    
    
    
    		}
    
    
    
    		printf("\n\n	%3d	", kount);
    		kount = kount + 1;
    
    		printf(" %02d/%02d/%02d", month, day, year);
    		month = month + 1;
    
    		printf("	%d", mopay);
    		printf("		%d", paid);
    		printf("			%d", curbal);
    
    
    		mopay = curbal;
    		paid = paid + mopay;
    		curbal = fee - paid;
    
    		printf("\n\n	%3d	", kount);
    		printf(" %02d/%02d/%02d", month, day, year);
    		printf("	%d", mopay);
    		printf("		%d", paid);
    		printf("			%d\n\n", curbal);
    
    
    
    
    	return (0);
    
    }

    I know it is very basic and maybe even possibly data redundant, but it simply gets the job done. I just need to figure out how to make the numbers floats so I can have the required output.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Here's an idea. Take your .54 and immediately multiply it by 100 to make it 54. Then have the amount processed, as normal, as an integer.

    Just before you're ready to report results, divide it by 100 and put it back into float form, and into the right size.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    1) Floats are bad for money because of arithmetic problems
    2) The formatting you want for xx.xx is printf("%05.2f\n", amount);

    The format specifier %05.2f means "Output a float 5 characters long, give me 2 places after the decimal, and pad the output from the left with zeros." The reason for the five characters is that it's xx.xx which is 5 characters. Kind of a gludge, but it works.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    I need it to work with a variety of numbers.

    Sample input: Treatment Fee 2500.00; Initial payment 300.00; Monthly 75.00

    If I'm using those numbers it stores 25 3 75.

    I have a version using int that cheats - printf("%d.00", variable) - and I could probably get away with it, but I would like the program to work with any input including uneven dollar amounts. Though, I could be asking too much of myself being a beginner and should stick with my cheated version

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You could always work in pennies instead of dollars... Use integers, but divide by 100 when displaying.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Preprocessor issues
    By sedontane in forum C++ Programming
    Replies: 4
    Last Post: 10-12-2009, 04:58 AM
  2. Money problem
    By Tesnik in forum C++ Programming
    Replies: 6
    Last Post: 08-24-2007, 08:12 AM
  3. Compiler Issues
    By marliwht in forum C Programming
    Replies: 6
    Last Post: 10-21-2005, 12:57 PM
  4. How the rich get rich [Long]
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 05-05-2005, 10:36 PM
  5. Money classs
    By blackjs in forum C++ Programming
    Replies: 5
    Last Post: 12-04-2001, 10:43 AM