-1

I've read through all the other similar topic but unable to still find answer for mine. I've re-read my code and I don't see an issue. I've each if/is else statement in their own {} but I'm still only getting the first if expression statement output.

I've not totally new to programming but I'm new to C++.

In my comparison debugging I've done:

  • if home is less than home 2 or home 3 ->> home < home2 || home3
  • if home is less than home 2 and home 3 ->> home < home2 && home3

So I'm comparing price per sqft and output only the home with least price per sqft or if the price per sqft are the same between another home, then output that result. Changing || or && and vice versa and putting them in {} is still resulting in the same first if statement. I've even out the expression in ((...) || or && (...)) #include

using namespace std;

int main() {
    // Write your main here

long colonialPrice, splitEntryPrice, singleStoryPrice;
double colonialArea, splitEntryArea, singleStoryArea;



double splitEntryModel = splitEntryPrice/splitEntryArea;
double colonialModel = splitEntryPrice/splitEntryArea;
double singleStoryModel = singleStoryPrice/singleStoryArea;

cout << "Enter base price for colonial model, split-entry, and single-story model >> ";
cin >> colonialPrice >> splitEntryPrice >> singleStoryPrice;
cout << endl;

cout <<"Enter finished area in sqft colonial model, split-entry, and single-story >> ";
cin >> colonialArea >> splitEntryArea >> singleStoryArea;
cout <<  endl;


//colonial model
if(colonialModel < splitEntryModel || singleStoryModel)
   cout << "The price per square foot of the colonial model is the least" << endl;
else if(splitEntryModel < colonialModel || singleStoryModel)
   cout << "The price per square foot of the split-entry model is the least" << endl; 
else if (singleStoryModel< splitEntryPrice || colonialModel)
  cout << "The price per square foot of the single-story model is the least" << endl;
else if (colonialModel == splitEntryModel)
  cout << "The price per square foot of the colonial and split-entry models tie for the least." << endl;
else if(colonialModel == singleStoryModel)
  cout << "The price per square foot of the colonial and single-story models tie for the least." << endl;
else if(singleStoryModel == splitEntryModel)
 cout << "The price per square foot of the single-story and split-entry models tie for the least." << endl;
else if (colonialModel == singleStoryModel && splitEntryModel)
  cout << "The price per square foot all three models are the same." << endl;

return 0;

}

1 Answers1

0

The main issue is that the comparison logic is incorrect. In C++, you can't perform a multiple comparison like you tried to. Your statement

if (home < home2 || home3) 

is actually equivalent to

if (home < home2 || home3 != 0)

That is, "if home < home2 and home3 isn't zero". The || and && operator can only be used the way you want between booleans or expressions, not between numbers.

If you want a multiple comparison, you need to spell it explicitly. The correct formulation for what you wanted to achieve should be

if (home < home2 && home < home3)

which checks if home is both smaller than home2 and home3 at the same time, or

if (home < home2 || home < home3)

if you want just to check if home is smaller than home2 or smaller than home3.

Similarly, for checking equality you need to write an expression like

if (home == home2 && home == home3)

I leave it to you how to fix the rest of the conditions, following a similar logic.

P.S. if you are going to stick to C++ for longer, take the habit of avoiding using namespace std;, as it can create more problems than it solves. See the answers to this question for additional context.

EDIT 2022-10-10 As per question in comments, I decided to point out another reason why the code cannot run properly. In your code, you are calculating splitEntryModel, colonialModel and singleStoryModel before the user inputs the values. Variables in C++ aren't updated dynamically. All calculations reflect the value of the variable at the time they are performed.

One way to fix your code is to move the calculation of those three variables after all inputs are entered via std::cin.

DerPhysiker
  • 141
  • 7
  • That's still not the correct formulation for what the asker wanted to achieve, because `home < home2 || home < home3` doesn't imply that `home` is the smallest of those three values. – Nathan Pierson Oct 07 '22 at 14:12
  • @NathanPierson you are correct. According to the text in their question, they wanted to check "if home is less than home 2 or home 3 ->> home < home2 || home3", so I fixed that part. The rest of the question asks a different thing, I feel dumb for having skimmed through the code once I have found the logic mistake in the text. – DerPhysiker Oct 07 '22 at 14:36
  • Fixed following your comment, thanks – DerPhysiker Oct 07 '22 at 14:38
  • Some reason it doesn't work still – Wendy Xiong Oct 07 '22 at 21:59
  • @WendyXiong I know why: You can't calculate variables before assigning them. You have to declare colonialModel, splitEntryModel and singleStoryModel *after* the user inputs the values. I will edit my answer to reflect that too. – DerPhysiker Oct 10 '22 at 07:31
  • @WendyXiong here, updated. Please, let me know if that solved it. – DerPhysiker Oct 10 '22 at 07:37