Jump to content
  • Advertisement
Sign in to follow this  
kingpinzs

prossesing string input in c++

This topic is 5138 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

I am trying make a function that will take a bunch of key strings. example
int menuinput(void)
{
std::string pingv;
	std::string input2;
	std::string drive;
	drive = "c";
    std::cout << "C:\>" ;
	cin >> input2 ;
std::transform(input2.begin(), input2.end(), input2.begin(), (int(*)(int))std::toupper);
//if( strnicmp( input2.c_str(), "fdisk", 10 ) == 0 )	
if (input2 == "FDISK")
	{
		FDISK();
	}	
	if (input2 == "DEBUG")
	{
		debug();
	}
if (input2 == "FORMAT")
	{
	    // can not put in a drive letter yet
	    // need to define drive letter as a string constant
	  std::cout << "The type of file system is NTFS."<< std::endl << std::endl;
		FORMAT();
	}    
if (input2 == "HELP")
	{
		help();
	}
	if (input2 == "PING") 
{
		std::cin >> pingv;
	std::cout << "pinging " << pingv << std::endl;
	ping();
}
if (input2 == "ipconfig")
{
	ipconfig();
}	
if (input2 == "EXIT")
{
	std::cout << "CLOSING SIMULATION" << std::endl;
	exit (1);
}
if (input2 == "TIME")
{
	ttime();
}
if (input2 == "DATE")
{
	ttime();
}
/*
if (input2 == "sample")
{
  externalpro();
}

if (input2 == "DIR")
  {
  int dir();
  }
*/
  if (input2 == "CLS")
  { 
  system("cls");
  menuinput();
  }
if (input2 == "DOS")
{
    doscom();
}
	else
{
	std::cout <<" NOT A VALID COMMAND:"<<std::endl;
	menuinput();
}
	return 0;
}

Is there is better way to do this? I need to keep adding in input strings and I dont want to make it any bigger. Thanks

Share this post


Link to post
Share on other sites
Advertisement
I dont think there is an overall better solution but there is one thing I can definately help out with with that I'm sure you'll be glad about.

Instead of going ...

if then
if then
if then

use a SWITCH control structure.

These should get you going:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/statem_10.asp

http://courses.cs.vt.edu/~cs3304/Spring00/notes/Chapter-7/tsld017.htm

http://www.ictp.trieste.it/texi/gpp/gpp_36.html

Share this post


Link to post
Share on other sites
Switch's only work with singular constants, not arrays (like strings) so you'll have to use if statements.
However if it were my project I'd create a structure containing a name & prehaps a function pointer, then make a global array of those.
Do a quick search through & compare each name when a command is entered rather than typing all searches by hand

Share this post


Link to post
Share on other sites
Re xyuri: I think the C++ switch statement only allows simple constants (e.g. char or int) in the case expression.

How about mapping strings to function pointers? Here is an example of the idea:


typedef map<string, void (*)()> CmdMap;

void bye() { cout << "Bye\n"; exit(0); }
void hello() { cout << "Hello\n"; }
void world() { cout << "World\n"; }

int main(int argc, char** argv) {
CmdMap cmdMap;
cmdMap["HELLO"] = hello;
cmdMap["WORLD"] = world;
cmdMap["BYE"] = bye;

string cmd;
while (cin >> cmd) {
transform(cmd.begin(), cmd.end(), cmd.begin(), toupper);
if (cmdMap.find(cmd) != cmdMap.end()) {
cmdMap[cmd]();
} else {
cerr << "Unknown command: " << cmd << '\n';
return 1;
}
}
return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by xyuri
I dont think there is an overall better solution but there is one thing I can definately help out with with that I'm sure you'll be glad about.

Instead of going ...

if then
if then
if then

use a SWITCH control structure.

Which, however, only works for integral data types, not for strings.

Share this post


Link to post
Share on other sites
Quote:
Original post by Miserable
Quote:
Original post by xyuri
I dont think there is an overall better solution but there is one thing I can definately help out with with that I'm sure you'll be glad about.

Instead of going ...

if then
if then
if then

use a SWITCH control structure.

Which, however, only works for integral data types, not for strings.


Then why in this example is it being used for strings?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/statem_10.asp

EDIT:

nevermind, I see the light now :)

Share this post


Link to post
Share on other sites
I recommend making a tokenizer.
example(ugly, but it should get the idea across):

class Tokenizer {
std::deque<std::string> tokens;
int index;
public:
Tokenizer(std::string str) {
for (int i = 0; i < str.length(); i++) {
if (str != ' ') {
tokens[index][tokens[index].length()] = str;
} else {
index++;
}
}
index = 0;
}

std::string nextToken() {
index++;
return tokens[index-1];
}

void setIndex(int index) {
this.index = index;
}
};



EDIT: fixed a bug in the example

[Edited by - Roboguy on October 22, 2004 1:54:43 AM]

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!