473,387 Members | 1,501 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,387 software developers and data experts.

case statement,

Why can't I define variables in a case statement:

case 1:
float u = expression;
...
break;
Also, is it bad form to cast ints to enums? For example, one might want 11
to map to an enum element NOVEMBER.
Sep 15 '06 #1
4 2791
"vsgdp" <he***@null.comwrote in message
news:u8nOg.9542$cw.2165@fed1read03...
Why can't I define variables in a case statement:

case 1:
float u = expression;
...
break;
Also, is it bad form to cast ints to enums? For example, one might want
11 to map to an enum element NOVEMBER.
case 1:
{
float u = expression;
// ...
break;
}
case 2:
// ....
Sep 15 '06 #2
"Jim Langston" <ta*******@rocketmail.comwrote in message
news:qn************@newsfe04.lga...
"vsgdp" <he***@null.comwrote in message
news:u8nOg.9542$cw.2165@fed1read03...
>Why can't I define variables in a case statement:

case 1:
float u = expression;
...
break;
Also, is it bad form to cast ints to enums? For example, one might want
11 to map to an enum element NOVEMBER.

case 1:
{
float u = expression;
// ...
break;
}
case 2:
// ....
Sorry, missed the 2nd part of your question, re casting ints to enums.
Casting a number as a month, would be okay *some of the time*. What
happens, however, when the value is 13?

enum Months
{
January = 1,
February,
March,
// ...
December
}

int Month = (something).

Going from Months to Month wouldn't be a problem as such, as you can examine
the value. if ( Month 13 || Month < 1 ) { some error }.

But when you go into a variable declared as Months
Months EnumMonth = Month; // maybe some cast required

What re you going to do if Month is 13? In a switch statement it shouldn't
be that difficult as long as you remember to use default:

I go from ints to enums all the time when serializing my classes (writing
them to files or databases) but I ensure that the int value came from an
enum, not from some other form of input (such as user input) without
checking.

I may have understood your question wrong, however. You may wish to
rephrase it as to what it is you would actually want to do.
Sep 15 '06 #3
On Thu, 14 Sep 2006 18:19:53 -0700 in comp.lang.c++, "vsgdp"
<he***@null.comwrote,
>Why can't I define variables in a case statement:

case 1:
float u = expression;
...
break;
You cannot define variables in such a way that some code execution
paths jump around them, leaving it ambiguous whether they would be
defined or not. Oldy moldy switch/case is rather unstructured in
that regard.

If you want a local variable in a case, bracket it:

case 1: {
float u = expression;
...
}
break;
Sep 15 '06 #4
David Harmon wrote:
On Thu, 14 Sep 2006 18:19:53 -0700 in comp.lang.c++, "vsgdp"
<he***@null.comwrote,
Why can't I define variables in a case statement:

case 1:
float u = expression;
...
break;

You cannot define variables in such a way that some code execution
paths jump around them, leaving it ambiguous whether they would be
defined or not. Oldy moldy switch/case is rather unstructured in
that regard.
I think that's only the case when the declared variable is initialized.
So this:

switch(i)
{
case 1:
float u = 0;
break;

case 2:
break;

}

Requires a diagnostic, but if you remove the "= 0" part it would be ok.

Brian
Sep 15 '06 #5

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...
6
by: deanfamily11 | last post by:
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...
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...
5
by: Frederick Dean | last post by:
Hi,guys! I'm reading Stephen Dewhurst's book "C++ Gotchas"£¬in gothca #7, I meet a weird case: bool Postorder::next() { switch (pc) case START: while (true) if (!child()) { pc = LEAF; return...
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...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.