13 Replies - 2906 Views - Last Post: 04 October 2011 - 06:01 PM Rate Topic: -----

#1 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

how to work with a function that utilizes (bool = true) as a suffix?

Posted 01 October 2011 - 08:41 PM

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

Basically, the function "int invalidInputs (bool = true)", 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 enter a b c d, or, q.

Then the Main 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.

I have put in comment's my attempts at trying to do this. Which resulted in an error message from Visual Studio stating "'invalidInputs': identifier not found"

#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 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(totalCount) << " 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 = true)
{	
	int Count = 0;
	int totalCount;

	bool invalid;

	if (invalid == true){
		Count++;
		totalCount += Count;
	}
	else {
		totalCount += Count ;
	}
	
	return totalCount;
}  */


Is This A Good Question/Topic? 0
  • +

Replies To: how to work with a function that utilizes (bool = true) as a suffix?

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 844
  • View blog
  • Posts: 2,334
  • Joined: 20-August 07

Re: how to work with a function that utilizes (bool = true) as a suffix?

Posted 02 October 2011 - 12:22 AM

View Postmgrex, on 02 October 2011 - 04:41 AM, said:

Unfortunately the textbook I use, nor a Google search of, c++ "(bool = true)", doesn't show an example of the code in use.
Where did you find that particular bit of code? Code like that would only be used in defining default (a.k.a. optional) parameters for a function or member-function declaration.

However, typically you probably wouldn't write it exactly like that, since you'd usually want to give the argument a formal parameter name too (Although there's nothing technically wrong with omitting the name in the declaration, it's just unhelpful for someone reading your code)
e.g.
void foo(int bar = 1);

void foo(int bar)
{
    std::cout << bar << std::endl;
} 



Have a look at some of these pages for default function parameters and their explanation:
http://www.google.co...tion+parameters

This post has been edited by Bench: 02 October 2011 - 12:27 AM

Was This Post Helpful? 1
  • +
  • -

#3 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: how to work with a function that utilizes (bool = true) as a suffix?

Posted 02 October 2011 - 05:12 PM

The assignment instructions specifically stated, I need to atleast use a function precisely like that.

The function now declares 2 different variables.

int invalidInputs (bool invalid = true)
{	
	int Count = 0;
	int totalCount;

	if (invalid == true){
		Count++;
		return totalCount += Count;
	}
	else {
		return totalCount += Count ;
	}
}



My main challenge now, is trying to get to the Int Main to retrieve that function. I get the error message:
'invalidInputs' : function does not take 1 arguments

		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.
		}		


"bad" in the parentheses is declared in the int Main variable, as, int bad;

This post has been edited by mgrex: 02 October 2011 - 05:31 PM

Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 974
  • View blog
  • Posts: 3,390
  • Joined: 19-February 09

Re: how to work with a function that utilizes (bool = true) as a suffix?

Posted 02 October 2011 - 06:58 PM

What does your function prototype look like?
Was This Post Helpful? 0
  • +
  • -

#5 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: how to work with a function that utilizes (bool = true) as a suffix?

Posted 02 October 2011 - 07:16 PM

void mainMenu (char &);
double area (double, double);
double area (double r);
double volume (double, double, double);
double volume (double r);
int invalidInputs ();

Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 974
  • View blog
  • Posts: 3,390
  • Joined: 19-February 09

Re: how to work with a function that utilizes (bool = true) as a suffix?

Posted 02 October 2011 - 07:25 PM

The prototype needs to match the function or the compiler regards it is another function.

This should do :

int invalidInputs (bool invalid = true);


Was This Post Helpful? 0
  • +
  • -

#7 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: how to work with a function that utilizes (bool = true) as a suffix?

Posted 02 October 2011 - 09:23 PM

View Post#define, on 02 October 2011 - 07:25 PM, said:

The prototype needs to match the function or the compiler regards it is another function.

This should do :

int invalidInputs (bool invalid = true);



When I utilized
int invalidInputs (bool invalid = true);



for the function prototype, the program refused to run, and produced the errors:
forcing value to bool 'true' or 'false' (performance warning)
'invalidInputs' : redefinition of default parameter : parameter 1
see declaration of 'invalidInputs'

When I used
int invalidInputs (bool invalid);


For the function prototype instead, the program was able to execute, but produced an error stating: Run-Time check failure #3 - The variable 'bad' is being used without being initialized.

This happened right after I selected quit from the main menu.

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.

This post has been edited by mgrex: 02 October 2011 - 09:36 PM

Was This Post Helpful? 0
  • +
  • -

#8 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 844
  • View blog
  • Posts: 2,334
  • Joined: 20-August 07

Re: how to work with a function that utilizes (bool = true) as a suffix?

Posted 03 October 2011 - 12:13 PM

You need to put the default argument in the prototype (the forward declaration) and not in the function definition - look carefully at the example in my previous post for the difference between the declaration and definition.
Was This Post Helpful? 0
  • +
  • -

#9 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: how to work with a function that utilizes (bool = true) as a suffix?

Posted 04 October 2011 - 05:57 AM

View PostBench, on 03 October 2011 - 12:13 PM, said:

You need to put the default argument in the prototype (the forward declaration) and not in the function definition - look carefully at the example in my previous post for the difference between the declaration and definition.


I'm sorry, I'm not quite familiar with the terminologies my self.

For the function prototype using, int invalidInputs (bool invalid=true);

instead of, int invalidInputs (bool invalid);

results in the program refusing to execute. I also placed the function ahead of the int main, and the program was able to function the same way as the prototype, int invalidInputs (bool invalid);

for now i've made the following function for int invalidInputs (bool invalid = true)
{	
	static int Count = 0;
	
	if (invalid == true){
		Count++;
		return Count;
	}
	else {
		return Count;
	}
}


The program is able to execute and let the user quit without the program crashing. But it always returns 0 invalid errors, even when the main menu value are incorrectly entered.

Area of code that determines if user entered incorrect information.


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); //******CALLS int invalidInputs (bool invalid = true)

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;
}
}
Was This Post Helpful? 0
  • +
  • -

#10 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 844
  • View blog
  • Posts: 2,334
  • Joined: 20-August 07

Re: how to work with a function that utilizes (bool = true) as a suffix?

Posted 04 October 2011 - 09:20 AM

View Postmgrex, on 04 October 2011 - 01:57 PM, said:

View PostBench, on 03 October 2011 - 12:13 PM, said:

You need to put the default argument in the prototype (the forward declaration) and not in the function definition - look carefully at the example in my previous post for the difference between the declaration and definition.


I'm sorry, I'm not quite familiar with the terminologies my self.
The declaration is the line which declares the existance of the function at the top of the code and is just the signature on its own - it does not provide the body of the function (i.e. it's just the name)

Here's the example again - look at the difference between the declaration and the definition
//Declaration
void foo(int n = 1);

// Definition
void foo(int n)
{
} 
If you have put a declaration at the top of your code containing a default argument, then you cannot put the default argument (the n = 1) inside the definition. The only time when you would put a default argument inside a function definition is when there is no declaration.
Was This Post Helpful? 2
  • +
  • -

#11 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: how to work with a function that utilizes (bool = true) as a suffix?

Posted 04 October 2011 - 04:32 PM

I see, thanks for the heads up, the instructions indeed did say to utilize the declaration as a function prototype.

As a result of doing that, if use "int invalidInputs (bool invalid = true)", for the function, the program will refuse to run. Unfortunately though the main problem states there are no invalid errors. I had also added a Count++ right after the Count declaration, and program was indeed able to recognize it, and output there were 1 invalid output (all the time even when there were no invalid inputs).

int invalidInputs (bool invalid)
{	
	static int Count = 0;
	//Count++; //This proved, that the preceding block of code is always recognized.

	if (invalid == true){
		Count++;
		//invalid = false;
		//return totalCount += Count;
		return Count;
	}
	else {
		//return totalCount += Count ;
		return Count;
	}
}



I think the main culprit, is from the, void mainMenu (char &choose), function, but it's really out of my reach.
Was This Post Helpful? 0
  • +
  • -

#12 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 974
  • View blog
  • Posts: 3,390
  • Joined: 19-February 09

Re: how to work with a function that utilizes (bool = true) as a suffix?

Posted 04 October 2011 - 05:26 PM

You don't need int before invalidInputs (invalid);. The compiler probably interprets it as a function prototype.

while (choose != 'a' && choose != 'b' && choose != 'c' && choose != 'd' && choose != 'q')
{
invalid = true;
//int invalidInputs (invalid); 
invalidInputs (invalid); 


Was This Post Helpful? 1
  • +
  • -

#13 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: how to work with a function that utilizes (bool = true) as a suffix?

Posted 04 October 2011 - 05:52 PM

Thanks a million, that did the trick :)
Was This Post Helpful? 1
  • +
  • -

#14 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 974
  • View blog
  • Posts: 3,390
  • Joined: 19-February 09

Re: how to work with a function that utilizes (bool = true) as a suffix?

Posted 04 October 2011 - 06:01 PM

Great. :)

To call the function you could pass a true value.

invalidInputs (true); 



If the function has a default true value, you wouldn't need to pass a value. The true value would be passed automatically.

invalidInputs (); 



Unless you wanted a to pass a false value.

invalidInputs (false); 


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1