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

You should check the error message and see what line the error is occuring on.

Also you are reading in the document to a List<int> here

salesList.Add(int.Parse(inputFile.ReadLine()));

Why not trying

salesList.Add(Convert.ToInt32(inputFile.ReadLine()));

Of course I haven't used that in awhile but it should work (just throw this into a try/catch) (sorry I can't say more I am in class right now)

Remember you are reading in that data as a string, btw if you want to apply each new line to an index in the List you could try something like this (what I use all the time)

List<string> input = new List<string>();

try
{
    if (File.Exists(fileName)) //makes sure the file exists
    {
        string temp = File.ReadAllText(fileName);
        input.AddRange(temp.Split(new String [] { "\r\n", "," }, StringSplitOptions.RemoveEmptyEntries)); //splits the data by comma or new line
    }
    else //file doesn't exist, error message
    {
        MessageBox.Show("The File You Are Trying To Read In Does\nNot Exist Please Select An Exisiting FIle", "File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}
catch (Exception error)
{
    MessageBox.Show(error.ToString());
}

This reads in the whole document and stores it into a string, then the line below it splits up the data. If it comes across a new line or a comma, it adds it as a new index to the List. I use this all the time and love it (you would have to convert it afterwords ... but this little snippet of code I find very useful)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.