Switch statements

I am in my first C++ class and I believe I have the switch statement about right but I keep getting an error.
The error is [Error] expected unqualified-id before 'switch'

Thanks in advance


switch (x) {
case 1:
case 2:
case 3:
cout << "x is 1, 2 or 3";
break;
default:
cout << "x is not 1, 2 nor 3";
break;
}
Syntax looks fine.

What's on the line before the switch?

https://ideone.com/gDmhRe
The line before the switch is
int main()
So you're possibly missing a brace?

It's maybe best if you post the code.
Here is the whole thing.

//Eric Gilliam
// CS104
// 4-13-2016
#include <iostream>
using namespace std;








int myArray [] = {11, 9, 63, 27, 85};// my Array
int n, result=0;

int addition (int a, int b)
{
int r;
r=a+b;
return r;
}
int main()

switch (x) {
case 1:
case 2:
case 3:
cout << "x is 1, 2 or 3";
break;
default:
cout << "x is not 1, 2 nor 3";
break;

{

for ( n=0 ; n<5 ; ++n )
{
result += myArray[n];
}
int z;
z = addition (5,3);
cout << "The result is " << z;

double den, num, result;
char op;
do{
cout << "\nCalculator - Please enter a number :";
cin >> num;
cout << "Please enter a what kind of math +, -, *, /";
cin >> op;

cout << "Please enter second number :";

cin >> den;
if (op=='+') result=num+den;
// How the calculator calculates
if (op=='-') result=num-den;
if (op=='*') result=num*den;
if (op=='/') result=num/den;
cout<< result;
}
while (op!='e');
cout<<"HELLO WORLD MY NAME IS ERIC! "<< endl;







return 0;
}
Yeah, so you're missing a brace right after main.

And you haven't declared/defined x anywhere.
Thank You for your Help
I thought I was done but when I fixed what I had wrong this is what I got.

line: 77 col: 10 Sel: 0 Lines: 80 Length: 1078 insert Done parsing in 0.36 seconds

//Eric Gilliam
// CS104
// 4-13-2016
#include <iostream>
using namespace std;








int myArray [] = {11, 9, 63, 27, 85};// my Array
int n, result=0;

int addition (int a, int b)
{
int r;
r=a+b;
return r;
}
int main(){
int t;
switch (t) {
case 1:
case 2:
case 3:
cout << "t is 1, 2 or 3";
break;
default:
cout << "t is not 1, 2 nor 3";
break;


}




{

for ( n=0 ; n<5 ; ++n )
{
result += myArray[n];
}
int z;
z = addition (5,3);
cout << "The result is " << z;

double den, num, result;
char op;
do{
cout << "\nCalculator - Please enter a number :";
cin >> num;
cout << "Please enter a what kind of math +, -, *, /";
cin >> op;

cout << "Please enter second number :";

cin >> den;
if (op=='+') result=num+den;
// How the calculator calculates
if (op=='-') result=num-den;
if (op=='*') result=num*den;
if (op=='/') result=num/den;
cout<< result;
}
while (op!='e');
cout<<"HELLO WORLD MY NAME IS ERIC! "<< endl;






return 0;
}



Topic archived. No new replies allowed.