![]() |
|
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2006
Posts: 120
Rep Power: 3
![]() |
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 |
|
|
|
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Strange behavior: getting different results from the same input on a function. WTH? | pulpfiction | C | 1 | Mar 28th, 2006 11:43 PM |
| Strange Problem? | magic_e | PHP | 10 | Feb 9th, 2006 8:54 PM |
| cgi/perl script + IE problem | joyceshee | Perl | 2 | Jan 24th, 2006 11:10 AM |
| Strange, but true! | frankish | C++ | 9 | Oct 26th, 2005 9:04 PM |
| string problem when passing in linked list | quantz | C++ | 0 | Feb 27th, 2005 10:11 AM |