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

Creating variables in switches

MrPickle
100 100+
I am trying to create a new variable in a switch, read in some data then push back the variable onto a vector. The only problem is if I try to create a variable in a switch the compiler complains, so is there a way to do this without making the compiler complain?

Example:
Expand|Select|Wrap|Line Numbers
  1. struct Fruit {
  2.    std::string name;
  3.    double price;
  4. };
  5.  
  6. void Read(std::vector<Fruit>& f, std::ifstream& in) {
  7.    int type;
  8.    in >> type;
  9.  
  10.    switch(type) {
  11.       case APPLE:
  12.          Fruit apple;
  13.          apple.name = "Apple";
  14.          in >> apple.price;
  15.          f.push_back(apple);
  16.  
  17.       case KIWI:
  18.          Fruit kiwi;
  19.          kiwi.name = "Kiwi";
  20.          in >> kiwi.price;
  21.          f.push_back(kiwi);
  22.    }
  23. }
Dec 1 '08 #1
6 1989
Ganon11
3,652 Expert 2GB
Are you sure it's the object creation your compiler is complaining about?

Strings are not integer values, and so they can't be used in switch statements. Better modify your input there, or just use if...else if...else statements.
Dec 1 '08 #2
MrPickle
100 100+
It is saying:
initialization of 'apple' is skipped by 'default' label
Dec 1 '08 #3
Banfa
9,065 Expert Mod 8TB
It is best to avoid creating variables on the stack inside switches for just this reason. The switch is a single block of code (unlike an if statement) so it is possible for the code creating the data (and executing the constructor) to be skipped by going through a different case statement.

You can
  1. Add otherwise unrequired braces round the contents of the case
    Expand|Select|Wrap|Line Numbers
    1.        case KIWI:
    2.           {
    3.           Fruit kiwi;
    4.           kiwi.name = "Kiwi";
    5.           in >> kiwi.price;
    6.           f.push_back(kiwi);
    7.           }
    artifically creating a code block
  2. Declare the variable outside the switch statement
  3. Create the data on the heap (use new and delete)


BTW all you case blocks are missing break; statements.
Dec 1 '08 #4
MrPickle
100 100+
When I create an artificial code block, do I need to break the switch?
Dec 1 '08 #5
Banfa
9,065 Expert Mod 8TB
Yes, C++ is one of those languages that never invisibly adds statements for you, if you don't put in a break it wont break.
Dec 1 '08 #6
MrPickle
100 100+
Ok, thank you. It is working now.
Dec 1 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Ayaz Ahmed Khan | last post by:
Can a variable containing, say, an integer value be used as size declarator for an array of integers? g++ does not complain, but other compilers, such as Visual C++/6.0, Borland Builder C++, Turbo...
1
by: Richard Hayden | last post by:
Hi, I'm trying to port my O/S kernel from C to C++. I am using g++ with the following command-line switches: -Wall -fno-builtin -fno-rtti -fno-exceptions -fno-enforce-eh-specs -nostartfiles...
35
by: whisper | last post by:
My question is whether it is better to use a global variable to hold a dynamically malloced multidim array or pass around pointers to it. The details are below (forgive the long winded explanation)...
5
by: John Salerno | last post by:
Here's my full code: using System; using System.Collections.Generic; using System.Text; using System.IO; namespace PatternMatcher { class Program
5
by: Paramesh | last post by:
Hello friends, How can i create and use dynamic link libraries with ANSI or ISO C language? Thank You,
3
by: Rico | last post by:
Hello, I'm new to VB.net and I have an application that I would like to open up in one of two different modes depending on a command line switch. The code will run as a scheduled task in...
5
by: Noel Mosa | last post by:
Hi, i am using a library compiled in C(FFMPEG) and need to access a number of variables that are defined within a C module. The include file for the library declares it as a extern pointer. I can...
4
by: Ted | last post by:
I have a small pre-determined number of possible values for a variable, and I would like to set a value for the corresponding 'switch type' variable. For example, if $x is 14, I want to execute...
6
by: Victor | last post by:
I've got a website that displays the same whether it is accessed using www. or not. ex: http://www.mysite.com and http://mysite.com give me exactly the same website (both represent the top...
5
by: Vibhesh | last post by:
I am facing problem with TimeSpan structure when DirectX is used. Following is the sample code that causes the problem: ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
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...

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.