View previous topic :: View next topic |
Author |
Message |
M.CORP Grandmaster Cheater Supreme
Reputation: 28
Joined: 28 Oct 2009 Posts: 1010
|
Posted: Sun Feb 13, 2011 11:47 pm Post subject: [Solved]Switch/case string |
|
|
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!
_________________
Last edited by M.CORP on Wed Feb 16, 2011 10:33 pm; edited 1 time in total |
|
Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
|
Back to top |
|
 |
Freiza Grandmaster Cheater
Reputation: 22
Joined: 28 Jun 2010 Posts: 662
|
Posted: Mon Feb 14, 2011 8:02 am Post subject: |
|
|
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 |
|
 |
atom0s Moderator
Reputation: 204
Joined: 25 Jan 2006 Posts: 8581 Location: 127.0.0.1
|
Posted: Mon Feb 14, 2011 1:39 pm Post subject: |
|
|
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 |
|
 |
Freiza Grandmaster Cheater
Reputation: 22
Joined: 28 Jun 2010 Posts: 662
|
Posted: Tue Feb 15, 2011 7:00 am Post subject: |
|
|
True.. but the challenge was to find alternative..
Oops..
|
|
Back to top |
|
 |
M.CORP Grandmaster Cheater Supreme
Reputation: 28
Joined: 28 Oct 2009 Posts: 1010
|
Posted: Tue Feb 15, 2011 10:36 pm Post subject: |
|
|
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!
_________________
|
|
Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Wed Feb 16, 2011 6:39 am Post subject: |
|
|
std::map?
|
|
Back to top |
|
 |
M.CORP Grandmaster Cheater Supreme
Reputation: 28
Joined: 28 Oct 2009 Posts: 1010
|
Posted: Wed Feb 16, 2011 10:32 pm Post subject: |
|
|
Solved! Thanks Homer!
_________________
|
|
Back to top |
|
 |
|