473,324 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Case statement

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
6 4671
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
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
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Joe Stevenson | last post by:
Hi all, I skimmed through the docs for Python, and I did not find anything like a case or switch statement. I assume there is one and that I just missed it. Can someone please point me to the...
7
by: Shapper | last post by:
Hello, I have a "Select Case MyVar" in which I define the values of an Array according to the value of MyVar. I need to use the Array Values in a Loop after End Select. It seems the Array is...
3
by: mark.irwin | last post by:
Hello all, Have an issue where a redirect pushes data to a page with a select case which then redirects to another page. Problem is the redirect isnt working in 1 case. Code below: strURL =...
12
by: rAinDeEr | last post by:
Hi, I have a table with 2 columns ** CREATE TABLE test (emp_num DECIMAL(7) NOT NULL,emp_name CHAR(10) NOT NULL) and i have inserted a number of records. ** Now, I want to insert a new...
1
by: microsoft.public.dotnet.languages.vb | last post by:
Hi All, I wanted to know whether this is possible to use multiple variables to use in the select case statement such as follows: select case dWarrExpDateMonth, dRetailDateMonth case...
22
by: John | last post by:
Hi Folks, I'm experimenting a little with creating a custom CEdit control so that I can decide on what the user is allowed to type into the control. I started off only allowing floating point...
1
by: priyanka2203 | last post by:
Hi guys, I have a doubt regarding the CASE statement. It might sound silly, but me being new to DB2, it is kind of a genuine doubt. Try helping me with this.. When we use a case statement...
9
by: Robbie Hatley | last post by:
Greetings, group. I just found a weird problem in a program where a variable declared in a {block} after a "case" keyword was being treated as having value 0 even though its actual value should...
13
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.