Re: A pretty little bug in my code, need advice?

  • From: Jes <theeternalkid@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Wed, 7 Jul 2010 13:21:52 -0400

My bug has been fixed. The problem was I had the right paren after the division 
instead of before it. So the code was:
answer = (score_1+score_2+score_3/3)
It should have been:
answer = (score_1+score_2+score_3)/3;


On Jul 7, 2010, at 11:55 AM, Joseph Lee wrote:

> Hi Jess,
> Uh oh... This sounds like a logic error - where the program does what you
> don't want it to do, or perhaps you wrote the addition/division routine
> wrong. This is the reason why using parentheses helps. Try:
> answer = (score_1 + score_2 + score_3) / 3;
> Or, a more useful trick is:
> // The revised routine...
> // The aim is the store the added values in the answer, then divide the
> result by three within the output code.
> answer = score_1 + score_2 + score_3;
> cout << "The average is " << answer/3 << "." << endl;
> Note the "endl" string, which is used to insert a new line.
> Hope this helps.
> Cheers,
> Joseph
> 
> 
> -----Original Message-----
> From: programmingblind-bounce@xxxxxxxxxxxxx
> [mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Jes
> Sent: Wednesday, July 07, 2010 8:39 AM
> To: programmingblind@xxxxxxxxxxxxx
> Subject: A pretty little bug in my code, need advice?
> 
> Although the syntax of the code appears to be fine, I'm experiencing a nice
> little bug where all of the variables are being added and the result divided
> up in the same pass. This causes the prog to generate off the wall numbers.
> For example, if you put the number 8 for all three values, when it divides
> by 3, it gives you something like 18.6.
> As far as I can tell, and somebody correct me if I'm wrong, What I need to
> do is to have the program press an enter after each value  is added. How do
> I implement this in my code?
> Thanks
> Jes
> 
> __________
> View the list's information and change your settings at 
> //www.freelists.org/list/programmingblind
> 
> No virus found in this incoming message.
> Checked by AVG - www.avg.com 
> Version: 8.5.439 / Virus Database: 271.1.1/2986 - Release Date: 07/06/10
> 18:36:00
> 
> __________
> View the list's information and change your settings at 
> //www.freelists.org/list/programmingblind
> 

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

Other related posts: