Im struggling writing this code

Hey there I really don't know where I'm going wrong any help or hints would be very much appreciated.


#include<iostream>
#include<math.h>
using namespace std;

class Home
{
private:
double width;
double depth;
double floorSpace;
double squareFootage
double maxFloorSpace
double areaOfExpansion
public:
Home ();
Home (double,double,double);
void setWidth(int w) {width = w}
void setDepth(int d) {depth = d}
void setFloorSpace(int f) {floorSpace = f}
void calSquareFootage();
void calMaxFloorSpace();
void calAreaOfExpansion();
void display();


Home::Home()
{width = 0; depth = 0; floorSapce = 0}

Home::Home(double w, double d, double f)
{ width=w; depth=d; floorSpace=f; }

void Home::calSquareFootage()
{ squareFootage = width * depth }

void Home::calMaxFloorSpace()
{ maxFloorSpace = (width * depth) * .7}

void Home::calAreaOfExpansion()
{ areaOfExpansion = ((width * depth)* .7) - f}

void Home::display()
{
cout << "Square footage of your home is: " << squareFootage << "ft^2." << "\n";
cout << "Max floor space is : " << maxFloorSpace << "ft^2." << "\n";
cout << "The amount of square ft which you can expand your home is: " << areaOfExpansion << "ft^2" << "\n";
}



};
int main()
{
double w,d,f;
Home h;
cout << "Please enter the width of your land: ";
cin >> w;
while (w < 1) {
cout << "Please enter a positive number \n";
cin >> w;
}
h.setWidth(w);


cout << "Please enter the depth of your land: ";
cin >> d;
while (d < 1) {
cout << "Please enter a positive number \n";
cin >> d;
}
h.setDepth(d);


cout << "Please enter the floor space of your property: ";
cin >> f;
while (f < 1) {
cout << "Please enter a positive number \n";
cin >> f;
}
h.setFloorSpace(f);

h.calSquareFootage();
h.calMaxFloorSpace();
h.calAreaOfExpansion();
h.display();

return 0;
}
Last edited on
Hello noob12345,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

When I loaded your program into the IDE I first noticed that you are missing a semi-colon at the end of some lines.

The first step is to compile your program before posting it here and if there are any error messages you do not understand include them in your message.

When I fix the errors I will have a better idea of what is happening. I will know something shortly.

Hope that helps,

Andy
Awesome thanks, I will edit it and try to run it soon I wrote it on text edit because xcode won't let me run anything😑. Im going to school to work more on it over there I will post again after I try to get it running over there. Thanks again!

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
class Home
 {
 private:
 double width;
 double depth;
 double floorSpace;
 double squareFootage;
 double maxFloorSpace;
 double areaOfExpansion;
 public:
 Home ();
 Home (double,double,double);
 void setWidth(int w) {width = w;};
 void setDepth(int d) {depth = d;};
 void setFloorSpace(int f) {floorSpace = f;};
 void calSquareFootage();
 void calMaxFloorSpace();
 void calAreaOfExpansion();
 void display();


 Home::Home()
 {width = 0; depth = 0; floorSapce = 0};

 Home::Home(double w, double d, double f)
 { width=w; depth=d; floorSpace=f; };

 void Home::calSquareFootage()
 { squareFootage = width * depth }

 void Home::calMaxFloorSpace()
 { maxFloorSpace = (width * depth) * .7};

 void Home::calAreaOfExpansion()
 { areaOfExpansion = ((width * depth)* .7) - f};

 void Home::display()
 {
 cout << "Square footage of your home is: " << squareFootage << "ft^2." << "\n";
 cout << "Max floor space is : " << maxFloorSpace << "ft^2." << "\n";
 cout << "The amount of square ft which you can expand your home is: " << areaOfExpansion << "ft^2" << "\n";
 };



 };























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
#include<iostream>
 #include<math.h>
 using namespace std;
 #include<string>

 
 int main()
 {
 double w,d,f;
 Home.h;
 cout << "Please enter the width of your land: ";
 cin >> w;
 while (w < 1) {
 cout << "Please enter a positive number \n";
 cin >> w;
 }
 h.setWidth(w);


 cout << "Please enter the depth of your land: ";
 cin >> d;
 while (d < 1) {
 cout << "Please enter a positive number \n";
 cin >> d;
 }
 h.setDepth(d);


 cout << "Please enter the floor space of your property: ";
 cin >> f;
 while (f < 1) {
 cout << "Please enter a positive number \n";
 cin >> f;
 }
 h.setFloorSpace(f);

 h.calSquareFootage();
 h.calMaxFloorSpace();
 h.calAreaOfExpansion();
 h.display();

 return 0;
 }




ok I think I have fixed the issues in the above code that you have mentioned, I am now trying to run it and the main error codes I'm getting are

Error 1 error C2065: 'Home' : undeclared identifier
c:\users\skylib\documents\visual studio 2012\projects\project1\project1\source.cpp 11 1 Project1

Error 2 error C2228: left of '.h' must have class/struct/union c:\users\skylib\documents\visual studio 2012\projects\project1\project1\source.cpp 11 1 Project1


any help would be great.




Hello noob12345,

Without checking the new code what I first see is:

Lines 13 and 14 in the class code the semi-colon at the end of the line is not needed. Only the semi-colon inside the closing } is needed.

Line 10 in main should be Home h;. The dot is what is causing the error. A space is defining an object. The ".h" is trying to access in the class that does not exist and the ".h" would only work on an object of the class which you do not have yet. A good example is what you do at line 17.

In main line 39 you call the function h.calAreaOfExpansion();. In the function definition you use the variable "f", but it is never defined anywhere. I found that "f" needs to be passed to this function.

line 2 in main you should use "<cmath>" in place of "<math.h>".

Also you should learn not to use the line using namespace std; Someday in the future this WILL cause you a problem.

line 9 in main you should ALWAYS initialize your variables. double w{},d{},f{};. The empty {}s will initialize these variables to 0.0. Otherwise these variables would contain an unknown value. If used before they receive a usable number the results are unpredictable.

I will load up this version and see if there is anything else.

Hope that helps,

Andy
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
#ifndef Main_hpp
#define Main_hpp

#include <stdio.h>

#endif /* Main_hpp */
class Home
{
private:
    double width;
    double depth;
    double floorSpace;
    double squareFootage;
    double maxFloorSpace;
    double areaOfExpansion;
public:
    Home ();
    Home (double,double,double);
    void setWidth(int w) {width = w;}
    void setDepth(int d) {depth = d;}
    void setFloorSpace(int f) {floorSpace = f;};
    void calSquareFootage();
    void calMaxFloorSpace();
    void calAreaOfExpansion();
    void display();
};




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
#include "Home.hpp"
#include <iostream>
#include <cmath>
using namespace std;
Home::Home()
{width = 0; depth = 0; floorSpace = 0;};

Home::Home(double w, double d, double f)
{ width=w; depth=d; floorSpace=f; }

void Home::calSquareFootage()
{ squareFootage = width * depth; }

void Home::calMaxFloorSpace()
{ maxFloorSpace = (width * depth) * .7;};

void Home::calAreaOfExpansion()
{ areaOfExpansion = ((width * depth)* .7) - floorSpace;
    while ( areaOfExpansion < 1){
        cout << "Im sorry but you can not expand your floor space as your property already reaches the maximum floor space for County" << endl;
cin.get();
    }
};

void Home::display()
{
    cout << "Square footage of your home is: " << squareFootage << "ft^2." << "\n";
    cout << "Max floor space is : " << maxFloorSpace << "ft^2." << "\n";
    cout << "The amount of square ft which you can expand your home is: " << areaOfExpansion << "ft^2" << "\n";
};




First i just wanna say thanks again Andy you've been a huge help. Everything is running now but when the while loop is met in calAreaOfExpansion function it printss the message twice.
Last edited on
Hello noob12345,

Actually it prints more than twice. As a while loop where the value of "areaOfExpansion" never changes it becomes an endless loop.

Change "while" to "if" and it will work properly.

The rest of the code looks like it has not chnged, but I have not tested it yet.

Hope that helps,

Andy
Topic archived. No new replies allowed.