prossesing string input in c++

Started by
5 comments, last by Roboguy 19 years, 6 months ago
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
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
__________Michael Dawson"IRC is just multiplayer notepad." - Reverend
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
_______________________________ ________ _____ ___ __ _`By offloading cognitive load to the computer, programmers are able to design more elegant systems' - Unununium OS regarding Python
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;}
Kam-Hung Sohhttp://softwaresalariman.blogspot.com
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.
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 :)
__________Michael Dawson"IRC is just multiplayer notepad." - Reverend
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]

This topic is closed to new replies.

Advertisement