Okay, my problem is I can't figure out how to do this... I want to take a string and parse through it, taking out all singular words, seperated from other words by spaces.
so
NORTH SOUTH UP DOWN
And the program would be able to take out and recognize NORTH, SOUTH, UP, and DOWN as individual words and make actions based on those. Here is some code, messy code...:
( sorry for making the class all public, but I'm just doing it for test purposes )
Code:
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
class Test
{
public:
string usercmd;
Test (string u) : usercmd(u) { }
void disp (void)
{
cout << usercmd << endl;
}
void set_caps (int pos = 0);
int get_sloc (int pos = 0);
void translate_cmd (void);
void tran_to_dat (string &t, int l);
};
void Test::translate_cmd (void)
{
set_caps();
int fs = 0;
int ns = get_sloc();
string sub;
int loc = 0;
for (loc = 0; ; ++loc)
{
sub = usercmd.substr(fs, ns);
tran_to_dat (sub, loc);
fs = ns + 1;
ns = get_sloc(fs);
if (fs > usercmd.size() )
break;
}
}
void Test::tran_to_dat (string &t, int l)
{
int val = -1;
if (t == "NORTH")
val = 256;
if (t.substr(0, t.size()) == "SOUTH")
val = 255;
if (t == "WOWOW")
val = 512;
cout << "pos: " << l << " ~" << t << "~ ";
cout << val << endl;
Sleep(1500);
}
int Test::get_sloc (int pos) // find first space from pos
{
++pos; // increase pos to find correct space
for (int i = pos; i < usercmd.size(); ++i)
{
if (usercmd[i] == ' ')
break;
}
return i;
}
void Test::set_caps(int pos) // turn string to all-caps
{
for (int i = pos; i < usercmd.size(); ++i)
{
if ( (int)usercmd[i] > 96 && (int)usercmd[i] < 123 && usercmd[i] != ' ')
usercmd[i] = usercmd[i] - 32 ;
}
}
int main()
{
Test MyTest("North South Up Down");
MyTest.disp();
MyTest.translate_cmd();
return(0);
}
run this code and you'll notice it parses it as NORTH (first word is correct), SOUTH UP DO (incorrect), UP DOWN (incorrect), and DOWN (correct, but...)
Any suggestions on how to get it working? I appreciate any help... = ]