# Thread: Function: convert to notes

1. ## Function: convert to notes

The function need to cal. the no. of bank notes given a certain amount of money, it will calculate the number of hundred-dollar notes and twenty-dollar notes, given a certain amount of money, and also return the remaining amount of money not enough for a twenty-dollar note.

for example: \$1278
the function should return 12 hundred-dollar notes
,3 twenty-dollar notes, and then 18 as the remaining amount.

Can someone look my code and check my syntax error, thk a lot.
Code:
```struct notes {
int oneHundred;
int twenty;
int remainder;
};

void convertToNotes2(int money, struct notes* notes) {

(*notes).oneHundred=money/100;                      /* 1278 / 100 = 12 */
(*notes).twenty=(money-oneHundred*100)/20;          /* (1278 - 12 x 100) / 20 = 3 */
(*notes).remainder=money-oneHundred*100-twenty*20;  /* 1278 - 12 x 100 - 3 x 20 = 18 */

}```

2. Hello alice,

I might be able to help. First of all your syntax problem has to do with (*notes). You see when using a struct all you have to do is notes.twenty insteald of (*notes).twenty since you already have struct *notes notes in the function call.

What I will do is put the function convertToNotes2() in your struct, also making the three variables private so no other functions outside of the struct can touch/modify the values.

Code:
```#include <stdio.h>
#include <stdlib.h>

struct notes {
public:		// Outside functions can call on this
void convertToNotes2(int money);
private:	// Outside functions can't touch these
int oneHundred;
int twenty;
int remainder;
};

int main() {
int amount;
struct notes NOTES;

scanf("%i", &amount);

NOTES.convertToNotes2(amount);

return 0;
}

/*
** This function is inside struct notes
*/
void notes::convertToNotes2(int money) {
oneHundred = money / 100;				/* 1278 / 100 = 12 */
twenty = (money-oneHundred * 100) / 20;			/* (1278 - 12 x 100) / 20 = 3 */
remainder = (money-oneHundred * 100) - (twenty * 20);	/* 1278 - 12 x 100 - 3 x 20 = 18 */

printf("\$%i - %i (One hundred dollar bills)\n", (oneHundred*100), oneHundred);
printf("\$%i - %i (Twenty dollar bills)\n", (twenty*20), twenty);
printf("\$%i (Remainder)\n", remainder);
}```
Most of that should make sense, I just initialized the struct in main() and called on the function convertToNotes() which automatically sent retrieved the data and sent the text to the screen. Also the reason why you can modify the variables remainder, oneHundred, and twenty in the function convertToNotes() is because that function is within the struct speaking it can touch private variables.

Now of course if you still want the three integer variables in the public category in case you need to get the values feel free to move them however you see fit.

Hope this helps,
- Stack Overflow

3. Hi

I want to use
Code:
`void convertToNotes2(int money, struct notes* notes)`
as the function prototype. and
Code:
```struct notes {
int oneHundred;
int twenty;
int remainder;
};```
as the structure, is it correct if I change '(*notes).' to 'notes.' ?
the function only need to cal. the hundred,twenty and the remaining notes.it does not need to print the value out.

Thank a lot.

Code:
```struct notes {
int oneHundred;
int twenty;
int remainder;
};

void convertToNotes2(int money, struct notes* notes) {

notes.oneHundred=money/100;                      /* 1278 / 100 = 12 */
notes.twenty=(money-oneHundred*100)/20;          /* (1278 - 12 x 100) / 20 = 3 */
notes.remainder=money-oneHundred*100-twenty*20;  /* 1278 - 12 x 100 - 3 x 20 = 18 */

}```

4. Hello alice,

Glad to be of help, and yes, to run your current code all you have to do is change (*notes). to notes.. Example:

notes.oneHundred = (money / 100)

Will work.

- Stack Overflow

5. Hi

I need to change the function to another prototype (no struct use),
Code:
`int convertToNotes(int money, int* oneHundred, int* twenty);`
Did my code write correctly? please specify what I make wrong .
Thank a lot.

Code:
```int convertToNotes(int money, int* oneHundred, int* twenty) {

int remainder;

oneHundred=money/100;      /* 1278 / 100 = 12 */
twenty=(money-oneHundred*100)/20; /* (1278 - 12 x 100) / 20 = 3 */
remainder=money-oneHundred*100-twenty*20;  /* 1278 - 12 x 100 - 3 x 20 = 18 */

}```

6. 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

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

7. Hi

Thank for your detail reply.but I am sorry to let you know that I have not state clearly in my previous reply that I want to use another function ,

Code:
`int convertToNotes(int money, int* oneHundred, int* twenty); {`
because I want to learn using different function to make the program work.

Rgds

8. To dereference(retrieve or set the value it points to) a pointer, you use an asterisk.
Code:
```int convertToNotes(int money, int* oneHundred, int* twenty)
{
int remainder;

*oneHundred = money / 100;      /* 1278 / 100 = 12 */
*twenty = (money - *oneHundred * 100) / 20; /* (1278 - 12 x 100) / 20 = 3 */
remainder = money - *oneHundred * 100 - *twenty * 20;  /* 1278 - 12 x 100 - 3 x 20 = 18 */
}```
You may want to look into the modulus % operator. This returns the remainder of a division:
Code:
```int convertToNotes(int money, int* oneHundred, int* twenty)
{
int remainder;

*oneHundred = money / 100;
*twenty = (money % 100) / 20; /* 1278 % 100 == 78; 78 / 20 == 3 */
remainder = money % 20;       /* 1278 % 20 == 18; */
}```

9. hi,