Function: convert to notes

This is a discussion on Function: convert to notes within the C Programming forums, part of the General Programming Boards category; The function need to cal. the no. of bank notes given a certain amount of money, it will calculate the ...

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.

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.

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.

Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

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:

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:

Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.