error: Cannot implicitly convert type string to int

  • Thread starter Thread starter kgosi_mocumi
  • Start date Start date
K

kgosi_mocumi

Guest
Hi guys, my code seems to be working except for when I try to get the total sum of the number of the numbers which are read from a text file. The program that I am trying to develop is a Random Number File Reader program. I get this error: Cannot implicitly convert type 'string' to 'int'. I declared my total as an int. Should I convert it to a string also?




namespace Programming_problem_5._14
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnRead_Click(object sender, EventArgs e)
{
try
{
// Declare a StreamReader variable.
StreamReader inputFile;

// Declare a variable to hold the random numbers.
string amount;

// Declare a variable for the number of random numbers read from the text File.
int linecount = 0;

// Declare variable for the total sum of the random numbers.
int total = 0;

// Create output file.
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
// Open the selected file.
inputFile = File.OpenText(openFileDialog1.FileName);

// Read the File's contents.
while (!inputFile.EndOfStream)
{
// Get the amounts
amount = inputFile.ReadLine();

// Add the amounts to the ListBox.
lstNumbers.Items.Add(amount);

// Increment for the number of random numbers in text file.
linecount++;

// Increment for the total sum of the random numbers read from the text File.
total += amount;
}
// Display the total sum of the random numbers read from the text File.
lstNumbers.Items.Add("The total of the numbers is: " + total);

// Display the number of random numbers read from text file.
lstNumbers.Items.Add("The number of random numbers read from the File is "+ linecount);

// Close the file
inputFile.Close();
}
}
catch (Exception ex)
{
// Display an error message.
MessageBox.Show(ex.Message);
}
}
}
}

Continue reading...
 
Back
Top