12 Replies - 612 Views - Last Post: 21 July 2011 - 03:25 PM Rate Topic: -----

#1 tim9009  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 73
  • Joined: 01-October 10

"switch" using strings

Posted 20 July 2011 - 07:17 PM

hello

I searched for it and found that I can't test a string in a switch (So I know that, no need to tell me).
I wonder if there is a relativly simple way to make system that works like a switch, or to modify the string to work whit a switch(like a char array or something(need an exaple)).

I'm making a program that test a string and if the string = some command then it goes to a function. The problem is that if I use the if statement I can't use the else command because I have multiple if statements. I hope you understand :P

-Tim

This post has been edited by tim9009: 20 July 2011 - 07:18 PM


Is This A Good Question/Topic? 0
  • +

Replies To: "switch" using strings

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6033
  • View blog
  • Posts: 23,417
  • Joined: 23-August 08

Re: "switch" using strings

Posted 20 July 2011 - 07:27 PM

Is this C or C++? If it's C, use strcmp, if it's C++, use std::string and ==.

EDIT: And there's no reason you can't use
if (this)
{
} 
else if (that)
{
}
else if (the_other)
{
}
else
{
}

This post has been edited by JackOfAllTrades: 20 July 2011 - 07:29 PM

Was This Post Helpful? 0
  • +
  • -

#3 McSick  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 179
  • Joined: 02-September 10

Re: "switch" using strings

Posted 20 July 2011 - 07:28 PM

In C you can use string.h and strcmp(string1,string2). It returns 0 if both are equal so you could do that with else ifs.


 if(!strcmp(string,"Command"))
{
  blah blah
}else if(!strcmp(string,"Command2")){
  blah blah
}




and even easier in C++ <string>


#include <string>

std::string string,command;
if(string == command)
///...ect like above



Was This Post Helpful? 0
  • +
  • -

#4 tim9009  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 73
  • Joined: 01-October 10

Re: "switch" using strings

Posted 20 July 2011 - 07:29 PM

Ok, so I played around whit the if statemant and found a way to make a series of if statements that work the exact same way as a switch.


Here is an exapmle program:
#include <iostream>
#include <string>
using namespace std;

void main()
{
	string mystring = "0";

	while(1)
	{
		cin >> mystring;
		

		if (mystring=="/command1")
		{
			cout << "I'm doing command 1." << endl;
			system("pause");
		}

		if (mystring=="/command2")
		{
			cout << "I'm doing command 2." << endl;
			system("pause");
		}

		if (mystring!="/command2","command1",)
		{
		cout << "Invalid command, try again." << endl;
		system("pause");
		}
	}
}



Do you have a better soulution? :)
Was This Post Helpful? 0
  • +
  • -

#5 McSick  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 179
  • Joined: 02-September 10

Re: "switch" using strings

Posted 20 July 2011 - 07:33 PM

My good sir, why not just use else if?



if(condition){
  //code
}else if(condition2){
  //more code
}else{
  //default 
}



Was This Post Helpful? 2
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,298
  • Joined: 25-December 09

Re: "switch" using strings

Posted 20 July 2011 - 07:34 PM

How about if() else :

		if (mystring=="/command1")
		{
			cout << "I'm doing command 1." << endl;
			system("pause");
		} 
                else if (mystring=="/command2")
		{
			cout << "I'm doing command 2." << endl;
			system("pause");
		} 
		else
		{ // This would be the default if none of the above are true.
          		cout << "Invalid command, try again." << endl;
	        	system("pause");
		}



Also you can not use the comma operator in an if statement:
if (mystring!="/command2","command1",) you must have both sides of the comparison if (mystring!="/command2" || mstring != "command1").


Jim
Was This Post Helpful? 2
  • +
  • -

#7 tim9009  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 73
  • Joined: 01-October 10

Re: "switch" using strings

Posted 20 July 2011 - 07:57 PM

Thank you, that works better :)
I think I usnderstand how it works , but just to be on the safe side would you care to explain how?

-Tim

View Postjimblumberg, on 20 July 2011 - 07:34 PM, said:

How about if() else :

		if (mystring=="/command1")
		{
			cout << "I'm doing command 1." << endl;
			system("pause");
		} 
                else if (mystring=="/command2")
		{
			cout << "I'm doing command 2." << endl;
			system("pause");
		} 
		else
		{ // This would be the default if none of the above are true.
          		cout << "Invalid command, try again." << endl;
	        	system("pause");
		}



Also you can not use the comma operator in an if statement:
if (mystring!="/command2","command1",) you must have both sides of the comparison if (mystring!="/command2" || mstring != "command1").


Jim


Yeah... you see, I used the comma opperator and it worked out fine for me. The exaple I wrote at the top does compile and does not show any runtime errors. I will still use the if()else system because it is cleaner. :)

(no offence if I'm wrong, still learning here ;) )
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,298
  • Joined: 25-December 09

Re: "switch" using strings

Posted 20 July 2011 - 08:04 PM

Quote

Yeah... you see, I used the comma opperator and it worked out fine for me.

You will not get an error however it will not produce the desired results. You should insure that your compiler is generating warnings, because your compiler should warn you about this.

As far as using a series of if statements versus if else statements the difference is the series checks each if statement, the if/else/else if stops checking the first time one of the statements evaluate to true, the other statements will not be checked.

Jim

This post has been edited by jimblumberg: 20 July 2011 - 08:05 PM

Was This Post Helpful? 0
  • +
  • -

#9 tim9009  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 73
  • Joined: 01-October 10

Re: "switch" using strings

Posted 21 July 2011 - 12:37 AM

got it, thank you :)
Was This Post Helpful? 0
  • +
  • -

#10 oscode  Icon User is offline

  • D.I.C Regular

Reputation: 109
  • View blog
  • Posts: 257
  • Joined: 24-October 10

Re: "switch" using strings

Posted 21 July 2011 - 02:43 AM

I don't think the compiler should warn about this. Comma expressions are a fairly obscure part of the language but it is completely valid, see:
http://publib.boulde...%2Fref%2Fco.htm

This post has been edited by oscode: 21 July 2011 - 02:44 AM

Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,298
  • Joined: 25-December 09

Re: "switch" using strings

Posted 21 July 2011 - 05:54 AM

I get the following warning when using the comma operator used in the if statement.

Quote

warning: left-hand operand of comma has no effect|




Jim
Was This Post Helpful? 0
  • +
  • -

#12 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: "switch" using strings

Posted 21 July 2011 - 11:59 AM

View Postoscode, on 21 July 2011 - 10:43 AM, said:

I don't think the compiler should warn about this. Comma expressions are a fairly obscure part of the language but it is completely valid
All warnings are about code which is completely valid as far as the compiler is concerned - this is why they are warnings and not errors.

Warnings are a hint to the programmer that they have probably done something in their code which they didn't intend, or that they may have unwittingly left their program open to undefined behaviour. Having part of an expression which effectively "does nothing" seems to me like a reasonable enough cause to warn the programmer that they might have made a mistake.
Was This Post Helpful? 0
  • +
  • -

#13 bdemorrow  Icon User is offline

  • New D.I.C Head

Reputation: 11
  • View blog
  • Posts: 29
  • Joined: 21-July 11

Re: "switch" using strings

Posted 21 July 2011 - 03:25 PM

View Posttim9009, on 20 July 2011 - 07:17 PM, said:

hello

I searched for it and found that I can't test a string in a switch (So I know that, no need to tell me).
I wonder if there is a relativly simple way to make system that works like a switch, or to modify the string to work whit a switch(like a char array or something(need an exaple)).

I'm making a program that test a string and if the string = some command then it goes to a function. The problem is that if I use the if statement I can't use the else command because I have multiple if statements. I hope you understand :P

-Tim

I would recommend using numbers for your commands anyway..

enum Command{
    Command1,
    Command2,
    Command3
}
enum Command _command;
_command = Command1;

switch(_command){
    case Command1:
        DoCommand1();
        break;
    case Command2:
        DoCommand2();
        break;
    case Command3:
        DoCommand3();
        break;
};



Something like above would use less resources than having your processor loop through both strings comparing each character every time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1