-
December 20th, 2012, 07:18 PM
#1
small help with this C# program
Hello, I need help with this program. When the user types in All, I want the sum of the information i have loaded in array 4 to display. However, its not doing this. Any input would be great! Thank you!
Code:
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
const string FILENAME = "FinalPractice.txt";
string[] cityName = new string[4];
string[] population = new string[4];
string[] income = new string[4];
double[] sales = new double[4];
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//given info
FileStream inFile = new FileStream(FILENAME, FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(inFile);
reader.Close();
inFile.Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Focus();
}
private void button2_Click(object sender, EventArgs e)
{
string cityInput;
string userInput;
bool found = false;
int foundPosition = -999;
double totalSales = 0;
const string all= "All";
FileStream inFile = new FileStream(FILENAME, FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(inFile);
for (int x = 0; x < cityName.Length; ++x)
{
cityName[x] = reader.ReadLine();
population[x] = reader.ReadLine();
income[x] = reader.ReadLine();
sales[x] = Convert.ToDouble(reader.ReadLine());
}
reader.Close();
inFile.Close();
//user input of data
cityInput = textBox1.Text;
for (int x = 0; x < cityName.Length; ++x)
{
if (cityInput == cityName[x] || cityInput==all)
{
found = true;
totalSales += sales[x];
foundPosition = x;
}
}
//check to see if found
if (found == true)
{
for (int x = 0; x < cityName.Length; ++x)
{
label2.Text = String.Format("{0},{1}, {2}, and {3}", cityInput, population[foundPosition], income[foundPosition], sales[foundPosition]);
}
}
else
{
label2.Text = "Sorry that city is not found";
label2.Focus();
}
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|