Thread: Call the value from a function that utilizes bool=true in it's parameter?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    2

    Call the value from a function that utilizes bool=true in it's parameter?

    Unfortunately the textbook I use, nor a Google search of, c++ "(bool = true)", doesn't show an example of the code in use.

    I am required to use a function with the following parameter:
    "int invalidInputs (bool = true)",

    This function must utilize it's own counter to keep track of the total amount of times, the user entered an invalid input at the main menu of the program (not entering a b c d, or, q).

    Then the int ain function must call "invalidInputs (bool = true)" function, to aid in the output of a final message to the user, that includes the the total amount of times that he/she attempted an invalid input at the main menu.

    what i have so far

    Code:
    #include <iostream>#include <cstring>
    #include <iomanip>
    
    
    using namespace std;
    
    
    const double PI = 3.14;
    
    
    void mainMenu (char &);
    double area (double, double);
    double area (double r);
    double volume (double, double, double);
    double volume (double r);
    int invalidInputs ();
    
    
    int main()   
    {
    	char choice;
    	int bad;
    	//int Count = 0;
    	//int totalCount;
    	//totalCount += Count;
    
    
    	double l, w, h, r;
    
    
    	do {
    
    
    		mainMenu(choice);
    
    
    		if (choice != 'q')
    		{
    			if (choice == 'a')
    			{
    				do {
    				cout << "Enter l: ";
    				cin >> l;
    				} while (l < 0 );
    
    
    				do {
    				cout << "Enter w: ";
    				cin >> w;
    				} while (w < 0);
    												
    				cout << "Area of rectangle: " << area(l, w) << endl;
    				cout << "" << endl;
    			}
    						
    			else if (choice == 'b')
    			{
    				do {
    				cout << "Enter r: ";
    				cin >> r;
    				} while (r < 0 );
    
    
    				//total = area(r);
    				cout << "Area of circle: " << area(r) << endl;
    				cout << "" << endl;
    			}
    
    
    			else if (choice == 'c')
    			{
    				do {
    				cout << "Enter l: ";
    				cin >> l;
    				} while (l < 0 );
    
    
    				do {
    				cout << "Enter w: ";
    				cin >> w;
    				} while (w < 0);
    
    
    				do {
    				cout << "Enter h: ";
    				cin >> h;
    				} while (h < 0);
    												
    				cout << "Area of box: " << volume(l, w, h) << endl;
    				cout << "" << endl;
    			}
    
    
    			else if (choice == 'd')
    			{
    				do {
    				cout << "Enter r: ";
    				cin >> r;
    				} while (r < 0 );
    
    
    				cout << "Area of sphere: " << volume(r) << endl;
    				cout << "" << endl;
    			}
    		}
    
    
    		else //when user quits
    		{
    			cout << "Have a good day. " << endl << " There were " << invalidInputs(bad) << " invalid inputs." << endl;
    			//cout << "Have a good day. " << endl;  //RUN without checking for total invalid inputs.
    		}		
    		} 
    	while (choice != 'q');
    
    
    	system ("pause");
    	return 0;
    }
    
    
    void mainMenu (char &choose) 
    {
    	int countError = 0;
    	bool invalid;
    
    
    	cout << "a to calculate area of a rectangle" << endl;
    	cout << "b to calculate area of a circle" << endl;
    	cout << "c to calculate volume of a box" << endl;
    	cout << "d to calculate volume of sphere" << endl;
    	cout << "q to quit" << endl;
    	cin >> choose;
    
    
    	while (choose != 'a' && choose != 'b' && choose != 'c' && choose != 'd' && choose != 'q')
    	{
    		invalid = true;
    		int invalidInputs (invalid);
    
    
    		cout << "Invalid choice" << endl;
    		cout << "";
    		cout << "press a to calculate area of a rectangle" << endl;
    		cout << "b to calculate area of a circle" << endl;
    		cout << "c to calculate volume of a box" << endl;
    		cout << "d to calculate volume of sphere" << endl;
    		cout << "q to quit" << endl;
    		cin >> choose;
    	}
    }
    
    
    double area (double l, double w) //Selection A
    {
    	return l * w;
    }
    
    
    double area (double r) //B
    {
    	return pow(PI * r, 2);
    }
    
    
    double volume (double l, double w, double h)  //C
    {
    	return l * w * h;
    }
    
    
    double volume (double r) //D
    {
    	return 4/3 * PI * pow(r, 3); 
    }
    
    
    
    
    int invalidInputs (bool invalid = true)
    {	
    	int Count = 0;
    	int totalCount;
    
    
    	if (invalid == true){
    		Count++;
    		invalid = false;
    		return totalCount += Count;
    	}
    	else {
    		return totalCount += Count ;
    	}
    }
    The lines
    Code:
    else //when user quits		{
    			cout << "Have a good day. " << endl << " There were " << invalidInputs(bad) << " invalid inputs." << endl;
    Produces a visual studio error message stating:

    'invalidInputs' : function does not take 1 arguments

    So, the main question is, how can this be overcome?

    If one has links showing examples of this kind of function parameter in use, that would be appreciated as well.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by lipxin
    This function must utilize it's own counter to keep track of the total amount of times
    A static local variable comes to mind.

    Quote Originally Posted by lipxin
    So, the main question is, how can this be overcome?
    Look at your forward declaration of invalidInputs:
    Code:
    int invalidInputs ();
    Clearly, invalidInputs is a function that takes no arguments and returns an int.

    Now look at the first line of your definition:
    Code:
    int invalidInputs (bool invalid = true)
    Clearly, invalidInputs is a function that takes an optional argument and returns an int. The parameter is of type bool, with a default argument of true.

    So, there is an inconsistency here. You cannot declare the default argument in both places, but you must consistently declare the parameter in both places.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    2
    After changing the function prototype from

    int [COLOR=#000000 !important]invalidInputs;[/COLOR]

    [COLOR=#000000 !important]to
    [/COLOR]
    int invalidInputs (bool invalid);


    The program was able to run, but when i decided to quit, it produced a Run time check failure.

    The variable 'bad' is being used without being initialized. So i decided to do, int bad = 0;, instead of just int bad;, and now it states:
    Run time check failure - the variable 'totalCount' is being used without being initialized.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Right, so keep on fixing. You are going in the right direction.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    double volume (double r) //D
    {
        return 4/3 * PI * pow(r, 3);
    }
    4/3 is integer division which results in value of 1 and not 1.333333. Test that part of your code and see that it does not produce the values you would expect.

    Also, you need to #include<cmath> as well here if you're using the pow function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. write a function that returns true
    By tingting in forum C Programming
    Replies: 8
    Last Post: 08-10-2009, 09:15 PM
  2. What is the true address of a function ptr?
    By Raigne in forum C++ Programming
    Replies: 8
    Last Post: 10-18-2008, 12:24 PM
  3. Writing a bool function
    By findme in forum C++ Programming
    Replies: 6
    Last Post: 11-29-2005, 11:31 PM
  4. bool function
    By MB1 in forum C++ Programming
    Replies: 10
    Last Post: 04-22-2005, 05:31 PM
  5. How do I use the char & true function?
    By Zopyrus in forum C++ Programming
    Replies: 5
    Last Post: 11-09-2003, 02:51 PM