By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,278 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,278 IT Pros & Developers. It's quick & easy.

Case statement

P: n/a
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;
}
Sep 10 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
deanfamily11 wrote:
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++.
[snip]


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!"
Sep 10 '05 #2

P: n/a
GB
deanfamily11 wrote:
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
Sep 10 '05 #3

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

"Artie Gold" <ar*******@austin.rr.com> wrote in message
news:3o************@individual.net...
deanfamily11 wrote:
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++.
[snip]


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!"

Sep 10 '05 #4

P: n/a
M
On Sat, 10 Sep 2005 03:51:48 GMT, "deanfamily11"
<de**********@verizon.net> wrote:
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.
Sep 10 '05 #5

P: n/a
deanfamily11 wrote:
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
Sep 10 '05 #6

P: n/a

"deanfamily11" <de**********@verizon.net> wrote in message
news:oLsUe.576$vQ3.44@trnddc08...
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...
Sep 11 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.