Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Sep 1st, 2006, 12:44 AM   #1
brad sue
Hobbyist Programmer
 
Join Date: Mar 2006
Posts: 115
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
Old Sep 1st, 2006, 1:16 AM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 748
Rep Power: 3 Jimbo is on a distinguished road
cpp Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. int candies,cash,coupon,finish; // no initialization?
  4. int new_candies=0;
  5. candies=cash; // cash not initialized yet
  6. coupon=cash; // cash not initialized yet
  7. /* ... */
  8. cout<<"coupon"<<coupon<<endl;// coupons still not initialized?
Jimbo is offline   Reply With Quote
Old Sep 1st, 2006, 2:19 AM   #3
Samuaijack
Programmer
 
Samuaijack's Avatar
 
Join Date: Jul 2006
Location: using Earth.Africa.Egypt.Cairo;
Posts: 71
Rep Power: 3 Samuaijack is on a distinguished road
you should put this ( coupon=cash; ) after your input
Samuaijack is offline   Reply With Quote
Old Sep 1st, 2006, 8:50 AM   #4
sarumont
Hobbyist Programmer
 
sarumont's Avatar
 
Join Date: Apr 2004
Location: /dev/urandom
Posts: 154
Rep Power: 5 sarumont is on a distinguished road
Send a message via ICQ to sarumont Send a message via AIM to sarumont Send a message via Yahoo to sarumont
I would also initialize all those variables to something (0), just for clarity's sake.
__________________
"Time is an illusion. Lunchtime doubly so."
-the late, great Douglas Adams
sarumont is offline   Reply With Quote
Reply

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 5:09 PM.

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