1. Coin counting (logic problem)

Problem here. I assume that I put too many nested if and else statements and screwed up somewhere :x. Anyway, the project is:
Write a program to print the coins necessary to return change to a customer after a purchase. The change can be made up of quarters, dimes, nickels and pennies. You may assume that the amount of change is less than \$1. Your program should use the minimum number of coins needed. Input the amount of change in cents.
Yeah, So I've been at this for a while and came up to the conclusion that I suck at programming. Help would be greatly appreciated .
I get no value for Quarters (q) and everything else says its equal to 0 in the output.
Code:
```#include <iostream.h>
#include <conio.h>

main()
{
int q1, d1, n1, p1, ans, change;
int q=0;
int d=0;
int n=0;
int p=0;

do
{
cout << "What is the change amount? ";
cin >> change;
if (change>25)
do
{
q1=change-25;
q++;
change=change-(q*25);
}
while(q1>=25);
else
if (change>10)
do
{
d1=change-10;
d++;
change=change-(d*10);
}
while (d1>=10);
else
if (change>5)
do
{
n1=change-5;
n++;
change=change-(n*5);
}
while (n1>=5);
else
if (change>1)
do
{
p1=change-1;
p++;
change=change-(p*1);
}
while (p1>=1);
else
cout << "Amount of quarters " << q << '\n';
cout << "Amount of dimes " << d << '\n';
cout << "Amount of nickels " << n << '\n';
cout << "Amount of pennies " << p << '\n';
cout << "\nTry again? (1 = yes, 2 = no): ";
cin >> ans;
}
while (ans=1);
return 0;
}```

2. Hey, I'm not that great at programming, but I could offer some help since two heads are better than one.

But from what I'm seeing and judging from your output, I think you have a problem with the if/else statements. The change variable enters the first if and I went through it using the number 37. It passes the first if statement test as it is greater than 25, so it performs the operations within the if statement. Therefore, q1 is updated to 12, q is icremented to 1, and change is updated to be 12 (which all makes sense).

However, since it passed the test for the if statement, wouldn't it skip the else statement because the if was satisfied. Therfore all of your nested if/else statements are ignored because the first if statement is performed and therefore it would not have to go to the else statement.

Hope this helps

3. Here is a program that calculates the amount of change in a given sum. I believe this might point you in the right direction for what you are doing.

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

void change(float, int*, int*, int*, int*); //function prototype

int main(void){

float fMoney;
int iQuarters, iDimes, iNickels, iPennies;

//Prompt for money entry
printf("Enter how much money you wish to convert to change: ");
scanf("%f", &fMoney);

//call our function to perform conversion
change(fMoney, &iQuarters, &iDimes, &iNickels, &iPennies);

//Display result
printf("\nYou have %d quarters, %d dimes, %d nickels, and %d
pennies\n", iQuarters, iDimes, iNickels, iPennies);

return(0);
}
//provide our function definition
void change(float fMoney, int* iQuart, int* iDime,
int* iNick, int* iPenn) {

//Determine how many quarters, dimes, nickels and pennies are in the money provided
*iQuart = (int)(fMoney / .25);
fMoney = fMoney - (.25 * *iQuart);
*iDime = (int)(fMoney / .10);
fMoney = fMoney - (.10 * *iDime);
*iNick = (int)(fMoney / .05);
fMoney = fMoney - (.05 * *iNick);
*iPenn = ceilf(fMoney*100); //ceilf used to counter rounding error experienced with floating point numbers
}```
Happy Coding!!

4. Yeah, I had a feeling that those if/else statements would kill me. So i wrote it over a bit:
Code:
```#include <iostream.h>
#include <conio.h>

main()
{
int change;
int ans;
int q=0;
int d=0;
int n=0;
int p=0;

do
{
cout << "What is the change amount? ";
cin >> change;

do
{
q1=change-25;
q++;
change=change-(q*25);
}
while(change>=25);
do
{
d1=change-10;
d++;
change=change-(d*10);
}
while (q1>=10);
do
{
n1=change-5;
n++;
change=change-(n*5);
}
while (d1>=5);
do
{
p1=change-1;
p++;
change=change-(p*1);
}
while (n1>=1);
cout << "Amount of quarters " << q << '\n';
cout << "Amount of dimes " << d << '\n';
cout << "Amount of nickels " << n << '\n';
cout << "Amount of pennies " << p << '\n';
cout << "\nTry again? (1 = yes, 2 = no): ";
cin >> ans;
}
while (ans=1);
return 0;
}```
Now, My main problem here is that, it keeps going even after it has enough coins. Example: If the change was 90, it would give me 3 quarters, 1 dime, 1 nickel, and 1 penny which equals 91. I'm guessing there could be a simple solution to this.. Hopefully someone out there knows it. Thanks for the reply anyway swayp.

5. Hey, no problem I hoped I help.

I was looking at your updated code and it appears you updated half of it and forget the rest heh .

You still have the variables q1,d1,etc. in there but they are not declared in the beginning, so which part did you mess up on. Did you not mean to use it or did you forget to declare it.

Judging from your output (although this could be totally off because I couldn't go through the code because of the above problem) one of your loops may be running one more time than you would like. Keep in mind that the do/while loop does the code automatically before it tests the condition. Then if the condition is still true, it will run again. If this is what you intended, just double check that it is running the right amount of times or perhaps implement it with a while loop.

If you want, you can post the updated code again and I'll look at it.

6. Alright let's go through this step by step soldier!
-----------------------
main returns an integer, so turn:
Code:
`main()`
into this:
Code:
`int main()`
-----------------------
Change this:
Code:
```int q1, d1, n1, p1, ans, change;
int q=0;
int d=0;
int n=0;
int p=0;```
to this (easier to read for ya)
Code:
```int change;
int quarters=0, dimes=0, nickels=0, pennies=0;```
-----------------------
You did good when asking for the change, but instead of doing it your way, make it into a loop, and STOP if there is no change left to deduct! First deduct change from the quarter, then the dime, etc. If you can deduct, go back to the top of the loop and continue deducting from the largest coin.
-----------------------
Now don't just copy this! Thou art must understand! Ask infinite questions if it needs be!
Code:
```#include <iostream>
//#include <conio.h> Not standard C++ :(

// We could do: using namespace std;
// But we'd be including more than we need/want!
using std::cout;
using std::cin;
using std::endl;

int main() {
int change;
int quarters=0, dimes=0, nickels=0, pennies=0;

cout << "What is the change amount? ";
cin >> change;

while(change > 0) {
if(change >= 25) { // You found a quarter, now deduct it from the change
quarters++;
change -= 25;
continue; // Go back to the top of the loop
}

if(change >= 10) { // You found a dime, now deduct it from the change
dimes++;
change -= 10;
continue; // Go back to deduct from the highest coin
}

if(change >= 5) {
nickels++;
change -= 5;
continue; // Go back to the top of the loop
}

if(change >= 1) {
pennies++;
change -= 1;
continue;
}
}

cout << "Quarters: " << quarters << endl;
cout << "Dimes:    " << dimes  << endl;
cout << "Nickels:  " << nickels  << endl;
cout << "Pennies:  " << pennies  << endl;

return 0;
}```

7. Way too much computing power wasted on loops...As an example, this simple code will give you the total coins for quarters in one single pass.
Code:
```if ( change >= 25 )
{
quarters = change / 25;
change %= 25;
}```
Then it's a simple matter of passing the new value of change to the dimes.
Code:
```if ( change >= 10 )
{
dimes = change / 10;
change %= 10;
}```
etc...

8. Damn, you make it look so easy
Just one question, what's "change -= 25" I don't get what that's doing

And why should i use 'int main()' instead of main(), I was never taught 'int main()'.

I also have no clue why I was never taught "continue", as it's very useful.

9. 1) change -= 25 is compiler shortcut for change = change - 25

2) Well for one reason Salem will kick your ass

10. Originally Posted by andyhunter
2) Well for one reason Salem will kick your ass

11. I'll try and stay away from him..

But it's still not working..

I used some of what you told me,
Code:
```#include <iostream.h>
#include <conio.h>

main()
{
int change;
int q=0,d=0,n=0,p=0;

while(change > 0)
{
if (change >= 25)
{
q++;
change = change - 25
continue;
}
if(change >= 10)
{
d++;
change = change - 10;
continue;
}
if(change >= 5)
{
n++;
change = change - 5;
continue;
}
if(change >=1)
{
p++;
change = change - 1;
continue;
}
}
cout << "Amount of quarters " << q << '\n';
cout << "Amount of dimes " << d << '\n';
cout << "Amount of nickels " << n << '\n';
cout << "Amount of pennies " << p << '\n';
return 0;
}```
I also tried compiling yours, and when I enter something in, it crashes. Maybe it's because im using the dev c++ compiler? D:

12. compile who's?

13. Originally Posted by xlokix
I also have no clue why I was never taught "continue", as it's very useful
It makes you go back to the top of the loop. break gets you out of the current loop. This is an infinite loop:
Code:
```do {
continue;
} while(false); // Break out of the loop once you get here```
Code:
```while(change > 0)
{
if (change >= 25)
{
q++;
change = change - 25
continue;
}
if(change >= 10)
{
d++;
change = change - 10;
continue;
}
if(change >= 5)
{
n++;
change = change - 5;
continue;
}
if(change >=1)
{
p++;
change = change - 1;
continue;
}
} // <-- You forgot to stop your while loop```
Keep asking questions, anything that you don't understand quite.

14. The continue statement is only used in while, for, and do/while loops.

Try the method I posted a few mins ago.

..ne'r mind. I just noticed you had the if statements enclosed in a while. Still I'd recommend the 1 pass method.

15. hold on, checking it over.