hi, I am trying to take a command input by the user and continue looping until the user types exit
within the loop there are 5 keywords which will send throw control elsewhere
it works the first time through
i think the problem is that the strings holding the input are not cleared on subsequent iterations
i have tried various things with no success
could someone offer me some suggestions on how to fix this?
or
is there (i'm sure there is) a better way in which a beginner (me) can do this with the most basic of skills.
Thanks
Code:
while(true)
{
string str, cmd, titl; //str entire input
int loc; //cmd first word - determines where to send
cout << "Enter command: "; //titl text after cmd
getline(cin, str);
loc = str.find(" ", 0);
cmd.append(str,0,loc);
titl.append(str,loc,titl.size()-1);
//cout << "1st word: " << cmd << endl << "text: " << titl <<endl;
char c_cmd1[80]; //convert string to lower
const char *c_cmd = cmd.c_str(); // c_cmd(c_style string)
strncpy(c_cmd1, c_cmd, 79); // c_cmd1 lowercase
strlwr(c_cmd1); // c_cmd2 is
string c_cmd2(c_cmd1); //converted back to string class
//cout << c_cmd << endl << c_cmd1 << endl << c_cmd2 << endl;
if(c_cmd2 == "took")
took();
//pass to took function, send string title
else if(c_cmd2 == "clear")
clear();
//pass to clear function, send string title
else if(c_cmd2 == "list")
list();
//pass to list function
else if(c_cmd2 == "advice")
advice();
//pass to advice function
else if(c_cmd2 == "exit")
break;
else huh();
}
//Function Took
void took()
{
cout << "took" << endl;
}
//Function Clear
void clear()
{
cout << "clear" << endl;
}
//Function List
void list()
{
cout << "list" << endl;
}
//Function Advice
void advice()
{
cout << "advice" << endl;
}
void huh()
{
cout << "Huh??" << endl;
}