# [HELP] do-while loop

• 12-12-2010
hustlerism
[HELP] do-while loop
I've been doing some C++ project n i encountered some problems. my program doesnt want to loop.

Quote:

#include<stdio.h>

int main()
{
int price,depreciation,x=1,year,acc_depreciation,end_v alue;
char ans;

do{
printf("The price of the machine (RM):");
scanf("%d",&price);
printf("The rate of depreciation (RM):");
scanf("%d",&depreciation);
printf("Years of depreciation :");
scanf("%d",&year);

end_value=price-(year*depreciation);
acc_depreciation=price-end_value;

printf("YEAR DEPRECIATION END-YEAR-VALUE ACCUMULATED DEPRECIATION\n");
printf("---- ------------ -------------- ------------------------\n");

for(x=1;x<=year;x++)
{
printf("%d %d %d %d\n", x,depreciation,price-(x*depreciation),x*depreciation);
}

{
if(end_value<price/2)
printf("The final end-year-value (RM %d) is less than half of the actual price (RM %d).\n",end_value,price/2);
else if(end_value>price/2)
printf("The final end-year-value (RM %d) is greater than half of the actual price (RM %d).\n",end_value,price/2);
else
printf("The final end-year-value (RM %d) is equals to half of the actual price (RM %d).\n",end_value,price/2);
}

printf("Do you want to continue? Y-yes,N-no: ");
fflush(stdin);
scanf("%c\n",&ans);
}while(ans=='Y');

system("pause");
return 0;
}
• 12-12-2010
Salem
• 12-12-2010
hustlerism
ive edited the codes. sry im new here n i need some help pls.
*sigh*
• 12-12-2010
hugoguan
i don understand what you want to mean in below code. it seem syntax problem.

Code:

```{ if(end_value<price/2) printf("The final end-year-value (RM %d) is less than half of the actual price (RM %d).\n",end_value,price/2); else if(end_value>price/2) printf("The final end-year-value (RM %d) is greater than half of the actual price (RM %d).\n",end_value,price/2); else printf("The final end-year-value (RM %d) is equals to half of the actual price (RM %d).\n",end_value,price/2); }```
• 12-12-2010
hustlerism
idk how to explain cuz im a new programming student. but if u try the codes that ive given in Dev C++ u will know. RM is the currency of my country Malaysia. after key-in all the information needed it will show a table of calculated values. n then lastly "Do you want to continue?" n if i type Y it doesnt continue. thats the problem im facing right now.
• 12-12-2010
hugoguan
you need to change your
scanf("%c\n",&ans);
to
scanf("\n%c",&ans);
• 12-12-2010
hustlerism
Quote:

Originally Posted by hugoguan
you need to change your
scanf("%c\n",&ans);
to
scanf("\n%c",&ans);

Hey thx. the problem solved. thx very much. i rly appreciate it.