I am getting error codes "Function definition is not allowed here" for very function beginning ath the void hopeChest() line 117 and beyond. What am I doing wrong ??
Goal is to have a MAIN menu based switch statement call different sub menus and their functions. Also need to return to MAIN menu after sub menu function is complete.
void areaShape()
{const float PI = 3.14159;
float area, radius, base, height, length, width;
int user_choice;
cout << " Geometry Calculator\n";
cout << " 1. Calculate the Area of a Circle\n";
cout << " 2. Calculate the Area of a Rectangle\n";
cout << " 3. Calculate the Area of a Triangle\n";
cout << " 4. Quit\n";
cout << " Enter you choice (1-4): ";
cin >> user_choice;
switch (user_choice)
{
case 1:
cout << "What is the radius: ";
cin >> radius;
if (radius < 0)
{
cout << "\nWe're sorry. The radius must be a positive number. ";
cout << "Rerun the program and try again.";
}
else
{
area = PI * pow(radius, 2);
cout << "The area of the circle is " << area;
}
break;
case 2:
cout << "What is the length? ";
cin >> length;
if (length > 0)
{
cout << "What is the width? ";
cin >> width;
if (width > 0)
{
area = length * width;
cout << "The area of rectangle is " << area;
}
else
{
cout << "\nWe're sorry. Width must be greater than 0.";
cout << "Rerun the program and try again.";
}
}
else
{
cout << "\nWe're sorry. Length must be greater than 0.\n";
cout << "Rerun the program and try again.";
}
break;
case 3:
cout << "What is the base? ";
cin >> base;
if (base > 0)
{
cout << "What is the height? ";
cin >> height;
if (height > 0)
{
area = (base * height) * .5;
cout << "Area of triangle is " << area;
}
else
{
cout << "\nWe're sorry. Height must be greater than 0.\n ";
cout << "Rerun the program and try again.";
}
}
else
{
cout << "\nWe're sorry. Base length must be greater than 0.\n";
cout << "Rerun the program and try again.";
}
break;
case 4:
cout << "Good-bye.";
break;
default:
cout << "\nWe're sorry. Your choice must be between 1 and 4.\n ";
cout << "Rerun the program and try again.";
break;
}
}
void hopeChest()
{
bool get_dimensions(int & l, int & w, int & h)
{
bool r = true;
cout << "\n Enter chest dimensions.\n The valid range is 20 to 48 inclusive.";
cout << "\n Enter legth of chest: ";
cin >> l;
if (l < 20 || l > 48)
r = false;
cout << "\n Enter width of chest: ";
cin >> w;
if (w < 20 || w > 48)
r = false;
cout << "\n Enter depth of chest: ";
cin >> h;
if (h < 20 || h > 48)
r = false;
return r;
}
// Extra charge of $50 if volume is above 12000 inches.
int calc_Volume(int l, int w, int h, long & v)
{
v = l * w * h;
if (v > 12000)
return 50;
else
return 0;
}
// Option 1. Premium Oak with or without additional cedar lining.
int calc_Charge(int basePrice, int volCharge, char & cedar, int primeCharge = 0)
{
cout << "\n Do you want the custom (Cedar) lining?(y/n) ";
cin >> cedar;
if (cedar == 'y' || cedar == 'Y')
return basePrice + volCharge + primeCharge + 75;
return basePrice + volCharge + primeCharge;
}
//Option 2. Base chest with no additions.
int calc_Charge(int basePrice, int volCharge)
{
return basePrice + volCharge;
}
//Ask Which type of chest and cedar lining(y/n) customer wants. List out What Chest Type and charge, Volume of Chest and charge, Cedar Lining(y/n) and charge, and Final Total Price.
int mainHope()
{
int length, width, depth, totalPrice;
bool isValid = get_dimensions(length, width, depth);
if (!isValid)
{
cout << "\n\nInvalid dimension given. Program terminated.";
exit(1);
}
long volume;
int charge = calc_Volume(length, width, depth, volume);
char premium = 'n', cedar = 'n';
cout << "\n Do you want a Premium Chest?(y/n) ";
cin >> premium;
if (premium == 'y' || premium == 'Y')
{
totalPrice = calc_Charge(150, charge, cedar, 100);
bool isCedar = cedar == 'y' || cedar == 'Y';
cout << "\n Chest Type: Premium Hope Chest";
cout << "\n Volume of the Chest: " << volume;
cout << "\n Cedar Lining:";
if (isCedar)
cout << "Yes ";
else
cout << "No" << endl;
cout << "\n\n Price of the Chest: $" << 250;
cout << "\n Cubic Volume Charge Incurred: $";
if (volume > 12000)
cout << 50;
else
cout << 0;
if (isCedar)
cout << "\n Cedar Lining Cost: $" << 75;
cout << "\n Total Cost: $" << totalPrice;
//Function that displays main menu
int mainMenu()
{
int choice;
cout << "\n\n \t Main Menu \n ";
cout << "\n 1. Calculate Area of Shapes \n 2. Calculate Hope Chest Cost \n 3. Search and Sort Arrays \n 4. Password Validation \n 5. Exit \n Your Option(Please Enter a number 1-5): ";
cin >> choice;
return choice;
}
//Main function
int main()
{
int mainOption;
mainOption = mainMenu();
switch(mainOption)
{
case 1: areaShape(); break;
case 2: hopeChest(); break;
case 3: cout << " Do you wish to exit the Program ? "
default: cout << "\n\n Invalid Choice. Please enter a number between 1-5. \n"; break;
}
void hopeChest()
{
bool get_dimensions(int & l, int & w, int & h)
{
also, please use code tags going forward, <> on the edit bar. Sometimes they are less obvious on the first new post of a new topic, but its there somewhere.
Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
In Main" case 3 you have case 3: cout << " Do you wish to exit the Program ? ". Consider, for now writing it like this:
1 2
case 3:
cout << " Do you wish to exit the Program ? "
This makes it easier to see that the line does not end in a semi-colon and there is no "break" statement. So case 3 falls through to the "default" before it finds a "break" and leaves the switch.
In function "mainHope" you use "exit(1)". Not a good choice. "exit()" is a C function and does not know about classes and other C++ features, so somethings will not close down properly before you make an unconditional exit. Better to "return 1" and deal with it there to leave the program properly. You will also need to end the function with "return 0;" to make the compiler happy.
As an example you could do something like:
1 2 3 4
int response{};
if(response = mainHope())
return response;
This way you would leave the program properly. In the beginning it is best to keep it simple until you understand it better. Also if you want to make the "case" statements 1 line you can adjust that later.
After that as jonnin said you will need to take all those functions out of "hopeChest" and call them when needed.
As is, even if it would compile, calling "hopeChest" would not automatically run the functions it contains.