473,738 Members | 8,848 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4700
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*******@aust in.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, "deanfamily 11"
<de**********@v erizon.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

"deanfamily 11" <de**********@v erizon.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
14147
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 appropriate document, or post an example? I don't relish the idea especially long if-else statements. Joe
7
1780
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 deleted on End Select. How can I make it available for my loop?
3
3398
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 = "" if i = 1 then strURL = "redirect.aspx?page=APIQ&parcel=" & strParcel &
12
21087
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 record (3232,'Raindeer') based on the condition that the
1
21686
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 "01" : dWarrExpDateMonth="Jan" : dRetailDateMonth="Jan" case "02" : dWarrExpDateMonth="Feb" : dRetailDateMonth="Feb" End Select
22
3168
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 numbers then added support for putting in lat/lon coordinates. I tried this little piece of code inside the OnChar function but compiler complained about missing ';' after "case _T('W'):"
1
5012
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 (simple-case-statement-when-clause), in this - the value of the expression prior to the first WHEN keyword is tested for equality with the value of each expression that follows the WHEN keyword. Right? Then if the search condition is true, the THEN...
9
2112
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 have been something else. An extremely stripped-down version: int Function (int something) { switch(something) { case WHATEVER:
13
11830
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 just curious to find out is it effective to use this method?? or is there is any other alternative method present so that execution time and code size can be reduced?? Thanks in advance.
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.