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

• 04-29-2004
McFury
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. :rolleyes:

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. :confused:

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.

for question 2a I have done so far:
Code:

```/******************************************************************************** Author: Steve Date: April 2004 Purpose: ICT 102, Assignment Program Two, Cents. ********************************************************************************/ #include <stdio.h> #include <conio.h> #include <math.h> /******************************************************************************* ********************************************************************************                             MAIN PROGRAM STARTS HERE ******************************************************************************** *******************************************************************************/ int main() {                 //Variables         int i, result, fifty, twenty, ten, five, money;         char option;         printf("This program calculates the amount of cents, and what type of coins need to be returned to the customer.");                 do          {                  printf("\n\nDo you wish to continue? (Y/N): ");           //MAKES UPPER OR LOWER CASE CHARACTERS INTO THIER OPPOSITE VALUE.                     do              {             option = getch();             option = toupper (option);             }while (!((option=='Y')||(option=='N')));             printf("%c\n",option);             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", &money);                   if ((money < 5) || (money > 95))                           printf("You cannot enter an amount less then 5 or greater than 95."\n);                   return (1);                            else                           {                           for (i=0; i<money; i+5)        //really need help here :[                         {                                                    //FINDS HOW MANY OF EACH COIN WILL BE NEEDED.                                         fifty = money / 50;                         result %= 50;                         twenty = money / 20;                         result %= 20;                           ten = money / 10;                           result %= 10;                           five = money / 5;                         result %= 5;                         //AMOUNT OF EACH COIN BEING RETURNED.                         printf("To make change for %d, you will need the following coins:\n", money);                                                 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)                 }                  } //IF USER ENTERED 'N' FOR NO, PROGRAM QUITS.         while (option!='N'); return(0); } /******************************************************************************* ********************************************************************************                             MAIN PROGRAM ENDS HERE ******************************************************************************** *******************************************************************************/```

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.

Again your solution for this problem should be modular.

for question 2b I have done so far:
Code:

```/******************************************************************************** Author: Steve Date: April 2004 Purpose: ICT 102, Assignment Program Three, Dollars and Cents. ********************************************************************************/ #include <stdio.h> #include <conio.h> #include <math.h> #include <ctype.h> /******************************************************************************* ********************************************************************************                             MAIN PROGRAM STARTS HERE ******************************************************************************** *******************************************************************************/ int main() {                 //Variables         float money;         int i, total, dollars, fifty, twenty, ten, five;         char option;         printf("This program calculates the amount of cents, and what type of coins need to be returned to the customer.");                 do          {                  printf("\n\nDo you wish to continue? (Y/N): ");           //MAKES UPPER OR LOWER CASE CHARACTERS INTO THIER OPPOSITE VALUE.                     do              {             option = getch();             option = toupper (option);             }while (!((option=='Y')||(option=='N')));             printf("%c\n",option);             if (option=='Y')                           while (1)                 {                                     //GATHERS INPUT FROM USER.                   printf("Please enter the amount of Dollars and Cents that needs to be returned to the customer: \n");                   scanf("%f", &money);                   if (money < 5)                           printf("You cannot enter an amount less then 5."\n);                   return (1);                            else                           {                           for (i=0; i<money; i+5)        //I guessed at this and know its wrong, but i cant think of how to make it read in increments                         {                                //of 5+ at a time, instead of 1,2,3,4 (Australia doenst have anything less than 5 cent coins).                                                         //I suppose i need to find some way of rounding (money) off to the nearest '5'.                                                                    //FINDS HOW MANY OF EACH COIN WILL BE NEEDED.                                         dollars = total / 100                         total %= 100;                         fifty = total / 50;                         total %= 50;                         twenty = total / 20;                         total %= 20;                           ten = total / 10;                           total %= 10;                           five = total / 5;                         total %= 5;                         //CONVERTING FLOAT (AMOUNT) INTO AN INT                                                 ??? need help here to :/                         //AMOUNT OF EACH COIN BEING RETURNED.                         printf("To make change for %f, you will need the following coins:\n", money);                                                         if (dollars > 1)                                 printf("%d Dollars.\n", dollars);                         if (dollars == 1)                                 printf("%d Dollar.\n", dollars);                         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)                 }                  } //IF USER ENTERED 'N' FOR NO, PROGRAM QUITS.         while (option!='n'); return(0); } /******************************************************************************* ********************************************************************************                             MAIN PROGRAM ENDS HERE ******************************************************************************** *******************************************************************************/```
This is Homework, so I will understand if you people cannot help with this too much, but any help at all would be great :)
Sorry for this long post.
• 04-29-2004
OOPboredom
Code:

```fifty = money / 50; result %= 50; twenty = money / 20; result %= 20; ten = money / 10; result %= 10; five = money / 5; result %= 5;```
Take a look at your logic here. Suppose that the amount entered was 90.

fifty = 90 / 50 = 1
result %= 50 (*note: result hasn't even been given a value yet)

twenty = 90 / 20 = 4
ten = 90 / 10 = 9
five = 90 / 5 = 18

Look at what happens to the amount of change needed to be given; it continues to accumulate - you don't want this.

Let input = 90
fifty = input % 50 = 1
input = fifty * 50
(input is now 40)

twenty = input % 20 = 2
input = twenty * 20
(input is now 0);

Continue to do this until you get to five = ...
Note: See how input is being subtracted by the largest amount of change needed to be given.
• 04-29-2004
blue8173
For Question 2a, I don't think you need the for loop when you are calculating all the amounts in one loop.

Code:

```  for (i=0; i<money; i+5)        //really need help here :[ {            //FINDS HOW MANY OF EACH COIN WILL BE NEEDED.     fifty = money / 50;     result %= 50;     twenty = money / 20;     result %= 20;     ten = money / 10;     result %= 10;     five = money / 5;     result %= 5;     .... }```
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;```
• 04-29-2004
McFury

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. :confused:

here is the code at the moment:

Code:

```#include <stdio.h> #include <conio.h> #include <math.h> #include <ctype.h> /******************************************************************************* ********************************************************************************                           MAIN PROGRAM STARTS HERE ******************************************************************************** *******************************************************************************/ int main() {                 //Variables         int i, fifty, twenty, ten, five, result;         char option;         printf("This program calculates the amount of cents, and what type of coins need to be returned to the customer.");                 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("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);                 }                                                  //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)         }        //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. :( //IF USER ENTERED 'N' FOR NO, PROGRAM QUITS.         else (option!='N'); return(0); } /******************************************************************************* ********************************************************************************                           MAIN PROGRAM ENDS HERE ******************************************************************************** *******************************************************************************/ /*i=0; i<result; i+5;*/```
once again any help would be great. :)
thanks.
• 04-29-2004
nonpuz
to make sure the input from the user is in increments of five add this check:

|| input%5>0

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 (.).
• 04-29-2004
McFury

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.
• 04-29-2004
quzah
Your if statements are all wrong.
Code:

```if (fifty > 1)                                     printf("%d Fifty cent piece's.\n", fifty);```
What if I have one fifty cent piece? You don't count it. It should be > 0. Furthermore, "pieces" is not "piece's". The piece isn't showing posession. It's plural.

Quzah.
• 04-29-2004
nonpuz
i sent you a private message on here, it might be of interest to you. had some info in it.
• 04-30-2004
blue8173
> //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);

Yes, you can -- you can save the original amount of money in another variable, like your previous "money". So before any processing, money = result.

To print out the original amt, you can:

printf("To make change for %d, you will need the following coins:\n\n", money);
• 04-30-2004
enjoy
```1-procedure (decomposing any some ofmoney in this way) exemple : 1523 ===> 1000 ===> 500 ===> 20 ===> 3 2-comparing input <> how much must pay 1000 with her equal .... 3-procedure(difference) else money  in back```