Trying to Create While Loop?

I am trying to compute amount of change in quarters, nickels,dimes,etc. I have done the program and it runs fine--when no while loop is placed. Everytime I try to change to while loop it sends me into an infinite loop.

here is my program:

#include <iostream>
using namespace std;

int main (void)
{
float price, amount_given,dollars_back;
int change,quarters, dimes,nickels,pennies;

cout<<"Enter price of item: ";
cin>>price;

cout<<"Enter amount given: ";
cin>>amount_given;

dollars_back=amount_given-price;
change=(int)(dollars_back*100); //exact number of cents
cout<<"The change should be: "<<change<<"\n";

quarters=change/25;
change=change%25;
dimes=change/10;
change=change%10;
nickels=change/5;
pennies=change%5;
cout << "\nQuarters: " << quarters << endl; // display # of quarters
cout << " Dimes: " << dimes << endl; // display # of dimes
cout << " Nickels: " << nickels << endl; // display # of nickels
cout <<" Pennies: " << pennies << endl; // display # of pennies

system("Pause");
return(0);
}

i have tried to execute while (change>0) and that is what sends it into an infinite loop. How can I fix this?
just passing by ... here is the code with code tags: use [ code ] [ / code ] without spaces next time...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;

int main (void)
{
float price, amount_given,dollars_back;
int change,quarters, dimes,nickels,pennies;

cout<<"Enter price of item: ";
cin>>price;

cout<<"Enter amount given: ";
cin>>amount_given;

dollars_back=amount_given-price;
change=(int)(dollars_back*100); //exact number of cents
cout<<"The change should be: "<<change<<"\n";

quarters=change/25;
change=change%25;
dimes=change/10;
change=change%10;
nickels=change/5;
pennies=change%5;
cout << "\nQuarters: " << quarters << endl; // display # of quarters
cout << " Dimes: " << dimes << endl; // display # of dimes
cout << " Nickels: " << nickels << endl; // display # of nickels
cout <<" Pennies: " << pennies << endl; // display # of pennies

system("Pause");
return(0);
}

I don't understand where you want to put the while loop. I'm not sure if it would be appropriate to put a while loop to replace the calculations for quarters/dimes/nickels/pennies etc, because each of these is calculated a different way (i.e. there's no simple repetition here). Can you post your code with your attempted while loop? Alternatively, can you put what you're trying to achieve into pseudo-code?
Topic archived. No new replies allowed.