please help me :(..

So I'm trying to write this simple program. What its supposed to do ask the user for input, the calculations of finding price of square foot, and then output to the screen the house that is the cheapest / most cost effective and the price. *****When I ran it through Visual Studios there wasn't any errors but it just asked for the input and shut down; no calculations or anything. I'm keeping it open with Cntrl f5 so I know its not auto shutting...*** Could anyone help me with this/ point me in the right direction?

I'm guessing its the if/ else statements, but i don't really know.

THanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  #include <iostream>
using namespace std;
//declare variables 
int colonialbaseprice;
int splitentrybaseprice;
int singlestorybaseprice;
int colonialsqft;
int splitentrysqft;
int singlestorysqft;
double colonialcostpersqft;
double splitentrycostpersqft;
double singlestorycostpersqft;


int main()
{
	cout << "Please enter the base price of the Colonial.";
	cin >> colonialbaseprice;
	cout << "Please enter the finished area in square feet of the Colonial.";
	cin >> colonialsqft;
	//calculate the cost per square foot of the colonial by dividing purchase price by sq ft
	colonialcostpersqft = colonialbaseprice / colonialsqft;

	cout << "Please enter the base price of the Split Entry";
	cin >> splitentrybaseprice;
	cout << "Please enter the finished area in square feet of the split entry";
	cin >> splitentrysqft;
	//calculate the cost per square foot of the colonial by dividing the purchase price by sq ft
	splitentrycostpersqft = splitentrybaseprice / splitentrysqft;

	cout << "Please enter the base price of the single story";
	cin >> singlestorybaseprice;
	cout << "Please enter the finished area in square feet of the single story";
	cin >> singlestorysqft;
	//calculate the cost per square foot of the single story by dividing the purchase price by sq ft
	singlestorycostpersqft = singlestorybaseprice / singlestorysqft;
	
	
	//if single story cost per sq feet is lower than split entry and colonial cost,
	if(colonialcostpersqft > splitentrycostpersqft > singlestorycostpersqft)
	{//display that its the cheapest and price
		cout << "Single Story is the cheapest. Price is ="; cout << singlestorycostpersqft;
		return 0;
	}
	//if colonial cost per sq ft is cheaper than single or split cost, 
	elseif  (splitentrycostpersqft > singlestorycostpersqft > colonialcostpersqft)
	{//display that its te cheapest and price 
		cout << "Colonial is the cheapest. Price is ="; cout << colonialcostpersqft;
		return 0;
	}
		//if split entry per sq ft is cheaper than single or colonial cost, 
	else (colonialcostpersqft > singlestorycostpersqft > splitentrycostpersqft)
	{//display that its the cheapest andprice
		cout << "Split entry is the cheapest. Price is ="; cout << splitentrycostpersqft;
		return 0;
	}
	return 0;
}
Last edited on
Your if statement conditions are incorrect:

if(colonialcostpersqft > splitentrycostpersqft > singlestorycostpersqft)
is read be the compiler as:
if((colonialcostpersqft > splitentrycostpersqft) > singlestorycostpersqft)
and so if colonialcostpersqft is greater than splitentrycostpersqft, you get this:
if(true > singlestorycostpersqft)
which is clearly not what you intended. Instead, you need to use && to link the tests:
if(colonialcostpersqft > splitentrycostpersqft && splitentrycostpersqft > singlestorycostpersqft)

Also, your program will output the information, but then it will immediately close because you are exiting main with your return 0 statements, before you have a chance to read the output.
Thank you so much for your help and replying!
I wanted to post a picture (i am extremely new to this site) and i was very sad that I could not.
:(


I fixed my lines of code as you demonstrated, but for some reason its not doing the calculations. I know its not just auto closing because I run it with the Ctrl f5 trick. Its just asking for input and then closing. Do you have any insight into that problem ? :(
Integer division returns integer and the remainder is discarded. Force floating-point division with a cast:
1
2
3
int foo = 7;
int bar = 42;
auto gaz = static_cast<double>( foo ) / bar;

You should not use return 0; statements in each if block, as the else blocks will be skipped if a condition is true, resulting in the final return statement being executed anyway at the end.

Your final else isn't really valid, as it still checks a condition. You want the final else to capture anything outside of the other options.

Lets presume this worked as intended, what would happen if you entered 3 equivalent values for all 3 types?
Last edited on
Topic archived. No new replies allowed.