1

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;
}
user3536870
  • 249
  • 1
  • 2
  • 13
  • 3
    What kind of menu? Like a GUI with buttons, etc? Or text based through the command line? – Cory Kramer Apr 21 '14 at 13:55
  • Sorry I'm very new to this so I don't know what GUI buttons are but I meant just a simple menu where it gives a list of 4-5 operations and then you enter #1 #2 e.t.c so then the program performs that function. Hope that made sense? – user3536870 Apr 21 '14 at 13:59
  • If you were to do this "properly", I'd recommend using std::function. However, it seems like you're still just learning, so I'd stick with a few simple Ifs to do this if I were you. – Ben Apr 21 '14 at 14:01
  • @user3536870: give it only once in the beginning after the file selection or constantly in a loop? Please specify your use case. – László Papp Apr 21 '14 at 14:03
  • Okay so when I use 'ifs' do I just carry on from the end of my code that I already have and go from there? @Laszlo Papp Oh and in a loop! – user3536870 Apr 21 '14 at 14:03

1 Answers1

2

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...

László Papp
  • 51,870
  • 39
  • 111
  • 135