CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 23 of 23
  1. #16
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Changing function based on a condition?

    Actually, there may be an even better way to design the function because it's more "self-documenting":

    Code:
    		studio = getData("studio");
    		oneBed = getData("one-bedroom");
    		twoBed = getData("two-bedroom");
    Code:
    int getData(const std::string &apttype)
    {
            int num;
            cout << "How many " << apttype << " apartments?: ";
            cin >> num;
            return num;
    }
    Again we could have used a const char* rather than a std::string const reference, but in this case it isn't *quite* the same-----this way allows you to pass either a char* in or a string object.

  2. #17
    Join Date
    Nov 2010
    Posts
    81

    Re: Changing function based on a condition?

    Do I need to include a different header file? I tried to test it your way and it won't work... it underlines the << after "How many " and says no operator matches these operands. I excluded assert, as we haven't learned that yet - but it didn't seem to make a difference either way...

    Code:
    int getData(int bar)
    {
            const string foo[3] = {"studio", "one-bedroom", "two-bedroom"};
            int num;
            cout << "How many " << foo[bar] << " apartments?: ";
            cin >> num;
            return num;
    }

  3. #18
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Changing function based on a condition?

    You should #include <iostream>, #include <string>, and #include <cassert> for assert.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #19
    Join Date
    Nov 2010
    Posts
    81

    Re: Changing function based on a condition?

    I forgot about string... I'm stuck with all these simple exercise programs so when I finally write a decent size one for a project I forget stuff. Anyway - Here's my final program, incorporating what I learned here. Anybody feel free to rip it apart - just remember to explain because that's how I learn!

    Code:
    /***************************************\
    |	ACME Planning Program		|
    |	Written by: Ben ********	|
    |	v1.3 Last mod: 02/22/11		|
    \***************************************/
    
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    //--FUNCTION PROTOTYPES--//
    char run_program();
    int getData(int);
    void studioInfo();
    void oneInfo();
    void twoInfo();
    int spaceCalc();
    int costCalc();
    int incCalc();
    void results(int, int, int, int, int, int, int, int, int, int, 
    			int, int, int, int, int);
    char rerun_program(char);
    
    
    //--VARIABLES--//
    int studio, oneBed, twoBed;
    const int studioSq = 200; const int oneSq = 300; const int twoSq = 450;
    int studioSize, oneSize, twoSize;
    const int cost = 75;
    int studioCost, oneCost, twoCost;
    int studioInc, oneInc, twoInc;
    const int studioProf = 450;	const int oneProf = 550; const int twoProf = 700;
    int spaceTotal, costTotal, incTotal;
    
    int main()
    {
    
    	cout	<< "This program calculates income totals for" << endl
    			<< "ACME Real Estate Company." << endl;
    
    	char rerun = run_program();
    
    	while (rerun == 'y' || rerun == 'Y')
    	{
    		
    		//--GETTING DATA FROM USER--//
    		studio = getData(0);
    		oneBed = getData(1);
    		twoBed = getData(2);
    
    		//--PROCESSING DATA--//
    		studioInfo();
    		oneInfo();
    		twoInfo();
    
    		//--CALCULATING RESULTS--//
    		spaceTotal = spaceCalc();
    		costTotal = costCalc();
    		incTotal = incCalc();
    
    		//--PRINT RESULTS--//
    		results(studio, oneBed, twoBed, studioSize, studioCost, studioInc,
    			oneSize, oneCost, oneInc, twoSize, twoCost, twoInc,
    			spaceTotal, costTotal, incTotal);
    
    		//--RERUN--//
    		rerun = rerun_program(rerun);
    	}
    	cout	<< "THANK YOU!" << endl << endl;
    	return 0;
    }
    
    //--FUNCTION DEFINITIONS--//
    //************************//
    
    //--Run Program--//
    char run_program()
    {
    	char rerun;
    	int valid = 0;
    	while (valid == 0)
    	{
    		cout	<< "Would you like to run this program (y/n)?: ";
    		cin		>> rerun;
    
    		//validate input
    		if (rerun == 'y' || rerun == 'Y' || rerun == 'n' || rerun == 'N')
    			valid = 1;
    	}
    	system("cls");
    
    	return rerun;
    }
    
    //--Getting data from user--//
    int getData(int bar)
    {
            const char* foo[3] = {"studio", "one-bedroom", "two-bedroom"};
            int num;
            cout << "How many " << foo[bar] << " apartments?: ";
            cin >> num;
            return num;
    }
    
    //--Processing data--//
    void studioInfo()
    {
    	studioSize = studio*studioSq;
    	studioCost = studioSize*cost;
    	studioInc = (studio*studioProf)*12;
    }
    void oneInfo()
    {
    	oneSize = oneBed*oneSq;
    	oneCost = oneSize*cost;
    	oneInc = (oneBed*oneProf)*12;
    }
    void twoInfo()
    {
    	twoSize = twoBed*twoSq;
    	twoCost = twoSize*cost;
    	twoInc = (twoBed*twoProf)*12;
    }
    
    //--Calculations--//
    int spaceCalc()
    {
    	int spaceTotal = studioSize+oneSize+twoSize;
    	return spaceTotal;
    }
    int costCalc()
    {
    	int costTotal = studioCost+oneCost+twoCost;
    	return costTotal;
    }
    int incCalc()
    {
    	int incTotal = studioInc+oneInc+twoInc;
    	return incTotal;
    }
    
    //--Print Results--//
    void results(int studio, int oneBed, int twoBed, int studioSize, int studioCost, int studioInc,
    			int oneSize, int oneCost, int oneInc, int twoSize, int twoCost, int twoInc,
    			int spaceTotal, int costTotal, int incTotal)
    {
    	cout	<< "Report for " << studio << " apartments" << endl
    			<< oneBed << " one-bedroom apartments" << endl
    			<< twoBed << " two-bedroom apartments" << endl
    			<< "\t\tSpace\t\t Costs\t\tIncome" << endl
    			<< "     Studios:\t " << studioSize << "\t\t " 
    				<< studioCost << "\t\t " << studioInc << endl
    			<< "One-Bedrooms:\t " << oneSize << "\t\t" 
    				<< oneCost << "\t\t " << oneInc << endl
    			<< "Two-Bedrooms:\t " << twoSize << "\t\t" 
    				<< twoCost << "\t\t " << twoInc << endl
    			<< "---------------------\t\t------\t\t------" << endl
    			<< " Total Space:\t " << spaceTotal << "     Cost:\t" 
    			<< costTotal << "\t\t" << incTotal << endl;
    }
    
    //--Re-run Program--//
    char rerun_program(char rerun)
    {
    	int valid = 0;
    	while (valid == 0)
    	{
    		cout	<< "Do you want to enter another set of data (y/n)? ";
    		cin		>> rerun;
    
    		//validate input
    		if (rerun == 'y' || rerun == 'Y' || rerun == 'n' || rerun == 'N')
    			valid = 1;
    	}
    	system("cls");
    
    	return rerun;
    }

  5. #20
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Changing function based on a condition?

    Couple of things.

    Don't put multiple statements on a single line, even variable declarations. Readability is very important.

    In your function prototypes, give your arguments names. It makes the functions self-documenting and if you're using and IDE with an intellisense feature, it will be much more user friendly.

    In C++, 0 indicates a false condition, anything else true. So code such as
    Code:
    	int valid = 0;
    	while (valid == 0)
    is counterintuitive. Better style would be
    Code:
    	bool valid = true;
    	while (valid)

  6. #21
    Join Date
    Jan 2001
    Posts
    253

    Re: Changing function based on a condition?

    Quote Originally Posted by GCDEF View Post
    Better style would be
    Code:
    	bool valid = true;
    	while (valid)
    I think to get the same meaning as the original, better style would be
    Code:
    	bool valid = false;
    	while (! valid)
    (and then set valid = true in when the string entered is valid).

    Best regards,
    John

  7. #22
    Join Date
    Nov 2010
    Posts
    81

    Re: Changing function based on a condition?

    Quote Originally Posted by GCDEF View Post
    Couple of things.

    Don't put multiple statements on a single line, even variable declarations. Readability is very important.

    In your function prototypes, give your arguments names. It makes the functions self-documenting and if you're using and IDE with an intellisense feature, it will be much more user friendly.

    In C++, 0 indicates a false condition, anything else true. So code such as
    Code:
    	int valid = 0;
    	while (valid == 0)
    is counterintuitive. Better style would be
    Code:
    	bool valid = true;
    	while (valid)
    Can you give me an example of naming the function prototype? I don't know if you mean to use a comment or something... I name my functions based on what they do in this program, such as "getData" or "results."

  8. #23
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Changing function based on a condition?

    A function prototype introduces a function to the compiler but doesn't yet give an implementation. This allows the compiler to know which and in what order to push arguments onto the stack and the return type so that it can decide if the return should be stack or register based. This means in essence that you can declare a function in 1 file of your project and define it in another and call it from the file its just been declared in, or for a single file you can call the function before the compiler has seen the implementation, ie...
    Code:
    int Add( int num1, int num2 ); // this could be simply int Add( int, int ) but adding names self-documents better
    
    int main()
    {
       int a = 3;
       int b = 5;
       int c = Add( a,b ); // the compiler here knows the return type is an int and the arguments are ints. It doesn't need to know what add does
       return 0;
    }
    
    // we can now define the function after its been called because we introduced it to the compiler. This bit could even be in a different file.
    int Add( int num1, int num2 )
    {
       return num1 + num2;
    }
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

Page 2 of 2 FirstFirst 12

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured