Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

my program will not display the sum of the sales array when typing all

$
0
0
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
   
    {
        //arrays from txt file
        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)
        {//activates text box
            textBox1.Focus();


        }

        private void button2_Click(object sender, EventArgs e)
        {//assign and declare variables
            string cityInput;
            string userInput;
            bool found = false;
            int foundPosition = -999;
            double totalSales = 0;
            const string all= "All"; 
             //reading from the b
            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 what user is typing is equal to what is in the array
                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)
                {
                    //heres what it would display
                    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();
            }
            

                








                








        }
    }
}

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>