Pregunta

below is the code I have so far which works, my question is how do I then go from here to include a menu so operations can be performed on the matrices that are read from the text files?

Examples for operations could be the determinant, add, subtract e.t.c.

#include <iostream>  //declaring variables
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;
string code(string& line);
int main()
{
    int MatrixA[3][3] = {{1,0,0},{0,2,0},{0,0,3}};

    ofstream outf;
    ifstream myfile;
    string infile;
    string line;
    string outfile;

    cout << "Please enter an input file (A.txt) for Matrix A or (B.txt) for Matrix B" << endl;
    cin >> infile;   //prompts user for input file

    if (infile == "A.txt")
    {      //read whats in it and write to screen

        myfile.open("A.txt");
        cout << endl;
        while (getline (myfile, line))
        cout << line << endl;

        //float elements[9];
        //ifstream myfile("A.txt");
        //myfile >> elements[0] >> elements[1] >> elements[2];
        //myfile >> elements[3] >> elements[4] >> elements[5];
        //myfile >> elements[6] >> elements[7] >> elements[8];

        //myfile.close();


    }
    else
        if (infile == "B.txt")
        {
            myfile.open("B.txt");
            cout << endl;
            while (getline (myfile, line))
            cout << line << endl;
        }
        else
    { 
        cout << "Unable to open file." << endl;
    }
        //{
            //while("Choose next operation");

        //}


    return 0;
}
¿Fue útil?

Solución

You could write something like this by using enums and switch statement:

enum options { Operation1, Operation2, Operation3 };

cout << "Operations:\n\n";
cout << "Operation1 - " << Operation1 << "\n";
cout << "Operation2 - " << Operation2 << "\n";
cout << "Operation3 - " << Operation3 << "\n";
cout << "Your choice: ";
int choice;
cin >> choice;

switch (choice)
{
case Operation1:
    cout << "You picked Operation1.\n";
    break;
case Operation2:
    cout << "You picked Operation2.\n";
    break;
case Operation3:
    cout << "You picked Operation3.\n";
    break;
default:
    cout << "You made an illegal choice.\n";
    break;
}

Once that is done, you could put it into a while/for loop wherever you need to do that, and quit whenever you need to do that...

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top