Jump to content
  • Advertisement
Sign in to follow this  
deathwearer

Another Simple Question

This topic is 4847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
No. Use if..else if instead. Or, if you have a huge number of cases, look into using a map-lookup of some sort.

Share this post


Link to post
Share on other sites
Or you can use a std::map to connect strings to functions/function objects or even integers you can switch on.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 map
using 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();
}

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!