# Need help with summing up numbers

Page 1 of 1

## 11 Replies - 3327 Views - Last Post: 30 September 2012 - 09:35 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=293652&amp;s=5a762ebfaeb195cc34de2d15dd04b5b5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Noob99

• New D.I.C Head

Reputation: 0
• Posts: 6
• Joined: 24-September 12

# Need help with summing up numbers

Posted 29 September 2012 - 01:44 PM

So I'm writing a pretty basic program that sums up some numbers (I'm a beginner myself, as you can see ). But I'm kinda stuck now. My program keeps writing 0 as the result and I'm pretty sure what the problem is but I can't for the life of me figure out the solution!

In the ReadInput() method, "sum" keeps getting replaced everytime I enter a new number, correct? So how am I supposed to solve this, as you can see I tried solving it by typing sum = sum + ReadInput(); after the while iteration but that doesn't solve anything. Any help guys?

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication33
{
public class FloatNumbersWhile
{
private double sum;

public void Start()
{
WriteProgramInfo();
ShowResults();

}

{

//Reads a number. If the value is given as 0, end the iteration.
//Otherwise accumulate the results in the instance-variable sum

bool done = false;

while (!done)
{
sum = sum + ReadInput();

if (ReadInput() == 0)
{
done = true;
}

}

}

private void WriteProgramInfo()
{
Console.WriteLine("\n\n +++++++ Summation of numbers +++++++");
Console.WriteLine("           Using a while-statement\n");
Console.WriteLine();

}

{
Console.Write("Please enter a number: ");
return sum;
}
private void ShowResults()
{

Console.WriteLine("----------------------------------------");
Console.WriteLine(" The sum is             " + sum);
Console.WriteLine();
Console.WriteLine();

}

}
}

```

Is This A Good Question/Topic? 0

## Replies To: Need help with summing up numbers

• Saucy!

Reputation: 6237
• Posts: 24,002
• Joined: 23-August 08

## Re: Need help with summing up numbers

Posted 29 September 2012 - 01:49 PM

In ReadInput, you're reading directly into the member variable sum. Don't do that. Read into a locally created variable and return that to the caller.

### #3 Skydiver

• Code herder

Reputation: 4842
• Posts: 15,982
• Joined: 05-May 12

## Re: Need help with summing up numbers

Posted 29 September 2012 - 01:51 PM

On line 54, you are overwriting the member variable sum. Declare a local variable instead to get the user input.

### #4 Noob99

• New D.I.C Head

Reputation: 0
• Posts: 6
• Joined: 24-September 12

## Re: Need help with summing up numbers

Posted 29 September 2012 - 01:52 PM

JackOfAllTrades, on 29 September 2012 - 01:49 PM, said:

In ReadInput, you're reading directly into the member variable sum. Don't do that. Read into a locally created variable and return that to the caller.

Right! That was dumb of me, o well thanks !

### #5 Skydiver

• Code herder

Reputation: 4842
• Posts: 15,982
• Joined: 05-May 12

## Re: Need help with summing up numbers

Posted 29 September 2012 - 01:53 PM

And then for lines 31 and 33, you only want to call ReadInput() once. Also store that in a local variable. If the value is not zero, add it to sum, otherwise, set done to true.

### #6 Momerath

• D.I.C Lover

Reputation: 1020
• Posts: 2,463
• Joined: 04-October 09

## Re: Need help with summing up numbers

Posted 29 September 2012 - 08:51 PM

Just a stylistic issue (and I'm sure someone will disagree with this ), but don't use not logic, it's harder to read (so say the studies!). So don't say
```bool done = false;
while (!done) {
...

done = true;
}
```

Say something like
```Boolean isMore = true;
while (isMore) {
...

isMore = false;
}
```

### #7 Skydiver

• Code herder

Reputation: 4842
• Posts: 15,982
• Joined: 05-May 12

## Re: Need help with summing up numbers

Posted 29 September 2012 - 09:39 PM

I think you misunderstood the advice about using "negative logic". The advice is to use variable names that are in the positive sense.

```bool notDone = true;
while (notDone)
{
:
notDone = false;
}

```

Good code:
```bool done = false;
while (!done)    // or while (done == false)
{
:
done = true;
}

```

### #8 Momerath

• D.I.C Lover

Reputation: 1020
• Posts: 2,463
• Joined: 04-October 09

## Re: Need help with summing up numbers

Posted 30 September 2012 - 02:37 AM

Nope, didn't make a mistake. The use of '!' is what is the issue (good variable names is another issue). It's common to miss the '!' when reading code, which can lead to errors if someone is trying to modify stuff they didn't write. While this is a simple example here, it really comes out when you use '!' with enclosed || and && logic, for example
```if (!(statement1 || statement2)) { ...
```

when
```if ((statement1 || statement2) == false) {
```
is clearer. Or the hideous
```if (!(statement1 || statement2) != true) { ...
```
which I've seen used more often than you'd think.

I'll see if I can't find where this is in my collection of books

This post has been edited by Momerath: 30 September 2012 - 02:41 AM

### #9 Skydiver

• Code herder

Reputation: 4842
• Posts: 15,982
• Joined: 05-May 12

## Re: Need help with summing up numbers

Posted 30 September 2012 - 07:03 AM

Oh! I see. That looks almost like something from Alexandrescu.

### #10 Noob99

• New D.I.C Head

Reputation: 0
• Posts: 6
• Joined: 24-September 12

## Re: Need help with summing up numbers

Posted 30 September 2012 - 09:14 AM

Hey guys, thanks everyone for the help yesterday. Today I got stuck again, with another part of the application:

``` private void BinNumbone() {
int i = 0;
int weeks= 52;
Console.WriteLine("Your bin empties the following weeks:");

for (i = 2; i < weeks; i++)

{
Console.Write("{0,15} {1,2}", "Week", i);
}

Console.WriteLine("--------------------");
}

```

So in this method I'm trying to print every second week in a year within a tabulation. Anyone know what the if statement under the foor loop would look like ? (I'm assuming I have to use an if statement)

### #11 Skydiver

• Code herder

Reputation: 4842
• Posts: 15,982
• Joined: 05-May 12

## Re: Need help with summing up numbers

Posted 30 September 2012 - 09:29 AM

Why use an if? Why not just increment i by 2, instead of incrementing it by 1 like you are doing now?

### #12 Noob99

• New D.I.C Head

Reputation: 0
• Posts: 6
• Joined: 24-September 12

## Re: Need help with summing up numbers

Posted 30 September 2012 - 09:35 AM

Ops, as you can see, I'm very new to this. Thank you