HELP!! while loop doesn't execute

Hi,
I was writing a program that counts the lest number of coins needed(quarter, dime, nickle, cent) to return a certain amount of change. so i used a code with 4 while loops like this:

[code]
int cent;
cout<<"Please enter the amount of cents owned:\n";
cin>>cent;
int iquar=0, idime=0, inick=0, icent=0;

while(cent>25)
{
iquar=cent/25;
cent=cent%25;
}

while(cent>10)
{
idime=cent/10;
cent=cent%10;
}

while(cent>5)
{
inick=cent/5;
cent=cent%5;
}

while(cent>1)
icent=cent;
At this point, when i test the data, the loop didn't seem to execute. When i enter the number for "cent" as 49, the program just stopped there, nothing comes up after that. It only works for some numbers like 70 and 50 but didn't work for 52,53 or any number below 50. So i tested each loop too see if it executed correctly and i found out the problem was in the last loop
while(cent>1)
icent=cent;
so i write my code again with the just the line "icent=cent" without the using the last loop(and i kinda figured out that i didn't need the last loop) and the program worked. But i still don't get how the last loop affected my program and how it caused the first three loops to not execute.

I really appreciate if anyone could give some opinions on this problems. Thanks.
Last edited on
closed account (SECMoG1T)
Yeah coz your loop is infinite.

1
2
3
4
5
6
7
8
while(cent>5)////when the value of cent is equal to 5 this loop exits
 {
    inick+=cent/5;
    cent=cent%5;
  }

 while(cent>1)/// cent value is always 5, that value doesn't change . This loop is infinite. 
   icent=cent;
oh that's right, thanks a lot. I completely forgot about the counter for the loop. Thanks
closed account (SECMoG1T)
But note all your loops can't achieve what you want while they continue to overwrite the previous values in your variables, i guess you must be intending to add all computed vvalues to end up with the correct results.

1
2
3
4
5
6
7
///by that I mean your loops were intended to have this form
 while (cent> 25)
 {
    iquar+=cent/25;/// note '+'  
    cent   =cent% 25;
  }
 
yeah i intend to add them all up later, and i initially set the values of the type of coins to zero
Topic archived. No new replies allowed.