Jeff Mann
Jeff Mann

Reputation: 73

How to create an if/else statement to accomplish this

How could I create another if/else statement to output two house types if a user were to enter the same base price and square footage for two houses?? All help is appreciated.

#include using namespace std;

int main()
{
    int baseColonial;
    int baseSplit;
    int baseSingle;
    int sqftColonial;
    int sqftSplit;
    int sqftSingle;
    int priceColonial;
    int priceSplit;
    int priceSingle;

    cout << "Please enter the base price of the Colonial home: ";
    cin >> baseColonial;
    cout << "Now please enter the finished area in square feet: ";
    cin >> sqftColonial;

    cout << "Please enter the base price of the Split-entry home: ";
    cin >> baseSplit;
    cout << "Now please enter the finished area in square feet: ";
    cin >> sqftSplit;

    cout << "Please enter the base price of the Single-story home: ";
    cin >> baseSingle;
    cout << "Now please enter the finished area in square feet: ";
    cin >> sqftSingle;

    priceColonial = baseColonial / sqftColonial;
    priceSplit = baseSplit / sqftSplit;
    priceSingle = baseSingle / sqftSingle;


    if ((priceColonial <= priceSplit) && (priceColonial <= priceSingle))
    {
        cout << endl << "The Colonial house is the cheapest." << endl;
    }
    else if ((priceSplit <= priceColonial) && (priceColonial >= priceSingle))
    {
        cout << endl << "The split-entry house is the cheapest." << endl;
    }
    else if ((priceSingle <= priceSplit) && (priceSplit >= priceColonial))
    {
        cout << endl << "The single-story house if the cheapest." << endl;
    }
    return 0;

I just tried to use this code for a scenario where all 3 are the same price per sq ft but it's not functioning correctly. What am I missing?

else if ((priceSingle == priceSplit) && (priceSingle == priceColonial)) { cout << endl << "All three house models have the same price per square foot." << endl; }

Upvotes: 0

Views: 111

Answers (3)

laszlok
laszlok

Reputation: 2515

The else if block will only be executed if the condition in the first if wasn't met. You generally cannot solve this kind of problem with a single if/else chain, or only with an absurdly mammoth one that's practically begging for killer typos. (Also, imagine that you then need to add a fourth type: it becomes practically unmanageable - even though the problem is clearly simple to solve for many types.)

Just to indicate the direction you want to go, you'll probably want something like this:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

namespace {

const int HOUSE_TYPE_COUNT = 3;
const char* const HOUSE_TYPE_NAMES[HOUSE_TYPE_COUNT] = {"Colonial", "Split-entry", "Single-story"};

struct Entry {
    const char* const name;
    int basePrice;
    int areaSqFt;
    int price;

    Entry(const char * const name_) : name(name_) {}
};

Entry inputEntry(const char* const name) {
    Entry result(name);
    cout << "Please enter the base price of the " << name << " home: ";
    cin >> result.basePrice;
    cout << "Now please enter the finished area in square feet: ";
    cin >> result.areaSqFt;
    result.price = result.basePrice / result.areaSqFt;
    return result;
}

bool cheaper(Entry& left, Entry& right) {
    return left.price < right.price;
}

}

int main() {
    vector<Entry> entries;

    // User enters information
    for (int i = 0; i < HOUSE_TYPE_COUNT; ++i) {
        entries.push_back(inputEntry(HOUSE_TYPE_NAMES[i])); 
    }

    // Find cheapest price
    int cheapest = min_element(entries.begin(), entries.end(), cheaper)->price;

    // Output all types corresponding to cheapest price
    for (auto it = entries.begin(); it != entries.end(); ++it) {
        if (it->price == cheapest) {
            cout << "The " << it->name << " house is the cheapest." << endl;
        }
    }
    return 0;
}

If you want the types on one line, you can do things like

#include <sstream>

// ...

ostringstream answer;
answer << "The cheapest type(s): ";
const char* separator = "";
for (auto it = entries.begin(); it != entries.end(); ++it) {
    if (it->price == cheapest) {
        answer << separator << it->name;
        separator = ", ";
    }
}
cout << answer.str() << endl;

(Disclaimer: The code above is sloppy in a number of ways. Obviously it should check user inputs, not use plain C chars, not use integer division, and probably a lot more. It's just to give a general idea about the approach you'll want to use.)

Upvotes: 0

Biruk Abebe
Biruk Abebe

Reputation: 2233

You need to change the >= and <= comparison in your if/else if statements to just > or < for the last else statement to work. Otherwise the first if condition will be met when all the homes have equal price. That is, if for example if all homes have a price of 50 then since you your first condition specifies (priceColonial <= priceSplit) && (priceColonial <= priceSingle) this condition will be true since you are also checking for equality in this condition.

you could have something like this:

if ((priceColonial < priceSplit) && (priceColonial < priceSingle))
{
    cout << endl << "The Colonial house is the cheapest." << endl;
}
else if ((priceSplit < priceColonial) && (priceColonial > priceSingle))
{
    cout << endl << "The split-entry house is the cheapest." << endl;
}
else if ((priceSingle < priceSplit) && (priceSplit > priceColonial))
{
    cout << endl << "The single-story house if the cheapest." << endl;
}
else if ((priceSingle == priceSplit) && (priceSingle==priceColonial)) 
{ 
    cout << endl << "All three house models have the same price per     square foot." << endl; 
}

Upvotes: 0

bunty
bunty

Reputation: 629

Check below else-if ladder:

if (priceColonial < priceSplit) {
    if (priceColonial < priceSingle) {
        cout << endl << "The Colonial house is the cheapest." << endl;
    }
    else if(priceColonial == priceSingle) {
        cout << endl << "The Colonial and Single-story houses are cheaper." << endl;
    }
    else {
        cout << endl << "The Single-entry house is the cheapest." << endl;
    }
}
else if(priceColonial == priceSplit) {
    if (priceColonial < priceSingle) {
        cout << endl << "The Colonial and Split-entry houses are cheaper." << endl;
    }
    else if(priceColonial == priceSingle) {
        cout << endl << "All are of same price." << endl;
    }
    else {
        cout << endl << "The Single-entry house is the cheapest." << endl;
    }
}
else {
    if (priceSplit < priceSingle) {
        cout << endl << "The Split-entry house is the cheapest." << endl;
    }
    else if(priceSplit == priceSingle) {
        cout << endl << "The Split-entry and Single-story houses are cheaper." << endl;
    }
    else {
        cout << endl << "The Single-entry house is the cheapest." << endl;
    }
}

Upvotes: 0

Related Questions