some problems with splitting words and variables.

This is a discussion on some problems with splitting words and variables. within the C++ Programming forums, part of the General Programming Boards category; I have 2 problems, the code posted here is a commandline to a mud, it works, but not as good ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    25

    some problems with splitting words and variables.

    I have 2 problems, the code posted here is a commandline to a mud, it works, but not as good as it should. is there any way to make it better? some better way to split up the words into diferent variables? I sometimes, not allways, get an error massage after I run the program with shows me some hex codes which I dont understand. its mostly only when I type one word, like "time" or "exit" how can these accur sometimes and not allways?
    to my second problem I wonder how I can make an "if" detect if there is written anything in a variable so I can make it print eighter the variable or a text that for instence says "nothing.." if anyone bothers to read the code and give me some pointers I apriciate it:P thx.

    the commandline code:
    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <dos.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    #include <time.h>
    
    
    /*functions*/
    void basic();
    void kom(char per[100]);
    void get();
    void wear();
    void time();
    
    
    /*GLOBALE variables*/
    char first[20], second[20], third[20];
    
    
    /*main funktion*/
    int main(void)
    {
    
    
    	char per[100];
       start:
    	cout 	<< "\nYour command? ";
       cin.get(per,100); cin.ignore();
       kom(per);
       goto start;
    }
    
    /*splits up the word into variabels*/
    void kom(char per[100])
    {
       int a=0, c=0;
       do{
       	if  (per[c] == ' ') 	{break;}
          else 	{
    				first[a] = per [c];
                a++;
          		}
          c++;
         }
       while (1);
       c++;
       a=0;
       do{
       	if  (per[c] == ' ') 	{break;}
          else 	{
    				second[a] = per [c];
                a++;
          		}
          c++;
         }
       while (1);
       c++;
       a=0;
       do{
       	if  (per[c] == ' ') 	{break;}
          else 	{
    				third[a] = per [c];
                a++;
          		}
          c++;
         }
       while (1);
       basic();
    }
    
    void basic()
    {
    getch();
    
    
    			if (!strcmp(first, "wear")) 		{wear();}
    	else 	if (!strcmp(first, "get"))			{get();}
       else 	if (!strcmp(first, "exit")) 		{exit(1);}
       else 	if (!strcmp(first, "time"))		{time();}
    	else 												{cout << "\n What? Who? When!?";}
    
    }
    
    /*checks the second word and exacutes the command*/
    void wear()
    {
    		  if	(!strcmp(second, "shirt")) 	{cout << "\nYou wear the ragget old shirt";}
       else if 	(!strcmp(second, "pants")) 	{cout << "\nYou wear the pants";}
       else if 	(!strcmp(second, "caps")) 		{cout << "\nYou wear the red caps";}
       else if 	(!strcmp(second, "boots"))		{cout << "\nYou wear the smooth, black boots";}
       else 												{cout << "\nYou dont have that to wear!";}
       getch();
    }
    
    /*checks the second word and exacutes the command*/
    void get()
    {
       if 		(!strcmp(second, "shirt"))		{cout << "\nYou get a ragget old shirt";}
       else if 	(!strcmp(second, "pants")) 	{cout << "\nYou get tha pants";}
       else if 	(!strcmp(second, "caps")) 		{cout << "\nYou get the red caps";}
       else if 	(!strcmp(second, "boots")) 	{cout << "\nYou get a pair of smooth, black boots.";}
       else 										  		{cout << "\nYou cant see that item anywhere";}
       getch();
    }
    
    /*prints the time and date*/
    void time()
    {
      time_t t;
      time(&t);
      cout << "Today's date and time: " << ctime(&t);
    }

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,800
    First off:
    1. Don't use <iostream.h> use <iostream> instead the put everything in the std namespace by puting using namespace std; after all your #includes also use <cstdlib> instead of <stdlib.h> and <cstring> instread of <string.h>
    2. Don't use that goto use a loop instead

    Why exactly do you need to split up the words?
    I would try to compile and read your code but the spacing is horrific it could be the board though so I won't focus to much on that.
    But if you are just doing a word compare just do something like this
    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    void run()
    {
    
      cout<<"Ya I am running"<<endl;
    
    }
    
    int main(void)
    {
    
      char input[20];
      bool exit = false;
      while(!exit)
      {
    
        cout<<"Enter a command"<<endl
        cin>>input;
    
        if(strcmp(input,"run") == 0)
        {
    
           run();
    
        }
    
        if(strcmp(input,"exit") == 0)
        {
    
          exit = true;
    
        }
    
      }
      
    }
    Woop?

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    25
    yeah.. thx for the pointers but what I need is not a single word compare that would make it easy. but I need some system so that when you type "get shirt" it checked first word "get" and then what to get in the second word.. and maby allso a third word.. and some with only one word.. I know the code looks strange but thats the webpage.. it doesnt look like that in boland or visual studio.
    and maybe I should learn that namspace thing:P we dont learn it at school and its not in my c++ book so I should probobly look it up on the net.
    thx tho.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,672
    If I understand what you are trying to do, use a stringstream. As an example:

    Code:
    #include <string>
    #include <sstream>
    #include <iostream>
    
    using namespace std;
    
    void look(stringstream& input)
    {
        string temp;
    
        input >> temp;
    
        if( temp.length() )
            cout << "You are looking at " << temp << endl;
        else
            cout << "You are looking at nothing!" << endl;
    }
    
    void eat(stringstream& input)
    {
        string temp;
    
        input >> temp;
    
        if( temp.length() )
            cout << "You are eating " << temp << endl;
        else
            cout << "You are starving!" << endl;
    }
    
    void process(stringstream& input)
    {
        string temp;
    
        input >> temp;
    
        if( temp.length() && temp == "look" )
            look(input);
        else if( temp.length() && temp == "eat" )
            eat(input);
        else if( temp.length() )
            cout << "Command not understood: " << temp << endl;
        else
            cout << "Nothing to do!";
    }
    
    int main()
    {
        string command;
    
        cout << "Enter your command: ";
        getline(cin,command);
    
        while( command.length() )
        {
            process(stringstream(command));
    
            cout << "Enter your command: ";
            getline(cin,command);
        }
    
        cout << "Goodbye!" << endl;
    
    }
    Possible input/output:
    Code:
    Enter your command: make
    Command not understood: make
    Enter your command: eat
    You are starving!
    Enter your command: look
    You are looking at nothing!
    Enter your command: eat monkey
    You are eating monkey
    Enter your command: look cloud
    You are looking at cloud
    Enter your command: 
    Goodbye!
    I used to be an adventurer like you... then I took an arrow to the knee.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    25
    Thanks.. thats what I needed and so much more simple to handle then my program which the compiler dont like.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  2. Cleaning variables for a CGI app (as a derived class?)
    By drrngrvy in forum C++ Programming
    Replies: 1
    Last Post: 02-01-2006, 11:34 AM
  3. Replies: 14
    Last Post: 01-12-2006, 04:08 AM
  4. help, i cant count spaces or words
    By cdosrun in forum C Programming
    Replies: 2
    Last Post: 11-25-2005, 02:13 PM
  5. how to extract words from a string
    By panfilero in forum C Programming
    Replies: 7
    Last Post: 11-04-2005, 07:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21