0

Hi I am completely new to C# programming and I am getting stuck on my code. My program is to ask the user for a city or zip code, the amount of beds/baths they want, and their price range. I need to to search through my array and then display all the houses that meet their criteria (think Zillow, the website). My code currently displays random houses that do not meet any of the criteria I selected for the home. HELP!

for (int a = 0; a < HOUSES; ++a)
{
    if (zipChecker == zip[a]) // check zip code
    {
        found = true;
        foundPosition = a;
    }

    if (BtnBath1.Checked) // check baths
    {
        if (bath[a] > 0 && bath[a] <= 1)
        {
            found = true;
            foundPosition = a;
        }
    }
    else if (BtnBath2.Checked) // check baths
    {
        if (bath[a] > 1 && bath[a] <= 2)
        {
            found = true;
            foundPosition = a;
        }
    }
    else if (BtnBath3.Checked) // check baths
    {
        if (bath[a] > 2 && bath[a] <= 3)
        {
            found = true;
            foundPosition = a;
        }
    }
    else if (BtnBath4.Checked) // check baths
    {
        if (bath[a] > 3)
        {
            found = true;
            foundPosition = a;
        }
    }

    if (BtnBed1.Checked) // check bed
    {
        if (bed[a] > 0 && bed[a] <= 1)
        {
            found = true;
            foundPosition = a;
        }
    }
    else if (BtnBed2.Checked) //check bed
    {
        if (bed[a] > 1 && bed[a] <= 2)
        {
            found = true;
            foundPosition = a;
        }
    }
    else if (BtnBed3.Checked) //check bed
    {
        if (bed[a] > 2 || bed[a] <= 3)
        {
            found = true;
            foundPosition = a;
        }
    }
    else if (BtnBed4.Checked) //check bed
    {
        if (bed[a] > 3)
        {
            found = true;
            foundPosition = a;
        }
    }

    if (BoxPrice1.Checked) //check price
    {
        if (price[a] < 200000 && price[a] > 300000)
        {
            found = false;
            foundPosition = a;
        }
    }
    if (BoxPrice2.Checked) //check price
    {
        if (price[a] < 300000 && price[a] > 400000)
        {
            found = false;
            foundPosition = a;
        }
    }
    if (BoxPrice3.Checked) //check price
    {
        if (price[a] < 400000 && price[a] > 500000)
        {
            found = false;
            foundPosition = a;
        }
    }
    if (BoxPrice4.Checked) //check price
    {
        if (price[a] < 500000)
        {
            found = false;
            foundPosition = a;
        }
    }
}

if (found)
{
    label1.Text +=
        string.Format("Bed: {0}, Bath:{1}, Asking Price:{2}, City:{3}, SQFT:{4}, " +
            "Zip Code:{5}, Year:{6}", bed[foundPosition], bath[foundPosition], 
            price[foundPosition].ToString("c2"), city[foundPosition], 
            sqft[foundPosition].ToString("n0"), zip[foundPosition], 
            year[foundPosition]);
}
else 
{
    label1.Text = ("Sorry there were no houses that met your criteria");
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • 1
    Is there any reason you're not storing the information about a house in a `House` class, rather than a bunch of different arrays? It would make it a lot easier and less error-prone. – Rufus L Dec 09 '14 at 04:50
  • looks like its the prices. i think you mean to check whether a price is greater than the lower bound and lower than the upper bound. – mrtig Dec 09 '14 at 04:56
  • Also, the reason you're getting houses that don't match all the criteria, is that your logic is not doing what you want it to do. You are looping through ALL the houses, and you're setting `found = true` whenever you meet one of the criteria. The only time it is set to false is if it doesn't meet the price range. But your conditions for price range are all impossible. for example, a number will never be < 400000 AND > 500000. – Rufus L Dec 09 '14 at 04:59
  • Yes I see the mistake in the pricing portion, thank you. However, I had commented out the pricing portion and just ran the radio buttons and those were not working to my criteria either. – Kayla Galindo Dec 09 '14 at 05:07
  • Please take a book and learn object oriented programming. You should be using a class named `House`, all the required properties and methods in it. Then everything becomes easy. – Sriram Sakthivel Dec 09 '14 at 06:51

1 Answers1

0
int printcount = 0;
for (int a = 0; a < HOUSES; ++a) {
    if (zipChecker == zip[a]) // check zip code
    {
        found = true;
        foundPosition = a;
    } else break;
    if (BtnBath1.Checked) // check baths
    {
        if (bath[a] > 0 && bath[a] <= 1) {
            found = true;
            foundPosition = a;
        } else break;
    }
    if (BtnBath2.Checked) // check baths
    {
        if (bath[a] > 1 && bath[a] <= 2) {
            found = true;
            foundPosition = a;
        } else break;
    }
    if (BtnBath3.Checked) // check baths
    {
        if (bath[a] > 2 && bath[a] <= 3) {
            found = true;
            foundPosition = a;
        } else break;
    }
    if (BtnBath4.Checked) // check baths
    {
        if (bath[a] > 3) {
            found = true;
            foundPosition = a;
        } else break;
    }

    if (BtnBed1.Checked) // check bed
    {
        if (bed[a] > 0 && bed[a] <= 1) {
            found = true;
            foundPosition = a;
        } else break;
    }
    if (BtnBed2.Checked) //check bed
    {
        if (bed[a] > 1 && bed[a] <= 2) {
            found = true;
            foundPosition = a;
        } else break;
    }
    if (BtnBed3.Checked) //check bed
    {
        if (bed[a] > 2 || bed[a] <= 3) {
            found = true;
            foundPosition = a;
        } else break;
    }
    if (BtnBed4.Checked) //check bed
    {
        if (bed[a] > 3) {
            found = true;
            foundPosition = a;
        } else break;
    }
    if (BoxPrice1.Checked) //check price
    {
        if (price[a] < 200000 && price[a] > 300000) {
            found = false;
            foundPosition = a;
        } else break;
    }
    if (BoxPrice2.Checked) //check price
    {
        if (price[a] < 300000 && price[a] > 400000) {
            found = false;
            foundPosition = a;
        } else break;
    }
    if (BoxPrice3.Checked) //check price
    {
        if (price[a] < 400000 && price[a] > 500000) {
            found = false;
            foundPosition = a;
        } else break;
    }
    if (BoxPrice4.Checked) //check price
    {
        if (price[a] < 500000) {
            found = false;
            foundPosition = a;
        } else break;
    }
    if (found) {
        printcount++;
        label1.Text += string.Format("Bed: {0}, Bath:{1}, Asking Price:{2}, City:{3},SQFT:{4}, Zip Code:{5}, Year:{6}", bed[foundPosition], bath[foundPosition], price[foundPosition].ToString("c2"), city[foundPosition], sqft[foundPosition].ToString("n0"), zip[foundPosition], year[foundPosition]);
    }
}
if (printcount == 0) label1.Text = ("Sorry there were no houses that met your criteria");

just changed the code for your requirement but can't test it

Kavin Eswaramoorthy
  • 1,595
  • 11
  • 19