Derek Pike
Derek Pike

Reputation: 41

reading from an array in c#

I am really close to getting this to work correctly, but I am just missing something. It's probably quite simple. I'm very new to C#. I have a textbox called txtState where you input a city(I know) to see if you have visited this city. It outputs a response to another textbox txtAnswer when you click the btnVisited button. Right now It's just inputting whatever I input into the txtState box and saying it's the 0 position in the array. I forgot to mention I need to include what position in the array this city is, and output that to the textbox as well. Here is my code:

namespace Test
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        txtAnswer.Text = "";
        txtState.Text = "";
    }

    private void button3_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void btnVisited_Click(object sender, EventArgs e)
    {
        string[] CityName = {"Columbus", "Bloomington", "Indianapolis",
        "Fort Wayne", "Greensburg", "Gary", "Chicago", "Atlanta", "Las Vegas"};
        bool visitedbool;
        int subscript;
        subscript = 0;
        visitedbool = false;
        string QueryCity;
        QueryCity = txtState.Text.ToUpper();
        do 
        {
            subscript += 0;
            if (subscript == 0)
                txtAnswer.Text = "You have visited" + " " + QueryCity + " " + "It is the" + " " + subscript + " " + "city you have visited.";

            else if (subscript == 1)
                txtAnswer.Text = "You have visited" + " " + QueryCity + " " + "It is the" + " " + subscript + " " + "st city you have visited.";

            else if (subscript == 2)
                txtAnswer.Text = "You have visited" + " " + QueryCity + " " + "It is the" + " " + subscript + " " + "nd city you have visited.";

            else if (subscript == 3)
                txtAnswer.Text = "You have visited" + " " + QueryCity + " " + "It is the" + " " + subscript + " " + "rd city you have visited.";

            else if (subscript == 4 - 8)
                txtAnswer.Text = "You have visited" + " " + QueryCity + " " + "It is the" + " " + subscript + " " + "th city you have visited.";

            else
                txtAnswer.Text = "You have not visited this city.";
        }
        while (visitedbool == true);       
    }
}
}

Upvotes: 0

Views: 174

Answers (1)

Dennis Traub
Dennis Traub

Reputation: 51674

First issue with your code:

This won't work: else if (subscript == 4 - 8)

Try this instead: else if (subscript >= 4 && subscript <= 8)

Second:

subscript += 0; is the same as subscript = subscript + 0;

If you're using subscript as some kind of counter it won't work since you're just adding 0

Third:

visitedbool will always be false. Your loop will only run once because you never set visitedbool to true

Upvotes: 1

Related Questions