Home > Enterprise >  Having trouble figuring out how to declare these functions in main
Having trouble figuring out how to declare these functions in main

Time:03-06

I'm having a tough time with this part of my code and it keeps giving me an error. I'm not sure what I am supposed to do here though, because I thought it was already declared? Edit: I've added the output to end of this question, thank you.

int main() {
    // Write code here . . .
     Apartment lakeside;

    lakeside.setStreetAddress(streetAddress);
    lakeside.setSquareFootage(squareFootage);
    lakeside.setTaxes(taxes);
    lakeside.setMonthlyRent(rent);

    displayPropertyInfo(lakeside);
    displayApartmentInfo(lakeside);
    
    return 0;

This is the error:

main.cpp: In function ‘int main()’:
main.cpp:90:31: error: ‘streetAddress’ was not declared in this scope
   90 |     lakeside.setStreetAddress(streetAddress);
      |                               ^~~~~~~~~~~~~
main.cpp:91:31: error: ‘squareFootage’ was not declared in this scope
   91 |     lakeside.setSquareFootage(squareFootage);
      |                               ^~~~~~~~~~~~~
main.cpp:92:23: error: ‘taxes’ was not declared in this scope
   92 |     lakeside.setTaxes(taxes);
      |                       ^~~~~
main.cpp:93:29: error: ‘rent’ was not declared in this scope
   93 |     lakeside.setMonthlyRent(rent);
      |                             ^~~~

This is the full code:

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

// Write the code for class RealProperty here . . .

class RealProperty
{
private:
    string streetAddress; // stores address of the street
    int squareFootage;    // stores area of square feet
    double taxes;         // stores taxes
public:
    RealProperty() 
        : streetAddress(""), squareFootage(0), taxes(0.0)
    {
    }
    RealProperty(string address, int sqft, double tax) 
        : streetAddress(address), squareFootage(sqft), taxes(tax)
    {
    }
    
    // mutator member functions
    void setStreetAddress(string address); // function to set streetAddress
    void setSquareFootage(int sqft);       // function to set squareFootage
    void setTaxes(double tax);             // function to set taxes
    // accessor functions
    string getStreetAddress() const
    {
        return streetAddress; // function to return streetAddress
    }
    int getSquareFootage() const
    {
        return squareFootage; // function to return squareFootage
    }
    double getTaxes() const
    {
        return taxes; // function to return taxes
    }
};

// function to set the streetAddress
void RealProperty::setStreetAddress(string address)
{
    streetAddress = address;
}
// function to set the squareFootage
void RealProperty::setSquareFootage(int sqft)
{
    if (sqft < 0)
    {
        cout << "Square footage must be a positive number." << endl;
    }
    squareFootage = sqft;
}
// function to set taxes
void RealProperty::setTaxes(double tax)
{
    taxes = tax;
}

// Apartment class
class Apartment : public RealProperty
{
private:
    double monthlyRent; // store the monthly rent
public:
    Apartment() : RealProperty() {}
    Apartment(string address, int sqft, double tax, double rent) : RealProperty(address, sqft, tax), monthlyRent(rent) {}
    // mutator function
    void setMonthlyRent(double rent); // function to set monthlyRent
    // accessor function
    double getMonthlyRent() const
    {
        return monthlyRent;
    }
};

// function to set monthlyRent
void Apartment::setMonthlyRent(double rent)
{
    monthlyRent = rent;
}

// Prototypes
void displayPropertyInfo(const RealProperty &rp); // Print the real property information.
void displayApartmentInfo(const Apartment &apt);  // Print the apartment information.

int main()
{
    // Write code here . . .
    Apartment lakeside;

    lakeside.setStreetAddress(streetAddress);
    lakeside.setSquareFootage(squareFootage);
    lakeside.setTaxes(taxes);
    lakeside.setMonthlyRent(rent);

    displayPropertyInfo(lakeside);
    displayApartmentInfo(lakeside);

    return 0;
}

//**********************************************************************
//* Print the real property information.
//*
//* Parameter
//*     rp - a reference to const referencing to caller's RealProperty
//*          variable.
//*
//* Return
//*     void
//**********************************************************************
void displayPropertyInfo(const RealProperty &rp)
{
    // Write your code here . . .
    cout << "Property is located at: " << rp.getStreetAddress() << endl;
    cout << "Square footage: " << rp.getSquareFootage() << endl;
    cout << "Taxes: " << fixed << setprecision(0) << rp.getTaxes() << endl
         << endl;
}

//**********************************************************************
//* Print the apartment information.
//*
//* Parameter
//*     rp - a reference to const referencing to caller's Apartment
//*          variable.
//*
//* Return
//*     void
//**********************************************************************
void displayApartmentInfo(const Apartment &apt)
{
    // Write your code here . . .
    cout << "Apartment is located at: " << apt.getStreetAddress() << endl;
    cout << "Square footage: " << apt.getSquareFootage() << endl;
    cout << "Taxes: " << fixed << setprecision(0) << apt.getTaxes() << endl;
    cout << "Monthly rent: " << fixed << setprecision(2) << apt.getMonthlyRent() << endl;
}

This is what the output is supposed to look like, but I can't get it to look like it (without the bulletpoints, wasn't sure how to indent in a list, sorry)?

  • Property is located at: Cupertino
  • Square footage: 1200
  • Taxes: 200
  • Apartment is located at: Cupertino
  • Square footage: 1200
  • Taxes: 200
  • Monthly rent: 2550.00

CodePudding user response:

You're supposed to replace the placeholders streetAddress, squareFootage, taxes and rent in

lakeside.setStreetAddress(streetAddress);
lakeside.setSquareFootage(squareFootage);
lakeside.setTaxes(taxes);
lakeside.setMonthlyRent(rent);

You can do this by either declaring variables with such names:

string streetAddress = "HelloWorld-ln. 4";
// ...
lakeside.setStreetAddress(streetAddress);

Or by directly replacing these values:

lakeside.setStreetAddress("HelloWorld-ln. 4");

Do this for all four of the placeholders.

CodePudding user response:

From what we can see, streetAddress is not a variable declared in the scope of main(), but a member of RealProperty class. The same goes to all other variables you are trying to pass when you call member functions from RealProperty. To fix this, just create variables with the values you wish inside the main() function scope or get the values from the user input.

  •  Tags:  
  • c
  • Related