![]() |
strange problem
Hi ,
I have this problem I need to code in C++: we want to know how many candies you can have with a certain amount of money. Each candy costs $1 and at each candy purchase, you receive 1 coupon. but you can redeem 1 candy with 7 coupons. If you have $20, you can have 23 candies. 20/7=2 more candies from the coupons=2 more coupons. then 20%7=6 coupons remain. 6+2=8 coupons with which you can have 1 candy. So 20+2+1=23 candies in total ( with 2 coupon left). I wrote this code below. The starnge problem I have is that when I run it with $20 as total cash, it gives me a number of coupons equal to 68??? It should be 20 at the beginning! [code] #include<iostream> using namespace std; int main() { int candies,cash,coupon,finish; int new_candies=0; candies=cash; coupon=cash; cout<<"Enter the amount of money: "; cin>>cash;// I enter 20. cout<<endl; new_candies=coupon/7; cout<<"coupon"<<coupon<<endl;// it gives 68 cout<<"New candies"<<new_candies<<endl;//9 that's ok since coupon=68 while (coupon>7) { new_candies=coupon/7; candies+=new_candies; coupon-=new_candies*6; } cout<<candies<<endl; cin>>finish; //to keep the output window open return 0; } [\code] can you tell me what is going on? or it is my compiler (DEV-C++) that bugs? thank you. B |
:
|
you should put this ( coupon=cash; ) after your input
|
I would also initialize all those variables to something (0), just for clarity's sake.
|
| All times are GMT -5. The time now is 12:31 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC