I am working on a program that will interpret PySub. The current problem I am working on now is a menu for the user to navigate. The user must be able to enter 'help' and the program would return a help menu with information for each possible command, 'read' which would then read a .py file, 'show' which would return the information from the .py file, and 'clear' which would clear the information from the screen.
I have already done the help, show, and read functions, but I cannot figure out how to pass a variable by reference into another function.
Here is my code for the .cpp file:
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include "interface.h"
using namespace std;
void Interface::startInterface()
{
do
{
std::cout << "PySUB Interpreter 1.0 on Windows (September 2022)" << endl;
std::cout << "Enter program lines or read(<filename>.py) at command line interface" << endl;
std::cout << "Type 'help' for more information or 'quit' to exit" << endl;
std::cin >> input;
if (input.back() == ')') //this checks to see if the user input is a function. the idea here is that if it is a function, there will be a right-side paren.
{
for (int i = 0; i < input.length(); i++)
{
if (input[i] == '(') //checks for an opening parenthesis
{
int k = i + 1;
if (input[k] != ')') //makes sure that there isn't a closing paren. directly after opening paren.
{
for (k; k < input.length() - 1; k++) //loop that continues iterating after the opening paren. and stops just before the last character, which is a closing paren.
{
command.push_back(input[k]); //pushes characters into string variable to be passed into other functions.
}
}
}
}
for (int j = 0; j < command.length(); j++)
{
if (command[j] == '.') //checks to see if command string is a filetype. a filetype will include a '.'
{
file = command; //if command is a filetype, file is now command and file will be passed to the read function.
}
} //if command is a filetype, verify that the first 4 characters of input is equal to read
for (int l = 0; l < input.length(); l++)
{
if (input[l] == '(')
{
for (int m = 0; m < l; m++)
{
readFunction.push_back(input[m]);
}
}
}
if (readFunction == "read")
{
read(file);
}
}
if (input == "help")
{
help();
}
else if (input == "show"||"show()")
{
show(fileVec);
}
} while (input != "quit");
}
void Interface::help()
{
{
string input;
std::cout << ">>> Help" << endl;
std::cout << "Welcome to the help utility!" << endl;
std::cout << "* To exit and return to the interpreter, type 'exit" << endl;
std::cout << "* To get a list of commands, type 'commands'" << endl;
std::cout << "* To get a description of any command, just type the command at the help> prompt" << endl;
do
{
std::cout << "help>";
std::cin >> input;
if (input == "commands")
{
std::cout << "Below is a list of commands. Enter any command at the promp to get more help" << endl;
std::cout << " " << endl;
std::cout << "clear help quit" << endl;
std::cout << "read show" << endl;
std::cout << " " << endl;
std::cout << "NOTE: All commands can also be entered as functions:" << endl;
std::cout << " " << endl;
std::cout << " " << endl;
std::cout << "clear() help() quit()" << endl;
std::cout << "read() show()" << endl;
}
else if (input == "clear" || input == "clear()")
{
std::cout << "This command clears out or deletes any lines that are stored in the program data structure." << endl;
}
else if (input == "help" || input == "help()")
{
std::cout << "This command will enter the help utility." << endl;
}
else if (input == "quit" || input == "quit()")
{
std::cout << "This command exits the command line interpreter." << endl;
}
else if (input == "read" || input == "read()")
{
std::cout << "This command reads a file to be interpreted." << endl;
}
else if (input == "show")
{
std::cout << "This command shows the results from a file that was read. This must be used in conjunction with the 'read' command." << endl;
}
} while (input != "exit");
}
}
void Interface::show(vector<string> fileVec)
{
for (auto& element : fileVec)
{
cout << element << endl;
}
}
vector<string> Interface::read(string file)
{
ifstream ofile;
string line;
ofile.open(file);
vector<string> fileVec;
if (ofile.is_open())
{
string lines;
while (getline(ofile, line))
{
fileVec.push_back(line);
}
ofile.close();
}
return fileVec;
}
void Interface::clear()
{
}
Here is my code for the header file:
#ifndef INTERFACE_H
#define INTERFACE_H
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
class Interface
{
friend class Interface;
public:
void startInterface();
void quit();
void help();
void show(std::vector<std::string> fileVec);
std::vector<std::string> read(std::string file);
void clear();
private:
typedef std::vector<std::string> programType;
programType programCode;
std::vector<std::string> fileVec;
std::string input, command, file, readFunction;
};
#endif
And here is the code for the main file:
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include "interface.h"
using namespace std;
int main()
{
Interface pySubInterpreter;
// Start the interface
pySubInterpreter.startInterface();
return 0;
}
Basically, what I am having a problem with is that the program reads user input, and if the user wants to open a file, it reads the information stored within the parentheses in the command 'read(someValue.py)' and pass it into a string, which would then pass it to the "read" function. The function would return the contents of the file in a vector, and when the user inputs "show()", the vector would then be passed into the "show" function. But when I run the code it won't return any values. How do I fix this?