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.