-
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();
}
}
}
}
-
December 21st, 2012, 11:23 AM
#2
Re: small help with this C# program
Originally Posted by phantom89
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();
}
}
}
}
You're always going to show the last city because your variable "foundPosition" is being reset to cityName.Length-1 each time you execute the first loop. Your 2nd loop is not taking your "All" condition into account, you're always looping over all 4 cities in the loop variant but on your output line you're always referencing "foundPosition", so you're going to always get 4 lines of the last city in the list.
You need to add a little bit of intelligence that will inform your 2nd loop that all cities were selected and that they should all be output.
Looks like you're doing this as a class assignment. Let me know if anything I said above is unclear and I'll try to help you some more.
-Max :-)
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
|