Case statement

D

deanfamily11

I've set up a case statement to have my program determine where on the
Cartesian plane a point the user enters is located. I keep getting the
C2051 error when I compile. Any help?


#include <iomanip>
#include <iostream>

using namespace std;

int main()
{

//declare variables
int var1, var2;

//get the points
cout << "Enter 2 numbers that are the coordinates on a Cartesian plane: ";
cin >> var1 >> var2;
cout << endl << endl;

//use a case statment to determine the plane
switch (var1 & var2)
{
case (var1 == 0) && (var2 == 0): cout << "(" << var1 << "," << var2 <<
")"
<< " is on the origin." << endl;
break;
case ((var1 >= 1) || (var1 <= -1)) && (var2 == 0):
cout << "(" << var1 << "," << var2 << ")" << "is on the x axis." <<
endl;
break;
case (var1 == 0) && ((var2 >= 1) || (var2 <= -1)):
cout << "(" << var1 << "," << var2 << ")" << "is on the y axis." <<
endl;
break;
case (var1 >= 1) && (var2 >= 1): cout << "(" << var1 << "," << var2 <<
")"
<< " is in the first quadrant." << endl;
break;
case (var1 <= -1) && (var2 >= 1): cout << "(" << var1 << "," << var2 <<
")"
<< " is in the second quadrant." << endl;
break;
case (var1 <= -1) && (var2 <= -1):cout << "(" << var1 << "," << var2 <<
")"
<< " is in the third quadrant." << endl;
break;
case (var1 >= 1) && (var2 <= -1): cout << "(" << var1 << "," << var2 <<
")"
<< " is in the fourth quadrant." << endl;
break;
}

return 0;
}
 
A

Artie Gold

deanfamily11 said:
I've set up a case statement to have my program determine where on the
Cartesian plane a point the user enters is located. I keep getting the
C2051 error when I compile. Any help?

An `error number' is specific to your compiler -- and meaningless in
this context.
#include <iomanip>
#include <iostream>

using namespace std;

int main()
{

//declare variables
int var1, var2;

//get the points
cout << "Enter 2 numbers that are the coordinates on a Cartesian plane: ";
cin >> var1 >> var2;
cout << endl << endl;

//use a case statment to determine the plane
switch (var1 & var2)

Somehow, I suspect that you don't want the bitwise `and' of the
variables that were input.
{
case (var1 == 0) && (var2 == 0): cout << "(" << var1 << "," << var2 <<
")"
Case labels must be constants.

*Please* get an appropriate book (see http://www.accu.org for
possibilities) -- and study. You, as of yet, have no clue as to the
syntax of C++.

HTH and Cheers,
--ag
 
G

GB

deanfamily11 said:
I've set up a case statement to have my program determine where on the
Cartesian plane a point the user enters is located. I keep getting the
C2051 error when I compile. Any help?

Error codes are compiler-specific, so in the future you should indicate
the actual error message, not the code. One problem you have is that you
are using runtime expressions in your case statements. Each case in a
switch statement must contain a distinct compile-time constant. You
cannot use expressions that are evaluated at runtime.
switch (var1 & var2)

This is technically okay, but I doubt you want to use the & operator.
{
case (var1 == 0) && (var2 == 0): cout << "(" << var1 << "," << var2 <<

This is not okay. You will need to use a nested if statement to do this.

Gregg
 
D

deanfamily11

Well, then the text of the error is "case expression not constant" and it
occurs on every line of the case statement.

Artie Gold said:
deanfamily11 said:
I've set up a case statement to have my program determine where on the
Cartesian plane a point the user enters is located. I keep getting the
C2051 error when I compile. Any help?

An `error number' is specific to your compiler -- and meaningless in this
context.
#include <iomanip>
#include <iostream>

using namespace std;

int main()
{

//declare variables
int var1, var2;

//get the points
cout << "Enter 2 numbers that are the coordinates on a Cartesian plane:
";
cin >> var1 >> var2;
cout << endl << endl;

//use a case statment to determine the plane
switch (var1 & var2)

Somehow, I suspect that you don't want the bitwise `and' of the variables
that were input.
{
case (var1 == 0) && (var2 == 0): cout << "(" << var1 << "," << var2
<< ")"
Case labels must be constants.

*Please* get an appropriate book (see http://www.accu.org for
possibilities) -- and study. You, as of yet, have no clue as to the syntax
of C++.

HTH and Cheers,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
 
M

M

I've set up a case statement to have my program determine where on the
Cartesian plane a point the user enters is located. I keep getting the
C2051 error when I compile. Any help?
//use a case statment to determine the plane
switch (var1 & var2)
{
case (var1 == 0) && (var2 == 0): cout << "(" << var1 << "," << var2 <<

You can't do that...

The expressions in a case statement must evaluate to a constant
integer at compile time.

The switch expression will compile, but will not work as you expect,
because it will also be evaluated as an integer, but at run time.
 
J

John Harrison

deanfamily11 said:
Well, then the text of the error is "case expression not constant" and it
occurs on every line of the case statement.

Well exactly

case 1:
case 2:
case 3:
case 4:

these are OK, the case expressions are constants

case (var1 == 0) && (var2 == 0):

this is not OK, the case expression is not a constant.

You want an if statement not a case statement

if ((var1 == 0) && (var2 == 0))
{
...
}
else if (((var1 >= 1) || (var1 <= -1)) && (var2 == 0))
{
...
}

etc.

john
 
J

Jim Langston

deanfamily11 said:
I've set up a case statement to have my program determine where on the
Cartesian plane a point the user enters is located. I keep getting the
C2051 error when I compile. Any help?


#include <iomanip>
#include <iostream>

using namespace std;

int main()
{

//declare variables
int var1, var2;

//get the points
cout << "Enter 2 numbers that are the coordinates on a Cartesian plane:
";
cin >> var1 >> var2;
cout << endl << endl;

//use a case statment to determine the plane
switch (var1 & var2)
{
case (var1 == 0) && (var2 == 0): cout << "(" << var1 << "," << var2 <<
")"
<< " is on the origin." << endl;
break;
case ((var1 >= 1) || (var1 <= -1)) && (var2 == 0):
cout << "(" << var1 << "," << var2 << ")" << "is on the x axis." <<
endl;
break;
case (var1 == 0) && ((var2 >= 1) || (var2 <= -1)):
cout << "(" << var1 << "," << var2 << ")" << "is on the y axis." <<
endl;
break;
case (var1 >= 1) && (var2 >= 1): cout << "(" << var1 << "," << var2 <<
")"
<< " is in the first quadrant." << endl;
break;
case (var1 <= -1) && (var2 >= 1): cout << "(" << var1 << "," << var2 <<
")"
<< " is in the second quadrant." << endl;
break;
case (var1 <= -1) && (var2 <= -1):cout << "(" << var1 << "," << var2 <<
")"
<< " is in the third quadrant." << endl;
break;
case (var1 >= 1) && (var2 <= -1): cout << "(" << var1 << "," << var2 <<
")"
<< " is in the fourth quadrant." << endl;
break;
}

return 0;
}

You have case statement syntax wrong, and it won't work for what you want.

it's:
switch ( expression ) statement
case constant-expression : statement
default : statement

Notice the case takes a constant-expression. == is already "built in".

Such as:
switch( c )
{
case 'A':
capa++;
break;
case 'a':
lettera++;
break;
default :
total++;
}

Notice, NOT case ( c == 'a' ), but case 'A':

You could do more than one...
case 'A':
case 'B':
case 'C':
std::cout << "A to C" << std:endl;
break;

So, it's not going to do what you want. A case statement can't check for
>=, only ==
You'll need to use some other form of construct, I'd use if statements, and,
to be truthful, your whole block is in the form of a big if statement block
anyway with a little format adjusting.

if ( (var1 == 0) && (var2 == 0) )
cout << "(" << var1 << "," << var2 << ")"
<< " is on the origin." << endl;
else if ( ((var1 >= 1) || (var1 <= -1)) && (var2 == 0) )
cout << "(" << var1 << "," << var2 << ")" << "is on the x axis." <<
endl;
else if ( (var1 == 0) && ((var2 >= 1) || (var2 <= -1)) )
cout << "(" << var1 << "," << var2 << ")" << "is on the y axis." <<
endl

etc...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top