Another Simple Question

Started by
9 comments, last by kontrolkl 18 years, 9 months ago
Can i make a switch with a string? switch ( stringname ) My compilator say it is illegal, is there anyway i can avoid that?. Thank EDIT : Read last post [Edited by - deathwearer on August 12, 2005 1:34:34 PM]
Advertisement
No you can't.

If/then else is about your only choice.
No. Use if..else if instead. Or, if you have a huge number of cases, look into using a map-lookup of some sort.
Or you can use a std::map to connect strings to functions/function objects or even integers you can switch on.
Alright thanks guy, i will use if/else. I just prefer to read a switch :(
A switch only works for discreet data types. That is data types where given an element you can pick the next element.

For example ints. Given 3 the next one is definately 4. Same goes for char, long, short and enumerated types.

But what string comes after "Bob"? Also floating point types won't work because what float comes after 3.3333?
i have an error when i compile ( many )

All say

c:\Documents and Settings\DeathWearer\Mes documents\c++\tuts\Tetris\Piece.cpp(93): error C2106: '-=' : left operand must be l-value

for this line exemple

iTempX -= 40;

what is that? or what does this error mean
Ahem. If you're going to italicize a "technical term", be sure to spell it correctly. We're not talking about whether the data types will "kiss and tell" here. ;) (You wanted 'discrete'.)

Anyway, in C++ it will only work for integral types (which includes enumerations). The reasoning is that it allows the compiler to do l33t stuff in the assembly code, such that some math is used to figure out where to continue operating.

Some languages provide a similar-looking construct that works with any type, but it is effectively syntactic sugar for the if-else chain.

For a lot of items, though, the mapping approach can end up being much cleaner. What you need to do is make some datatype that represents the action to take. It looks something like this:

#include <iostream>#include <string> // for strings - real ones#include <map> // for the mapusing namespace std;// For the needed polymorphism, we'll define a simple base "action".// The interface consists of just the "operator()", which lets us treat objects// as if they were functions.struct Action {  virtual operator() () = 0;};struct HelloAction : Action {  operator() () { cout << "y helo thar 2u2 lolz!!!11" << endl; }}// Now we can already do something like:// HelloAction ha;// ha(); // the object behaves like a function.// Now let's set up a map:map<string, Action> actions;// We construct a HelloAction, and store it in the map.// The map stores things by key; given a key, we can find the matching value. action.insert("hello", HelloAction());// Later, we can check a given string:string userInput; cin << userInput;// Look for the key in the map.map<string, Action>::iterator item = actions.find(userInput);if (item == actions.end()) {  // this means that the key was not found. Therefore "default" handling  // goes here} else {  // the "item" is an iterator pointing at the (key, value) pair stored in the  // map. Thus "item->second" gives us the stored value that corresponds to  // the key - for us, this means the Action object. Then, we "invoke" that  // object via the operator().  item->second();}
anyone know why i'm having this error?

c:\Documents and Settings\DeathWearer\Mes documents\c++\tuts\Tetris\Piece.cpp(93): error C2106: '-=' : left operand must be l-value

for this line exemple

iTempX -= 40;
In all likelihood, iTempX is const, making it a non-lvalue. Show us sufficient code, particularly including the declaration of iTempX and any scope transitions (especially being passed to a function, in which case we'll need the function signature) to get to the bottom of this.

This topic is closed to new replies.

Advertisement