Saturday, 24 August 2013

Whats a better way to code a command line interface in c++?

Whats a better way to code a command line interface in c++?

I'm using run of the mill C++, and im building a small "Zork-esc" game as
a pet project to exercise my newly learned C++ skills. the following code
works perfectly, however it will be a pain to have to do this for every
command/argument combo so if anybody out there can save me some trouble
then please do :D ...
as it stands i have this function responsible for processing commands at
run-time...
void execute()
{
//###### Command builder ######
cout << ">>>";
char input[100];
cin.getline(input, sizeof(input));
cout << input << endl;
string newin = input;
//###### Execution Phase ######
if(newin=="derp"){
// normally functions go here
cout << "You did it :D" << endl;
}else if(newin=="attack player1 player2"){
// normally functions go here
cout << "player1 attacks player2" << endl;
}else{
// quriky error message for the user
cout << "dafuq?" << endl;
}
execute();
}
it takes your input and compares it to all the possible string
combinations and when it finds a match its suppose to run the
corresponding functions placed inside the IF staments
like i said it works but there is much room for improvement i feel...

No comments:

Post a Comment