Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Sep 1st, 2006, 12:44 AM   #1
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 120
Rep Power: 3 brad sue is on a distinguished road
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
brad sue is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC