Return value in method from variable in main
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Return value in method from variable in main

  1. #1
    Join Date
    Feb 2006
    Posts
    5

    Return value in method from variable in main

    I'm trying to return a value from a method, simple enough. I am able to do it by declaring a variable in the class in which the method is in but I'm not suppose too. Here is part of the code and hopefully it will help with what I'm trying to do.

    Code:
    #include <iostream> 
    #include <string>   
    #include <iomanip>  
    using namespace std;
    class HouseCost //class
    {
    private:
    	double Length, Width; //declares variables for the area of each room 
    public:
    	HouseCost(); //default constructor
    	HouseCost(double len, double wid); //overloaded constructor
    	void print_name(); //get and print first and last name
    	void setLength(double len){Length = len;}
    	void setWidth(double wid){Width = wid;}
    	double getLength(){return Length;}
    	double getWidth(){return Width;}
    	void printHouseArea(); //prints the length and width of house rooms
    	double getFamArea(); //get the length and width of family room
    	double getKitArea(); //get the length and width of kitchen room
    	double getSunArea(); //get the length and width of sun room
    	double getMastBedArea(); //get the length and width of master bed room
    	double getGarageArea(); //get the length and width of garage
    	void printGarageArea(); //prints the length and width of garage rooms
    	double calcHouseArea(double, double, double, double); //calculates the area of all the rooms in the house
    	double calcHouseCost(double, double); //calculates cost of all rooms without garage
    	double calcGarageCost(); //calculates the cost of garage
    	void printGarageCost(); //prints garage cost
    };
    HouseCost::HouseCost()
    {
    	Length = 0;
    	Width = 0;
    }
    HouseCost::HouseCost(double len, double wid)
    {
    	Length = len;	
    	Width = wid;
    }
    void HouseCost::print_name() //gets first and last name and prints them
    {
    	string Fname, Lname; //declares variables for first and last name
    	cout << "\n\n\nPlease enter your first name: ";
    	cin >> Fname;
    	cout << "Please enter your last name: ";
    	cin >> Lname;
    	cout << "\n\nThank you " << Fname << " " << Lname << " for using my Analyzer.\n\n";
    }
    void HouseCost::printHouseArea()
    {
    	cout << "\nThe area of the room above is: " << fixed << setprecision(2) << getLength() * getWidth() << " Square Feet\n";
    }
    double HouseCost::getFamArea()
    {
    	cout << "\nPlease enter the family room length in feet only and hit return: "; //askes for length
    	cin >> Length; //gets input for length
    	cout << "Please enter the family room width in feet only and hit return: "; //askes for width
    	cin >> Width; //gets input for width
    	FamArea = Length * Width;
    	return FamArea;
    }
    double HouseCost::getKitArea()
    {
    	cout << "\nPlease enter the kitchen length in feet only and hit return: "; //askes for length
    	cin >> Length; //gets input for length
    	cout << "Please enter the kitchen width in feet only and hit return: "; //askes for width
    	cin >> Width; //gets input for width
    	KitArea = Length * Width;
    	return KitArea;
    }
    double HouseCost::getSunArea()
    {
    	cout << "\nPlease enter the sunroom length in feet only and hit return: "; //askes for length
    	cin >> Length; //gets input for length
    	cout << "Please enter the sunroom width in feet only and hit return: "; //askes for width
    	cin >> Width; //gets input for width
    	SunArea = Length * Width;
    	return SunArea;
    }
    double HouseCost::getMastBedArea()
    {
    	cout << "\nPlease enter the master sunroom length in feet only and hit return: "; //askes for length
    	cin >> Length; //gets input for length
    	cout << "Please enter the master sunroom width in feet only and hit return: "; //askes for width
    	cin >> Width; //gets input for width
    	MastArea = Length * Width;
    	return MastArea;
    }
    double HouseCost::getGarageArea()
    {
    	cout << "\nPlease enter the garage length in feet only and hit return: "; //askes for length
    	cin >> Length; //gets input for length
    	cout << "Please enter the garage width in feet only and hit return: "; //askes for width
    	cin >> Width; //gets input for width
    	GarArea = Length * Width;
    	return GarArea;
    }
    void HouseCost::printGarageArea()
    {
    	cout << "\n\nArea of the garage is: " << fixed << setprecision(2) << getLength() * getWidth() << " Square Feet\n";
    }
    double HouseCost::calcGarageCost()
    {					
    	garCst = GarArea * garCstSqFt;
    	return garCst;
    }
    double HouseCost::calcHouseArea(double FamArea, double KitArea, double SunArea, double MastArea)
    {
    	return (FamArea + KitArea + SunArea + MastArea);
    }
    double HouseCost::calcHouseCost(double RoomArea, double housCstSqFt)
    {
    	return (RoomArea * housCstSqFt);
    }
    void HouseCost::printGarageCost()
    {
    	cout << "The cost of the garage is $" << fixed << setprecision(2) << garCst;
    }                       
    int main() //main
    {
    	double housArea, garArea, housCst, garCst, FamArea, KitArea, SunArea, MastArea, RoomCost, totalArea;
    	const double garCstSqFt = 45.01;
    	const double housCstSqFt = 69.79;
    	
    	cout << "\t\tWelcome to Westbrook's Home Analyzer!\n\n";
    	
    	HouseCost houseAnalyze;
    	houseAnalyze.print_name();
    	houseAnalyze.getFamArea();
    	houseAnalyze.printHouseArea();
    	houseAnalyze.getKitArea();
    	houseAnalyze.printHouseArea();
    	houseAnalyze.getSunArea();
    	houseAnalyze.printHouseArea();
    	houseAnalyze.getMastBedArea();
    	houseAnalyze.printHouseArea();
    	houseAnalyze.getGarageArea();
    	
    	cout << "\n\n***************************************************";
    	
    	houseAnalyze.printGarageArea();
    	
    	totalArea = houseAnalyze.calcHouseArea(FamArea, KitArea, SunArea, MastArea);
        
    	cout << "\nThe total house area without garage is: " << totalArea << " Square Feet." << endl << endl;
    	
    	RoomCost = houseAnalyze.calcHouseCost(totalArea, housCstSqFt);
    
    	cout << "\nThe cost of the house without Garage is: " << RoomCost << endl;
    
    	houseAnalyze.calcGarageCost();
    	houseAnalyze.printGarageCost();
    	cout << houseAnalyze.getLength();
    
    	system("pause");
    	return EXIT_SUCCESS;
    } //end main
    Here are the errors

    error C2065: 'FamArea' : undeclared identifier
    error C3861: 'FamArea': identifier not found, even with argument-dependent lookup
    error C2065: 'KitArea' : undeclared identifier
    error C3861: 'KitArea': identifier not found, even with argument-dependent lookup
    error C2065: 'SunArea' : undeclared identifier
    error C3861: 'SunArea': identifier not found, even with argument-dependent lookup
    error C2065: 'MastArea' : undeclared identifier
    error C3861: 'MastArea': identifier not found, even with argument-dependent lookup
    error C2065: 'GarArea' : undeclared identifier
    error C3861: 'GarArea': identifier not found, even with argument-dependent lookup
    error C2065: 'garCst' : undeclared identifier
    error C2065: 'garCstSqFt' : undeclared identifier
    error C3861: 'GarArea': identifier not found, even with argument-dependent lookup
    error C3861: 'garCst': identifier not found, even with argument-dependent lookup
    error C2593: 'operator <<' is ambiguous

  2. #2
    Join Date
    Jul 1999
    Location
    Sth Australia, Australia
    Posts
    488

    Re: Return value in method from variable in main

    You are getting confused between the declaration of a class data members and the local variables declared in you main function.

    Ensure that the data members of your class are intialized properly when the respective class calculation functions are called. To get the total area have your class calculate it using the area member variables for the respective areas and then return the calculated total.

    I've provided only a hint, look at the declaration of the respective class members and how to access them from your main routine.

  3. #3
    Join Date
    May 2005
    Location
    Ellesmera
    Posts
    417

    Re: Return value in method from variable in main

    your local variables are not declared.

    // this is how i would designed my class.
    your input statement should be done on your main routine.
    your class member function should do the calculation by passing the values there.
    *** Con Tu Adios, Te Llevas, Mi Corazon***

    Traveling Encoder...

  4. #4
    Join Date
    Jan 2006
    Posts
    26

    Re: Return value in method from variable in main

    Yup sma right said, u have to look for the scope of the variables...

    For example in the method getFamArea() where have declared the variable
    FamArea in this method.....else u have use global variables.....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center