Need help with some C programs. (VERY Long Post)

This is a discussion on Need help with some C programs. (VERY Long Post) within the C Programming forums, part of the General Programming Boards category; Hi all,
Im pretty new to programming, so if my code makes no sence in parts, please forgive me.
I ...

Need help with some C programs. (VERY Long Post)

Hi all,
Im pretty new to programming, so if my code makes no sence in parts, please forgive me.

I need help with an assignment that I am doing. I have finished question 1 (was about Triangle types and whether they were valid, depending on what the user inputed), but I am stuck on question 2 (which has two parts).
Note that part 'b' is pretty much program 'a' except with added 'dollars'
Also I did not know really what rint() was in question 2b, so I did not add it to the program.

question 2a is:
You are asked to write a simple program that will accept an integer value in the range of 5-95 and in increments of 5 at a time, representing the number of cents to give to a customer in their change. The program should calculate how many coins of each denomination and display this to the user. Valid coin values are 50, 20, 10 and 5. Your solution (program and algorithm) should be modular in nature. This requires the submission of a high-level algorithm and suitable decompositions of each step.

question 2b is:
You are to extend your solution to part (a) above by allowing the user to enter the amount of change as dollars and cents as a double (real number). Valid dollar values are 100, 50, 20, 10, 5 and 1. You program should output the correct combination of dollar and cent denominations required.

In Part b you will need to explicitly convert a double to an integer to break the inputted double down to its component parts as integers. You can use the following code as the basis for doing this:

float change;
int dollar, cents;

...

dollar = (int) change;
cents = (int) rint(((change - dollar)*100));

Note to use the rint() function you will need to include the math.h file.

Also, you are assigning "result" the value of the remainder from the previous calculation, but you are not using it to calculate the next denomination.

Code:

//FINDS HOW MANY OF EACH COIN WILL BE NEEDED.
fifty = money / 50;
result %= 50;
twenty = result / 20; //Use RESULT instead of MONEY
result %= 20;
ten = result / 10;
result %= 10;
five = result / 5;
result %= 5;

Ive got rid of the 'money' value and changed it all to 'result' and listed it as an int variable, and i changed the problem with the result accumulating.

But how am I suppose to make the input(from the user) be in increments of 5, and how/where would it fit into the program?
I guessed it would be something like ( i=0; i<result; i+5; )
but no matter where I try to put the code it says 'Unreachable code in function main'
any ideas on how I could do this?

Other than that ive still got some errors at the end of the program when I try to compile it but I cannot see it. It says that its missing a semicolon (I wrote in the program where it is)
and it says anything beyond that (the bottom else statement etc) doesnt count.

to make sure the input from the user is in increments of five add this check:

|| input%5>0
to your loop.

also:

Code:

if (option=='Y')
{
while (1)
{
//GATHERS INPUT FROM USER.
printf("Please enter the amount of cents that needs to be returned to the customer: \n");
scanf("%d", &result);
if ((result < 0) || (result > 95))
printf("You cannot enter an amount less then 0 or greater than 95.\n");
return (1); /* what are you doing here? why do you even need a loop for one iteration? */
}
//FINDS HOW MANY OF EACH COIN WILL BE NEEDED.
fifty = result % 50;
result = fifty * 50;
twenty = result % 20;
result = twenty * 20;
ten = result % 10;
result = ten * 10;
five = result % 5;
result = five * 5;
//AMOUNT OF EACH COIN BEING RETURNED.
printf("To make change for %d, you will need the following coins:\n", result);
if (fifty > 1)
printf("%d Fifty cent piece's.\n", fifty);
if (fifty == 1)
printf("%d Fifty cent piece.\n", fifty);
if (twenty > 1)
printf("%d Twenty cent piece's.\n", twenty);
if (twenty == 1)
printf("%d Twenty cent piece.\n", twenty);
if (ten > 1)
printf("%d Ten cent piece's\n", ten);
if (ten == 1)
printf("%d Ten cent piece\n", ten);
if (five > 1)
printf("%d Five cent piece's\n", five);
if (five == 1)
printf("%d Five cent piece.\n",five);
} //This is where the ; error is. The only reason I put this bracket here was because it said
//one was missing. If I add a semicolon in it just adds more errors. :(

Another tip, you can simplify (read: get rid of) those if statements by using the tertiary operator, ie ? You can use it like this:

printf("%d twenty cent piece%s\n",twenty,twenty>1 ? "s." : ".");

basically this tests whether twenty is greater than 1 if so, append an s. to piece, otherwise just append a period (.).

But now i seem to have more problems
I ended up changing back to using all of those 'If' statements
because doing it the other way ie.

Code:

printf("\n%d Fifty cent piece%s\n",fifty,fifty>1 ? "s." : ".");

displayed '0' results as well as positive ones ie.
2 Twenty cent pieces.
0 Ten cent pieces.
0 Five cent pieces.
where as the 'If' way they dont show up

my program is working a bit better now but some of the other problems im getting are:
1) if i enter a character instead of a number at the start of the program where it asks for the users input, the program just keeps repeating that character infinatly down screen and i dont know how to make so it only accepts an integer input (so that if anything else is entered , the error message is triggered).
2) if i enter a number that is not in an increment of 5 it displays the error message fine, but then also displays the 'amount of coins' part and prints out a bunch of crazy numbers like
2147348480 Fifty cent piece's.
1 Ten cent piece.
and I dont know what to do to stop it doing that
3) is there a way to show the original amount that the user entered, in the printf statement down the bottom of the program when it displays the coins needed?

This is what it looks like now

Code:

#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <ctype.h>
/*******************************************************************************
********************************************************************************
MAIN PROGRAM STARTS HERE
********************************************************************************
*******************************************************************************/
int main()
{
//Variables
int fifty, twenty, ten, five, result;
char option;
printf("\nThis program decides how many, and of what type of coins need to be returned\nto the customer."); //If i enter a character here it goes crazy and spams continuously.
do
{
printf("\n\nDo you wish to continue? (Y/N): ");
//MAKES UPPER OR LOWER CASE CHARACTERS INTO THIER OPPOSITE VALUE.
option = getch();
option = toupper (option);
}
while (!((option=='Y') || (option=='N')));
printf("%c\n",option);
if (option=='Y')
{
while (1)
{
//GATHERS INPUT FROM USER.
printf("\nEnter the amount of cents that needs to be returned to the customer: ");
scanf("%d", &result);
if ((result<0) || (result>95) || (result%5>0))
printf("\nYou cannot enter an amount less then 0 and greater than 95.\nNumbers must be in increments of 5\n");
else
//FINDS HOW MANY OF EACH COIN WILL BE NEEDED.
fifty = result / 50;
result %= 50;
twenty = result / 20;
result %= 20;
ten = result / 10;
result %= 10;
five = result / 5;
result %= 5;
//AMOUNT OF EACH COIN BEING RETURNED.
//Can I make this show the original amount that the user entered? ie. "To make change for 65 cents, you will need the following coins" ?
printf("To make change for the amount you entered, you will need the following coins:\n\n", result);
if (fifty > 1)
printf("%d Fifty cent piece's.\n", fifty);
if (fifty == 1)
printf("%d Fifty cent piece.\n", fifty);
if (twenty > 1)
printf("%d Twenty cent piece's.\n", twenty);
if (twenty == 1)
printf("%d Twenty cent piece.\n", twenty);
if (ten > 1)
printf("%d Ten cent piece's\n", ten);
if (ten == 1)
printf("%d Ten cent piece.\n", ten);
if (five > 1)
printf("%d Five cent piece's\n", five);
if (five == 1)
printf("%d Five cent piece.\n", five);
}
}
return(0);
}
/*******************************************************************************
********************************************************************************
MAIN PROGRAM ENDS HERE
********************************************************************************
*******************************************************************************/

I really need help in finishing this
been up all night trying hehe
thanks.