I am trying to calculate sales totals from a txt file. I have completed it but when I debug, it tells me that the input string is not in a correct format. I think something needs to be changed to double but when I do it gives me an error. Any suggestions?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Week8Assign1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void ReadSales(List<int> salesList)
{
try
{
StreamReader inputFile = File.OpenText("Sales.txt");
while (!inputFile.EndOfStream)
{
salesList.Add(int.Parse(inputFile.ReadLine()));
}
inputFile.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void DisplaySales(List<int> salesList)
{
foreach (int sales in salesList)
{
SaleslistBox.Items.Add(sales);
}
}
private double Total(List<int> salesList)
{
double total = 0;
foreach (int sales in salesList)
{
total += sales;
}
return total;
}
private void buttonCalc_Click(object sender, EventArgs e)
{
double TotalSales;
List<int> salesList = new List<int>();
ReadSales(salesList);
DisplaySales(salesList);
TotalSales = Total(salesList);
SalesTotalLabel.Text = TotalSales.ToString("c2");
}
private void buttonExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}