1

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?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
supermage
  • 31
  • 4
  • 3
    You'll be glad to hear you don't need anyone's help to figure this out, just a tool you already have: your debugger! This is exactly what a debugger is for. It [runs your program, one line at a time, and shows you what's happening](https://stackoverflow.com/questions/25385173/), this is something that's every C++ developer must know how to do. With your debugger's help you'll able to quickly find all problems in this and all future programs you write, without having to ask anyone for help. Have you tried using your debugger, already? If not, why not? What did your debugger show you? – Sam Varshavchik Sep 25 '22 at 23:05
  • There are many things wrong with this code or which do not make any sense. Just scrolling through randomly, for example, I stumbled upon `else if (input == "show"||"show()")`, which does not do what you presumably expect it to do. – Karl Knechtel Sep 25 '22 at 23:07
  • @SamVarshavchik thank you for your response! I wasn't aware of how to use a debugger but I just figured it out. I will make use of this in the future. – supermage Sep 25 '22 at 23:19
  • @KarlKnechtel Thank you for the pointers. I was trying to include as much information about the program as I could but the minimal reproducible example makes sense. And I meant to rephrase that question so it was a bit misleading, I should have phrased it as "I am trying to pass a value from one function to another" rather than passing by reference. I know the difference. – supermage Sep 25 '22 at 23:22
  • *"I cannot figure out how to pass a variable by reference into another function."* -- that is WAY too much code to demonstrate this. And you can skip all that background information. You have a specific question, which is good. Now focus on it. What code do you need to demonstrate your question? Probably a variable to pass and a function to receive. So if you start with `void read(string file) {} int main() { string input; read(); }`, what more do you need to ask your question? (Note: I intentionally omitted the return value and enclosing structure, for simplicity.) – JaMiT Sep 26 '22 at 03:37

0 Answers0