I have a program I have to finish which involves opening a file that has numbers, displaying it, then also displaying how many numbers the file has and the sum of them.
Im currently stuck on how to add all the numbers read and display it :/
Here is my code:
private void btnReadRandomNumbers_Click(object sender, EventArgs e)
{
StreamReader inputFile;
try
{
int number = 0;
int count = 0;
int sum = 0;
lstRandomNumbers.Items.Clear();
if (fodOpenFile.ShowDialog() == DialogResult.OK)
{
inputFile = File.OpenText(fodOpenFile.FileName);
lstRandomNumbers.Items.Clear();
while (!inputFile.EndOfStream)
{
number = int.Parse(inputFile.ReadLine());
count = count + 1;
lstRandomNumbers.Items.Add(number);
}
lblNumberCount.Text = count.ToString();
lblSumNumbers.Text = number.ToString();
}
}
catch (Exception ex)
{
MessageBox.Show("There is a problem with the disk file." + Environment.NewLine + ex.Message, "User Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
As seen in the Picture, the sum is only reading the last number of the list and im not sure why
Thanks for reading!