Memory Project CodeΒΆ

#include <stdio.h>
#include <stack>
#include <sstream>
#include <iostream>
#include <fstream>
using namespace std;
#include <ctype.h>
#include <string>
#include <iomanip>
#include <vector>


string codeInput()
{
    cout << "Enter the line of code: " ;
    string code;
    getline(cin,code);
    cout << endl;

    return code;
}

string purpose()
{

    cout <<"What is the purpose of this line? :";
    string purpose;
    getline(cin, purpose);
    cout << endl;

    return purpose;
}

string screen()
{
    cout <<"What does the screen show? (Output):";
    string screen;
    getline(cin,screen);
    cout << endl;

    return screen;
}

string keyboard()
{
    cout <<"What does the user enter on the keyboard? :";
    string keyboard;
    getline(cin, keyboard);
    cout << endl;

    return keyboard;
}

string stackInput()
{
    cout <<"What is the stack from bottom to top? Seperate by commas ","";
    string input;
    getline(cin, input);
    cout << endl;


    stack<int> myStack;     
    stringstream ss(input); // turn the input into a stream we can parse
    string item;            

    // Split the string by commas
    while (getline(ss, item, ',')) 
    {
        if (!item.empty() && item[0] == ' ') 
            item.erase(0, item.find_first_not_of(" "));
        if (!item.empty() && item[item.size() - 1] == ' ')
            item.erase(item.find_last_not_of(" ") + 1);

        // Convert the string to an integer
        int number = stoi(item);

        // Push the integer onto the stack
        myStack.push(number);
    }
    
    // Display the stack contents (LIFO order: last entered comes out first)
    ostringstream output;  // acts like cout, but writes into a string

    output << "\nStack:\n";
    while (!myStack.empty()) 
    {
        output << myStack.top() << "\n"; // append top to string
        myStack.pop();                   // remove top
    }
    //cout << output.str() ;
    return output.str(); // return the whole thing as a string

    //return myStack;

}
struct Identifier {
        string name;
        string type;
        string location;
    };

vector<Identifier> idTable(ofstream& outFile)
{
    cout <<"What is in the identifier table?" << endl;
    cout << endl;
    cout << "Do you need to add an identifier? (Y or N) " << endl;
    string need;
    cin >> need;
    vector<Identifier> table; // store all identifiers
    if(need != "Y")
    {
        return table;
    }
    
    while(true)
    {
        Identifier id;
        cout << "Identifier (name): ";
        cin >> id.name;
        cout << endl;

        cout << "Type: ";
        cin >> id.type;
        cout << endl;

        cout << "Location: ";
        cin >> id.location;
        cout << endl;

        table.push_back(id); // store this entry

        cout << "Do you have another identifier to add? (Y or N): ";
        string repeat;
        cin >> repeat;
        if(repeat == "N")
        {
            break;
        } 
    }

    cout << left << setw(10) << "ID (Name)"
            << setw(15) << "Type"
            << setw(10) << "Location" << endl;
            cout << string(35, '-') << endl; // separator line
    for(const auto& id: table)
    {
            cout << left << setw(10) << id.name
                << setw(15) << id.type
                << setw(10) << id.location << endl;
    }

    outFile << left << setw(15) << "ID (Name)"
            << setw(15) << "Type"
            << setw(15) << "Location" << endl;
    outFile << string(45, '-') << endl;

    for (const auto& id : table) {
        outFile << left << setw(15) << id.name
                << setw(15) << id.type
                << setw(15) << id.location << endl;
    }

    return table;
} 
struct Memory {
        string content;
        string location;
    };

vector<Memory> memory(ofstream& outFile)
{
    static vector<Memory> table;
    cout << "Do you need to add to memory? (Y or N) " << endl;
    string need;
    cin >> need;
    if(need != "Y")
    {
        return table;
    }
    

    
    Memory id;
    cout <<"What is in the memory?" << endl;;
    cout <<"Location: ";
    cin >> id.location;
    cout << endl;

    cout <<"Content: ";
    cin >> id.content;
    cout << endl;
    table.push_back(id);


    cout << left << setw(10) << "Location"
            << setw(15) << "Content" << endl;;
            cout << string(35, '-') << endl; // separator line
    for(const auto& id: table)
    {
        cout << left << setw(10) << id.location
            << setw(15) << id.content << endl;
    }

    outFile << left << setw(10) << "Location"
            << setw(15) << "Content" << endl;;
            outFile << string(35, '-') << endl; // separator line
    for(const auto& id: table)
    {
        outFile << left << setw(10) << id.location
            << setw(15) << id.content << endl;
    }

    return table;
}
int main()
{
    ofstream file("output.txt");
    if (!file.is_open()) {
        cerr << "Error opening file!" << endl;
        return 1;
    }
    while(true)
    {
        

        
        cout << "Welcome to the Memory Game" << endl << "Follow the directions and enter the information for each line of your code" << endl;
        cout << endl;

        file <<"These are your results of the Memory Game" << endl;
    
        string userCode = codeInput(); //returning code for gui output (Greyson working on his version)
        cout << endl;
        cout << userCode << endl;

        file << "Code: " << userCode << endl;


        // working: returning purpose for eventual gui output    (Greyson working on his version)
        string userPurpose = purpose();
        cout << endl;
        cout << userPurpose << endl;
        
        file << "Purpose: " << userPurpose << endl;


    // working
        string userScreen = screen();
        cout << endl;
        cout << userScreen << endl;

        file << "Output on Screen: " <<userScreen << endl;



        string userKeyboard = keyboard();
        cout << endl;
        cout << userKeyboard;
        cout << endl;

        file << "What the user enters on the keyboard: " << userKeyboard << endl;

        //working
        string userStack = stackInput();
        cout << userStack;
        cout << endl;

        file << userStack << endl;

        //idTable(cout);      // working with table output
        // idTable(file);

        vector<Identifier> myTable = idTable(file);

        vector<Memory> memoryTable = memory(file);
        // memory(cout);
        // memory(file);


        cout << "Do you have another line of code? (Y or N)" << endl;
        string repeat;
        cin >> repeat;
        if(repeat == "N" || repeat == "n" )
        {
            break;
        }
    }

}