11 Replies - 1946 Views - Last Post: 30 September 2012 - 09:35 AM Rate Topic: -----

#1 Noob99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 :P). 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?

Thanks in advance!


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

namespace ConsoleApplication33
{
    public class FloatNumbersWhile
    {
        private double sum;


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

        }

        private void ReadInputAndSumNumbers()
        {

            //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(); 

        }


        private double ReadInput()
        {
            Console.Write("Please enter a number: ");
            sum = double.Parse(Console.ReadLine());
            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

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,989
  • 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.
Was This Post Helpful? 1
  • +
  • -

#4 Noob99  Icon User is offline

  • New D.I.C Head

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

Re: Need help with summing up numbers

Posted 29 September 2012 - 01:52 PM

View PostJackOfAllTrades, 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 !
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,989
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • 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;
}

Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,989
  • 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.

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



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


Was This Post Helpful? 0
  • +
  • -

#8 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • 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

Was This Post Helpful? 1
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,989
  • 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.
Was This Post Helpful? 0
  • +
  • -

#10 Noob99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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)
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,989
  • 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?
Was This Post Helpful? 0
  • +
  • -

#12 Noob99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1