Functions

• 09-16-2006
thewizardalbany
Functions
I am struggling horribly with functions and I am wondering if anyone can tell me why the following code does not display the value of dollar correctly. I have commented out the middle bit as I have know idea how to get that part going as a function.

Code:

```#include <stdio.h> void GetData(float &change) {         printf("Using intervals of five for the cents, please enter any amount of dollars and cents: \$");         scanf("%f%*c", &change);         return; } void SplitChange(int &dollar, int &cents) {         int change;         dollar = (int) change;         cents = (int) (((change - dollar)*100) + 0.5);         return; } int main() {         float change;         int dollar, cents;         const int ONEHUNDREDDOLLARS = 100, FIFTYDOLLARS = 50, TWENTYDOLLARS = 20,                 TENDOLLARS = 10, FIVEDOLLARS = 5, TWODOLLARS = 2, ONEDOLLAR = 1, FIFTYCENTS = 50,                 TWENTYCENTS = 20, TENCENTS = 10, FIVECENTS = 5;                 GetData(change);         SplitChange(dollar, cents);                         /*printf("The notes and coins required to make change for %f\n dollars and cents are: ", change);                                while(dollar >= ONEHUNDREDDOLLARS)                 {                 printf("%d dollars ", ONEHUNDREDDOLLARS);                 dollar -= ONEHUNDREDDOLLARS;                 if(dollar > 0)                         printf(", ");                 }                                 if(dollar >= FIFTYDOLLARS)                 {                 printf("%d dollars ", FIFTYDOLLARS);                 dollar -= FIFTYDOLLARS;                 if(dollar > 0)                         printf(", ");                 }                         while(dollar >= TWENTYDOLLARS)                 {                 printf("%d dollars ", TWENTYDOLLARS);                 dollar -= TWENTYDOLLARS;                 if(dollar > 0)                         printf(", ");                 }                 while(dollar >= TENDOLLARS)                 {                 printf("%d dollars ", TENDOLLARS);                 dollar -= TENDOLLARS;                 if(dollar > 0)                         printf(", ");                 }                                 if(dollar >= FIVEDOLLARS)                 {                 printf("%d dollars ", FIVEDOLLARS);                 dollar -= FIVEDOLLARS;                 if(dollar > 0)                         printf(", ");                 }                 while(dollar >= TWODOLLARS)                 {                 printf("%d dollars ", TWODOLLARS);                 dollar -= TWODOLLARS;                 if(dollar > 0)                         printf(", ");                 }                 if(dollar >= ONEDOLLAR)                 {                 printf("%d dollar ", ONEDOLLAR);                 dollar -= ONEDOLLAR;                 if(dollar > 0)                         printf(", ");                 }                 if(cents >= FIFTYCENTS)                 {                 printf("%d cents ", FIFTYCENTS);                 cents -= FIFTYCENTS;                 if(cents > 0)                         printf(", ");                 }                         while(cents >= TWENTYCENTS)                 {                 printf("%d cents ", TWENTYCENTS);                 cents -= TWENTYCENTS;                 if(cents > 0)                         printf(", ");                 }                         if(cents >= TENCENTS)                 {                 printf("%d cents ", TENCENTS);                 cents -= TENCENTS;                 if(cents > 0)                         printf(", ");                 }                 if(cents >= FIVECENTS)                 {                 printf("%d cents ", FIVECENTS);                 cents -= FIVECENTS;                 if(cents > 0)                         printf(", ");                 }*/         printf("%d dollar ", dollar);                 return(0); }```
Thanks heaps
• 09-16-2006
dwks
Use [code] tags before Quzah reads that! Quick! (It's [code], not <code>.)

 Okay, Dave_Sinkula did it for you. Now I can read it.
Code:

`void SplitChange(int &dollar, int &cents)`
That looks like C++ to me . . . Dave . . .
Code:

```void SplitChange(int &dollar, int &cents) {         int change;         dollar = (int) change;         cents = (int) (((change - dollar)*100) + 0.5);         return; }```
That has a serious problem with an uninitialized variable.
[edit=2]
I'm guessing you want something like this, assuming change is the total number of cents involved:
Code:

```void SplitChange(int change, int &dollar, int &cents) {         dollar = change / 100;         cents = change % 100;         return; }```
[/edit][/edit]