Switch Statement

Hello everyone,

Im a new to C++ and trying to do this project for school, but i am getting this error: Case label 'Area_Rect' not within switch statement

If anyone has any ideas would be helpful!

Ive been starting at this code for over an hour trying to figure this out

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{


//Variables
int Radius, Leng, Width, Base, Height, Choice;

//Menu Choices
const int Area_Circle = 1,
Area_Rect = 2,
Area_Trian = 3,
Quit_Choice = 4;

cout << fixed << showpoint << setprecision(2);

do
{
//Menu
cout << "Geometry Calculator\n";
cout << "\t\t1. Calculate the area of a Circle\n";
cout << "\t\t2. Calculate the area of a Rectangle\n";
cout << "\t\t3. Calculate the area of a Triangle\n";
cout << "\t\t4. Quit\n";
cout << "Enter your choice (1-4): ";

while (Choice < Area_Circle || Choice > Quit_Choice)

{
cout << "Please enter a valid menu choice: ";
cin >> Choice;
}

if (Choice != Quit_Choice)

{

switch (Choice)

case Area_Circle:
cout << "Enter your radius: " << endl;
cin >> Radius;
cout << "The area of your circle with radius: " << Radius << "is: " << 3.14159 * pow(Radius, 2.0);
break;

case Area_Rect:
cout << "Enter the length: ";
cin >> Leng;
cout << "Enter the width: ";
cin >> Width;
cout << "The area of your rectangle with length: " << Leng << "and width: " << Width << "is: " << Leng * Width;
break;

case Area_Trian:
cout << "Enter base: ";
cin >> Base;
cout << "Enter height: ";
cin >> Height;
cout << "The area of your triangle with base: " << Base << "and height: " << Height << "is : " << Base * Height * .5;



}
} while (Choice != Quit_Choice);
return 0;

}
First of all, you really should be using code tags (highlight code and press <> button), secondly, switch statements use curly braces, put an opening one after switch(Choice) and a closing one after the final part of your final case statement.
Switch syntax:
1
2
3
switch(<switch_statement>) {
    <case_label>: <statements>
}
Last edited on
Oh wow! I didnt realize the bracket was before switch

Thanks for the help!
Topic archived. No new replies allowed.