searching an array for specific criteria and displaying

93 Views Asked by At

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");
}
1

There are 1 best solutions below

0
On
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