| | |
Need help with small beginner program, please.
![]() |
•
•
Join Date: Oct 2004
Posts: 2
Reputation:
Solved Threads: 0
:rolleyes:
This is a program where the user enters how much they owe, then they enter how much they are gonna pay, like (30 dollars for a $25.63 amount), and their change is calculated back to them in ten dollar bills, five dollar bills, one dollar bills, quarters, dimes, nickels, and pennies.
My instructor hinted us to use the mod operator but i found that '%' does not work with doubles or floats. So i am left with no clue on how to program this thing and i dont want to get an F, because so far ive been gettings A's. Its just this darn mod operator is so stubborn. thanks for any help i can get.
here is the code i have.
This is a program where the user enters how much they owe, then they enter how much they are gonna pay, like (30 dollars for a $25.63 amount), and their change is calculated back to them in ten dollar bills, five dollar bills, one dollar bills, quarters, dimes, nickels, and pennies.
My instructor hinted us to use the mod operator but i found that '%' does not work with doubles or floats. So i am left with no clue on how to program this thing and i dont want to get an F, because so far ive been gettings A's. Its just this darn mod operator is so stubborn. thanks for any help i can get.
here is the code i have.
C Syntax (Toggle Plain Text)
#include <stdio.h> /* includes stdio.h i/o */ void pause(void); /* declares function */ int main(void) { double due, given, bal = 0; /* sets double float variables */ double tens, fives, ones, quarters, dimes, nickels, pennies = 0; printf("\n\n\tHow much do you owe?\n\n\t\tAmount : "); /* asks for debt */ scanf("%lf", &due); /* accepts user's input */ printf("\n\n\tHow much will you pay?\n\n\t\tAmount : "); /* asks user how much they will give to pay for the amount */ scanf("%lf", &given); /* accepts user's input */ bal = given - due; /* calculates the user's change */ printf("\n\nAmount Due = $%.2lf", due); /* displays transaction info */ printf("\n\nAmount Given = $%.2lf", given); printf("\n\nAmount Balance = $%.2lf", bal); printf("\n\n\tHere is your Change."); if((bal / 10) >= 1) /* if can be divided by bill, determines how many bills */ { tens = bal / 10; bal = bal % 10; } if((bal / 5) >= 1) /* if can be divided by bill, determines how many bills */ { fives = bal / 5; bal = bal % 5; } if((bal / 1) >= 1) /* if can be divided by bill, determines how many bills */ { ones = bal / 1; bal = bal % 1; } if((bal / .25) >= 1) /* if can be divided by coin, determines how many coins */ { quarters = bal / .25; bal = bal % .25; } if((bal / .10) >= 1) /* if can be divided by coin, determines how many coins */ { dimes = bal / .10; bal = bal % .10; } if((bal / .05) >= 1) /* if can be divided by coin, determines how many coins */ { nickels = bal / .05; bal = bal % .05; } if((bal / .01) >= 1) /* if can be divided by coin, determines how many coins */ { pennies = bal / .01; bal = bal % .01; } printf("\nTens = %d", tens); /* prints change resulsts */ printf("\nFives = %d", fives); printf("\nOnes = %d", ones); printf("\nQuarters = %d", quarters); printf("\nDimes = %d", dimes); printf("\nNickels = %d", nickels); printf("\nPennies = %d", pennies); pause(); /* pauses */ return (0); } void pause(void) /* tells what pause does */ { char ch; /* sets character variable */ printf("\n\nPress \'Enter\' to exit."); scanf("%c", &ch); /* waits for 'enter' key */ scanf("%c", &ch); }
Last edited by alc6379; Oct 30th, 2004 at 7:12 pm. Reason: added [code] tags
>Its just this darn mod operator is so stubborn.
When working with money, it's best to use integers because floating-point has accuracy issues. However, if you really want to use floating-point, the standard library supports the fmod function. fmod will perform modulus on two floating-point values.
When working with money, it's best to use integers because floating-point has accuracy issues. However, if you really want to use floating-point, the standard library supports the fmod function. fmod will perform modulus on two floating-point values.
I'm here to prove you wrong.
![]() |
Similar Threads
- Projects for the Beginner (Python)
- Bank program help! (C++)
- Stockroom Program (Python)
- i need help to convert characters (C)
- cpu scheduling program (Java)
Other Threads in the C Forum
- Previous Thread: Problems of looping in saving to a text file
- Next Thread: binary trees
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






