Hello alice,
In fact one thing I learned in C is its not easy to pass an integer thru a function, then to write it and send it back. What I would do instead is write the data to your struct, and then read it from then on, example:
Code:
struct notes {
int oneHundred;
int twenty;
int remainder;
};
void convertToNotes(int money, struct notes *NOTES);
int main() {
int amount;
struct notes *NOTES = (notes*) malloc (sizeof(notes)); // Allocated memory to get data
printf("Enter your amount: ");
scanf("%i", &amount);
convertToNotes(amount, NOTES); // Send data to function
printf("%i\n%i\n%i\n", NOTES->oneHundred, NOTES->twenty, NOTES->remainder);
free(NOTES); // Free memory
return 0;
}
void convertToNotes(int money, struct notes *NOTES) {
// Use '->' if using a pointer for struct else use '.'
NOTES->oneHundred=money/100; /* 1278 / 100 = 12 */
NOTES->twenty=(money-NOTES->oneHundred*100)/20; /* (1278 - 12 x 100) / 20 = 3 */
NOTES->remainder=money-NOTES->oneHundred*100-NOTES->twenty*20; /* 1278 - 12 x 100 - 3 x 20 = 18 */
}
I changed some things around here. What this does now is the program now calls the function directly then pointing to your structure notes. Then it sets the data. Of course we allocated a slot of memory for the structure which we free later.
Why we allocate memory is because when you use 'struct *' call you have to set the pointer to read something, and it can't read empty memory so once you allocate memory for it you're all set. Also you don't need to make convertToNotes an int call because you shouldnt have to return a value when the function is over. Example:
Code:
int myFunc(void);
int main() {
int test;
test = myFunc();
}
int myFunc(void) {
return (255);
}
Then 'test' would equal 255. Unless you need that, you wont need the int call for your functions.
Hope this helps,
- Stack Overflow