Initializing variables (int32) inside switch statement

This question already has an answer here:

  • Why can't variables be declared in a switch statement? 23 answers

I have a code block that I'm trying to convert from PHP to C++ and the compiler is getting hung up on my switch statements.

I have something along the lines of:

switch(//Some int) { case 1: default: int32 x = 1; doSomething(x); break; case 2: doSomething(3); break; }

And this is throwing errors:

error C2360: initialization of 'x' is skipped by 'case' label

I seem to be able to fix this by declaring and initializing the variables outside of the switch statement, but why is this? What's the problem with creating temporary variables in the scope of a switch statement?

Just for further clarification, I am only attempting to use x in that one call to doSomething(x). I don't attempt to use it outside of the switch statement's scope.

-------------Problems Reply------------

Try adding {}:

int32 x = 1;

According to the standard:

It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. A program that jumps91 from a point where a variable with automatic storage duration is not in scope to a point where it is in scope is ill-formed unless ....

void f() {
// ...
goto lx; // ill-formed: jump into scope of a
// ...
X a = 1;
// ...
goto ly; // OK, jump implies destructor
// call for a followed by construction
// again immediately following label ly

91) The transfer from the condition of a switch statement to a case label is considered a jump in this respect.

Category:c# Views:2 Time:2018-03-27

Related post

  • For Loop Inside Switch Statement in PHP 2013-09-24

    I have certain ranges saved in array with price at index [3] and discount type at index [4] (%, fixed). Anyone buying within those ranges should get available discount. My Current Problem is range of an array could be of any count, for example here i

  • Get variable passed to switch statement 2010-11-08

    This isn't the exact use scenario but I was wondering if it was possible to get the value passed to the switch statement without having to retype what is in the switch() part. Example : switch(someObject.withSomevalue*(Math.random()*11)) { case 1 : a

  • No. of variables in a switch statement - Java 2011-06-24

    Can you include more than one variable in a switch statement in Java? enum Facing { North, South, East, West } enum Forward { Right, Left } Forward forward; Facing facing; Integer myNumber; So it looks like this? And if so how would I go about implem

  • How do i use a variable in a switch statement *updated * 2012-04-23

    I am trying to use a variable in my switch statement as it makes the player movement smooth. This is my smooth player movement code. stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown); stage.addEventListener(KeyboardEvent.KEY_UP, keyPress

  • How do you use enum inside switch statement using MVC & razor view engine? 2011-08-15

    I have a service which returns a Json data structure. One of the properties in the data is an enum value. When the Json is returned inside the client, I'd like it to react to the data returned using a switch statement. I don't want to hard-code the e

  • Why is variable declared inside IF statement created even when condition evaluates to false? 2010-06-01

    Since @A is never declared, sql server should throw an error, but it doesn’t. Why is that? DECLARE @i int = 1; IF @i > 10 BEGIN DECLARE @A int = 100; END PRINT @A; // doesn't return any result thanx --------------Solutions------------- SQL Server

  • PHP adding variable thats inside if statement 2011-11-03

    I dont understand why my code doesn't work....What I am trying to do is have a variable hold a number if a condition is true, and then add that variable to other variables $monthlyCharge = 0.00; $x = 5; $y = 10; $z = 15; if($y > $x){ $monthlyCharg

  • Why doesn't C# allow declaring variables with the same number inside different case blocks for switch statements? 2010-09-09

    Possible Duplicate: Case Statement Block Level Declaration Space in C# For example: string danger; switch ( this.Type ) { case Warfare.Nuclear: case Warfare.Biological: case Warfare.Chemical: danger = "deadly"; break; case Warfare.Air: string threat

  • Why can't variables be declared in a switch statement? 2008-09-18

    I've always wondered this - why can't you declare variables after a case label in a switch statement? In C++ you can declare variables pretty much anywhere (and declaring them close to first use is obviously a good thing) but the following still won'

  • Variable declaration in a C# switch statement 2008-10-21

    Why is it that in a C# switch statement, for a variable used in multiple cases, you only declare it in the first case? For example, the following throws the error "A local variable named 'variable' is already defined in this scope". switch (Type) { c

  • wordpress switch statement within the loop 2011-03-30

    I'm attempting to use a switch statement within the worpdpress loop to change classes on a div, but the incrementing counter ($IntCounter) doesn't seem to be firing within the loop: <?php global $intCounter; $intcounter = 0; query_posts('category_

  • Xcode 4.2 switch statement bug? 2011-10-16

    BUT! add a random statement before: Any thoughts? --------------Solutions------------- This isn't a switch-statement bug, it's a limitation in the C language. In C, the first statement after a case label cannot be a variable declaration. You can get

  • Switch statement doesn't switch 2011-12-18

    I'm trying to change the value of a variable in a switch statement, with a spinner. But the variable is stuck on the default value. Here's the code with the switch statement, and how the variable is used. final Spinner feedbackSpinner = (Spinner) fin

  • Is it possible to use a class in a switch statement? 2012-01-11

    I am developing for mobile on Java 1.3 and don't have enum type, so instead am using the Typesafe Enum Pattern. E.g. public class DownloadEvent { //Download events public static final DownloadEvent DOWNLOAD_STARTED = new DownloadEvent("Download start

  • Multiple Separate Switch Statements 2009-02-07

    I have multiple switch statements in on one of the pages in order to pass different variables to the URL, as well as different case. I need these different switch statements because I need the different variables. However, when I put a "default" in o

  • Switch statement with returns - code correctness 2010-06-17

    Let's say I have code in C with approximately this structure: switch (something) { case 0: return "blah"; break; case 1: case 4: return "foo"; break; case 2: case 3: return "bar"; break; default: return "foobar"; break; } Now obviously, the "break"s

  • Break out out forloop but within switch statement php 2010-07-16

    When I normally want to break out of a foreach loop before all of the iterations have completed I simply use a break; statement. e.g. foreach($nodelist as $node) { if($metCriteria) { break; } } But my next example has a switch statement in it. And if

  • Switch statement for greater-than/less-than 2011-07-12

    so I want to use a switch statement like this: switch (scrollLeft) { case (<1000): //do stuff break; case (>1000 && <2000): //do stuff break; } Now I know that either of those statements (<1000) or (>1000 && <2000) w

  • Python variable scope in if-statements 2011-09-12

    In Python, are variable scopes inside if-statements visible outside of the if-statement? (coming from a Java background, so find this a bit odd) In the following case, name is first defined inside the if-block but the variable is visible outside of t

  • Variable declared and initialized in a switch statement 2009-11-05

    Why does this program not output 20? #include<stdio.h> int main() { int a = 1; switch (a) { int b = 20; case 1: { printf("b is %d\n", b); break; } default: { printf("b is %d\n", b); break; } } return 0; } --------------Solutions------------- Be

  • I declare a variable in C# and cannot use it inside of the switch statement 2011-08-01

    Consider this C# code: string gr = comboBox1.ValueMember; decimal sum; try { decimal rite = Convert.ToDecimal(textBox1.Text); decimal left = Convert.ToDecimal(textBox2.Text); } catch (Exception) { string swr = "Please enter REAL a number, can be with

  • I declare a variable and cannot use it inside of the switch statement 2011-08-01

    string gr = comboBox1.ValueMember; decimal sum = 0M; try { decimal rite = Convert.ToDecimal(textBox1.Text); decimal left = Convert.ToDecimal(textBox2.Text); } catch (Exception) { string swr = "Please enter REAL a number, can be with decimals"; label2

  • Can I declare variables inside an Objective-C switch statement? 2009-07-12

    I think I'm going blind, because I can't figure out where the syntax error is in this code: if( cell == nil ) { titledCell = [ [ [ TitledCell alloc ] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier ] autorelease ]; switch( cellNumber ) { case

  • Declaring variables inside a switch statement 2009-08-05

    This question already has an answer here: Why can't variables be declared in a switch statement? 23 answers I saw a few answers to this issue, and I get it — you can't declare and assign variables inside a switch. But I'm wondering if the following i

Copyright (C), All Rights Reserved.

processed in 0.104 (s). 11 q(s)