I am trying to write a command line interface for a school project. I have a driver.cpp to run the command line and that parses the commands into an array of strings for the commandInterpreter. When I try to debug it looks like only the first string is getting passed when I call interpreter->interpret(rowData, length); If I type "import p filename" in the command line, it looks like args only contains the string "import". I am expecting it to be passed {"import", "p", "filename"}. Am I passing or accessing the array of strings incorrectly?
Driver.cpp
#include "CommandInterpreter.h"
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
CommandInterpreter * interpreter = new CommandInterpreter();
while ( !interpreter->terminate() )
{
string cmd;
getline(cin, cmd);
//Parse command args to lowercase
int i = 0;
while(cmd[i])
{
char c = cmd[i];
putchar(tolower(c));
i++;
}
//Parse command line into command and parameters.
//Space delimited.
stringstream ss(cmd);
string item;
string * rowData = new string[100];
int length = 0;
while (getline(ss, item, ' ')) {
rowData[length] = item;
length++;
}
//Interpret command
interpreter->interpret(rowData, length);
//Print feedback
cout << interpreter->feedback() << '\n';
}
delete interpreter;
return 0;
}
CommandInterpreter.h
#ifndef __COMMANDINTERPRETER_H
#define __COMMANDINTERPRETER_H
#include <string>
#include "ProjectController.h"
#include "LocationController.h"
#include "VolunteerController.h"
#include "ScheduleGenerator.h"
#include "VolunteerScheduler.h"
class ProjectController;
class LocationController;
class VolunteerController;
class ScheduleGenerator;
class VolunteerScheduler;
using namespace std;
class CommandInterpreter
{
public:
CommandInterpreter(void);
~CommandInterpreter(void);
//---------------------------------------------------------------------
// Tells the driver when to teminate the program.
//---------------------------------------------------------------------
bool terminate(void);
//---------------------------------------------------------------------
// Interprets the given array of commands.
//---------------------------------------------------------------------
bool interpret(string * commands, int length);
//---------------------------------------------------------------------
// Provides feedback for the last command run.
//---------------------------------------------------------------------
string feedback(void);
private:
bool exitFlag;
string handback;
ProjectController * PC;
LocationController * LC;
VolunteerController * VC;
ScheduleGenerator * SG;
VolunteerScheduler * VS;
public:
//---------------------------------------------------------------------
// Interprets a save command.
//---------------------------------------------------------------------
bool save(char arg0);
//---------------------------------------------------------------------
// Interprets an import command
//---------------------------------------------------------------------
bool import(char arg0, string filename);
//---------------------------------------------------------------------
// Interprets a print command.
//---------------------------------------------------------------------
bool print(char arg0, char arg1);
//---------------------------------------------------------------------
// Interprets a schedule command.
//---------------------------------------------------------------------
bool schedule(char arg0);
//---------------------------------------------------------------------
// Interprets a help command. Provides a list of commands in feedback.
//---------------------------------------------------------------------
bool help(void);
};
#endif
CommandInterpreter.cpp
#include "CommandInterpreter.h"
CommandInterpreter::CommandInterpreter(void)
{
exitFlag = false;
handback = "";
PC = new ProjectController();
LC = new LocationController();
VC = new VolunteerController();
SG = new ScheduleGenerator();
VS = new VolunteerScheduler();
}
CommandInterpreter::~CommandInterpreter(void)
{
delete PC;
delete LC;
delete VC;
delete SG;
delete VS;
}
bool CommandInterpreter::terminate(void)
{
return this->exitFlag;
}
bool CommandInterpreter::interpret(string * args, int length)
{
//args should be an array of strings like {"import","p","filename"}
//Debug only shows "import"
string cmd = args[0];
handback = "";
if(cmd == "exit")
{
exitFlag = true;
}
else if(cmd == "save")
{
this->save(args[1][0]);
}
else if(cmd == "import")
{
this->import(args[1][0], args[2]);
}
else if(cmd == "print")
{
if(length == 2)
this->print(args[1][0], '0');
else
this->print(args[1][0], args[2][0]);
}
else if(cmd == "schedule")
{
this->schedule(args[1][0]);
}
else if(cmd == "help")
{
this->help();
}
else
{
this->handback = "Invalid Command. Type help to see commands.";
return false;
}
return true;
}
string CommandInterpreter::feedback(void)
{
return this->handback;
}
bool CommandInterpreter::save(char arg0)
{
bool success = true;
switch (arg0)
{
case 'p':
// PC->save();
handback = "Saved project schedule.";
break;
case 'v':
// VC->save();
handback = "Saved volunteer schedule.";
break;
default:
// Uh-oh
handback = "Invalid argument for save: " + arg0;
success = false;
break;
}
return success;
}
bool CommandInterpreter::import(char arg0, string filename)
{
bool success = true;
switch (arg0)
{
case 'p':
PC->importCSV(filename);
handback = "Imported projects file: " + filename;
break;
case 'l':
LC->importCSV(filename);
handback = "Imported locations file: " + filename;
break;
case 'v':
VC->importCSV(filename);
handback = "Imported volunteers file: " + filename;
break;
default:
success = false;
handback = "Invalid argument for import command: " + arg0;
break;
}
return success;
}
bool CommandInterpreter::print(char arg0, char arg1)
{
bool success = true;
switch (arg0)
{
case 'p':
// PC->print()
// or project schedule print
// depending on arg1
if(arg1 == '0')
handback = PC->getProjectStrings();
else
; //Print project schedule here.
break;
case 'l':
// LC->print()
break;
case 'v':
// VC->print()
// or volunteer schedule print
// depending on arg1
break;
default:
success = false;
handback = "Invalid argument for print command: " + arg0;
break;
}
return success;
}
bool CommandInterpreter::schedule(char arg0)
{
bool success = true;
switch (arg0)
{
case 'p':
// SG->generate()
handback = "Project schedule generated.";
break;
case 'v':
// VS->generate()
handback = "Volunteer schedule generated.";
break;
default:
success = false;
handback = "Invalid argument for schedule command: " + arg0;
break;
}
return success;
}
bool CommandInterpreter::help(void)
{
return false;
}
new? Did somebody tell you to do that?argsis a variable, of typestd::string*. I doubt it contains any string.