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

New Topic/Question
Reply



MultiQuote






|