Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[Solved]Switch/case string

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
M.CORP
Grandmaster Cheater Supreme
Reputation: 28

Joined: 28 Oct 2009
Posts: 1010

PostPosted: Sun Feb 13, 2011 11:47 pm    Post subject: [Solved]Switch/case string Reply with quote

I haven't been on C++ lately, but my friend challenged me to a competition, which is basically make a source port of his program to C++.
I'm trying out this code that i wrote in scratch:
Code:
switch(var){
case "blah":
statement;
break;
case "stuff":
statement;
break;
default:
return 0;
break;
}

If there's an alternative way without using the 'if', 'else', 'else if' conditions, that's great!
Thanks! Very Happy

_________________
Shameless Self Advertising Very Happy!
Steam
Just lurking around...


Last edited by M.CORP on Wed Feb 16, 2011 10:33 pm; edited 1 time in total
Back to top
View user's profile Send private message
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Mon Feb 14, 2011 5:25 am    Post subject: This post has 1 review(s) Reply with quote

http://www.codeguru.com/cpp/cpp/cpp_mfc/article.php/c4067

You can't use switch on type String in c++, but look at that for a hackaround.

_________________
Back to top
View user's profile Send private message
Freiza
Grandmaster Cheater
Reputation: 22

Joined: 28 Jun 2010
Posts: 662

PostPosted: Mon Feb 14, 2011 8:02 am    Post subject: Reply with quote

Is this what you want??
Code:
#include <iostream>
#include<strings.h>
using namespace std;

int main()
{
   
char var[100];
cin>>var;
while(!strcmp(var,"blah"))
{
//do something
cout<<"blah";
goto here;
break;
}
while(!strcmp(var,"stuff"))
{
//do stuff
cout<<"stuff";
goto here;
break;
}
return 0;
here:
cout<<" here";
    return 0;
}


if you want more ways.. tell me
Back to top
View user's profile Send private message Send e-mail
atom0s
Moderator
Reputation: 200

Joined: 25 Jan 2006
Posts: 8526
Location: 127.0.0.1

PostPosted: Mon Feb 14, 2011 1:39 pm    Post subject: Reply with quote

Using while loops and goto is fairly bad practice to simulate a switch or if/then tree. (You should avoid goto all together with programming.)
_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Freiza
Grandmaster Cheater
Reputation: 22

Joined: 28 Jun 2010
Posts: 662

PostPosted: Tue Feb 15, 2011 7:00 am    Post subject: Reply with quote

True.. but the challenge was to find alternative..
Oops..
Back to top
View user's profile Send private message Send e-mail
M.CORP
Grandmaster Cheater Supreme
Reputation: 28

Joined: 28 Oct 2009
Posts: 1010

PostPosted: Tue Feb 15, 2011 10:36 pm    Post subject: Reply with quote

HomerSexual wrote:
http://www.codeguru.com/cpp/cpp/cpp_mfc/article.php/c4067

You can't use switch on type String in c++, but look at that for a hackaround.

Ok, i'll try that!
Thanks!

_________________
Shameless Self Advertising Very Happy!
Steam
Just lurking around...
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Wed Feb 16, 2011 6:39 am    Post subject: Reply with quote

std::map?
Back to top
View user's profile Send private message
M.CORP
Grandmaster Cheater Supreme
Reputation: 28

Joined: 28 Oct 2009
Posts: 1010

PostPosted: Wed Feb 16, 2011 10:32 pm    Post subject: Reply with quote

Solved! Thanks Homer! Very Happy
_________________
Shameless Self Advertising Very Happy!
Steam
Just lurking around...
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites