I am new to c++ and as a project to learn the language I have been working on a console application that would be able to handle and execute commands from the user. I have gotten it to work the way I want it to however I feel that I am doing it a very inefficient way (both handling the command and parsing the input into tokens). If someone could help point me in the right direction or give me some pointers this would be greatly appreciated.
console.h
class console{
public:
console();
~console();
//this will update the console application
void update();
//these wiill be used to print to the screen
static void printl(const char* output);
static void printl(std::string output);
static void printl(int value);
static void printl(float value);
static void printl(double value);
static void print(const char* output);
static void print(std::string output);
static void print(int value);
static void print(float value);
static void print(double value);
//this will be used to take input from the screen
void getInput();
//clears the value of command
void resetCommand();
//breaks the command into its tokens
void parseCommand(std::string command);
//evaluates what it will do based on the command
int evaluate();
void exicuteCommand(int commandNum);
//this will return the first token
std::string getFirstToken();
//sets active to false
void exitConsole();
//if the console is running or not
bool isActive() const;
private:
bool active;
std::string command;
std::string tokens[256];
};
console.cpp
#define OPTIONSSIZE 1024
std::string options[OPTIONSSIZE] = {
"exit", //0 exit console
"cal", //1 perform a calculation
"clr", //2 clear the screen
"connect", //3 connect to server
"open", //4 open a file
"close" //5 close a file or program
};
console::console(){
active = true;
}
//------------------------------------------------------
//This will be a loop that out and in puts to the screen
//------------------------------------------------------
void console::update(){
while (command == ""){
getInput();
}
parseCommand(command);
exicuteCommand(evaluate());
resetCommand();
}
void console::getInput(){
std::getline(std::cin, command);
}
void console::parseCommand(std::string command){
char *myString = &command[0];
char *p = strtok(myString, " ");
int i = 0;
std::stringstream ss;
while (p) {
//gets the current token and adds it to the string stream
ss << p;
//the string stream then converts it into a std::string and adds it to the token array
ss >> tokens[i];
//it then clears the stream, resetting it
ss.clear();
//removes the token from p
p = strtok(NULL, " ");
i++;
}
}
std::string console::getFirstToken(){
return tokens[0];
}
//------------------------------------------------------
//evaluates what to do based on the tokes passed
//------------------------------------------------------
int console::evaluate(){
//default
int optionNum = -1;
//lookes through all options and compairs them to the focus command
for (int i = 0; i < OPTIONSSIZE; i++){
if (options[i] == getFirstToken()){
optionNum = i;
break;
}
}
return optionNum;
}
void console::exicuteCommand(int commandNum){
switch (commandNum){
case 0:
exitConsole();
break;
case 1:
{
Calculator cal(tokens);
}
break;
case 2:
system("cls");
break;
case 3:
printl("Connecting with server");
break;
case 4:
break;
case 5:
break;
default:
printl("Command not valid");
break;
}
}
void console::resetCommand(){
command = "";
}
void console::exitConsole(){
this->active = false;
}
bool console::isActive() const{
return this->active;
}
void console::printl(const char* output){
std::cout << output << std::endl;
}
void console::printl(std::string output){
std::cout << output << std::endl;
}
void console::printl(int value){
std::cout << value << std::endl;
}
void console::printl(float value){
std::cout << value << std::endl;
}
void console::printl(double value){
std::cout << value << std::endl;
}
void console::print(const char* output){
std::cout << output;
}
void console::print(std::string output){
std::cout << output;
}
void console::print(int value){
std::cout << value;
}
void console::print(float value){
std::cout << value;
}
void console::print(double value){
std::cout << value;
}
console::~console(){
}
int main()
int main(){
console console;
console.printl("Solution [Command Line]");
console.printl("------Version 0.1-------");
console.printl("");
while (console.isActive()){
console.print("Solution::$ ");
console.update();
}
return 0;
}