I am trying to write a C program to calculate change due. How would I go about doing this? It needs to print the amount of change due for each coin/dollar.

For Example, lets say someone is due back $45.35, I need it to calculate and print how many 20's, 10's, 5's, 1's, quarters, dimes, nickels and pennies must be handed out in order to equal that amount.

I am lost! :p :confused: :o

11-11-2004

quzah

First, you break it down into steps. Then you convert that into code. Logicly, in your mind, break down how you give change to someone. You do know how to give change, right? Start with the largest amount, which is smaller or equal to the amount of change, and subtract that. Keep subtracting it until the remainder is smaller than that money size. Then move down to the next smaller size. Repeat until you have given all of the change you need.

then I step by step need to ask it to calculate the number of each bill due. This is where I get confused on how to do this and put it into C...

11-11-2004

itsme86

1) Loop through your bills starting at the largest
2) While the value of that bill plus the amount of change you've already handed out is less than the total change due, give another bill.

11-11-2004

Stack Overflow

Ah, that's a good start.

Converting ideas to the C language is not difficult. It is almost like taking your ideas and talking them through to the compiler. For example.

1) I enter price of item» This tells us we need to either write a function or use a standard function to get input from the user. The best way to find out how to do this is some searching. To make this easier, lets just say we happened upon a site: cplusplus.com reference. This is a good site that explains most, if not all, of the standard library functions within the C language. We spot out stdio.h, string.h, and stdlib.h first. We know string.h has to do with strings, and stdlib with the standard library, while stdio is for I/O or Input and Output. Exactly what we need!

We scroll down for a bit and happen upon scanf(). What does this function do? It reads formatted data from stdin. Also what we need. stdin is the input stream, while stdout is the output stream.

4) print this number as change due» We we know about scanf() that takes input, but what about a function that prints output. If we go back to the cplusplus.com reference page we can look 8 functions up from where you found scanf(). What do we see? printf(). What does printf() do? It prints formatted data to stdout. Exactly. It works almost identical to scanf() though we print out variables that already exist. Here is more info about printf().

I hope this helps. If you have further questions, please feel free to ask.

Note: Each of the links of printf() and scanf() have full examples at the bottom of each page.

I hope this helps you get started. The ideas you have in mind for your project will take more than the first 4 steps, but its a start!

- Stack Overflow

11-11-2004

ChrisH

I think I get it, but do you mind giving me a quick example. Say, change due is $43.00. Show me, with the loop, I assume a while statement, I would calculate and have it print the number of 20's to hand out....

11-11-2004

Stack Overflow

> do you mind giving me a quick example» Not at all. Just for examples sake lets look at the following.

The while loop
The while loop has a syntax of the following:

Code:

while (expression) {statement
}

This should make sense. As long as the expression is true, the statement executes.

So lets take for instance 43 dollars. If we wanted to sucessfully find how many tens are in forty-three dollars all we would have to do is run a while loop in statement like the following:

Code:

As long as money is greater than 10
Subtract 10
Increment tens counter

If this makes sense, so should this:

Code:

int tens = 0, money = 43;

while (money > 10) {
money -= 10;
tens++;
}

That is just an example of how it could work. Everytime the while loop executes, money is subtracted by 10 and our integer called tens is incremented. The answer should be '4 tens'. After a while the loop does run out of 10's to subtract. Though of course we haven't printed any output so we would never know.

If you have further questions, please feel free to ask.

- Stack Overflow

11-11-2004

ChrisH

Ok, now lets say I dont want a person to enter a value for cost or payment, etc., but that the info is already stored in a file that I want the program to open, scan and then calculate the change due, based on the information in that file....

11-11-2004

Stack Overflow

Alright,

You haven't gone off my page yet ;)

Lets go back to the cplusplus reference page. We are familar with scanf() and printf(), though there is a new one: fscanf()

Why is it called fscanf()? Because it reads formatted data from a stream. File Scan Format - fscanf() [or that is my analogy]

How does fscanf() work? Well, that link should show you everything you need to know.

fscanf() does need an open stream of a file. Though we haven't opened a file yet. How do we? We use fopen() - File Open [simple, eh?]

fopen() works like a charm. You open a file with your filename and you can read it into memory, or better yet just open it and start scanning away with fscanf(). If you look near the bottom of fscanf()'s page you will see fopen() and fclose() being used.

Ok, I think I may get it. So, lets say now I have the program scanning the contents of the file and printing them out. How do I then get it to calculate the change due ... do I do it the same way, with the while statement?

11-11-2004

Stack Overflow

Ah Prelude,

All my efforts for nothing! ;) Just kidding.

I like the code personally, though it may confuse a beginner programmer.

All in all, gain help and gain trust; as they say it.

- Stack Overflow

11-11-2004

Stack Overflow

ChrisH,

Well you can do the calculation during each fscanf() if you have multiple lines.

We could take an approach like this:

Code:

Open file
Scan line
Find difference between price and amount paid
Scan again...
Close file

It would be foolish to make 5 to 10 if (fscanf())'s so why not do a while loop.