2D arrays as well as switch

I cannot seem to get my switch to display the function it calls in each case. Any suggestions on the mini and maxi? This has to be the minimum number and the maximum number. Any help appreciated, thanks.

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

const int ROWS = 10;
const int COLS = 10;
//********************
//Function Prototype
//********************
void fillMatrix (int matrix[][COLS], int &row, int &col);
void printMatrix (int matrix[][COLS], int row, int col);
int Menu (int matrix[][COLS], int row, int col, int avg, int mini, int maxi);
void printDiag (int matrix[][COLS], int row, int col);
void getSum (int matrix[][COLS], int row, int col);
void getStats (int matrix[][COLS], int row, int col, int &avg, int &mini, int &maxi);
//********************
//Main Program Driver
//********************

int main ()
{
int matrix [ROWS][COLS]; //definging 2d array
int row=0, col=0, avg=0, mini=0, maxi=0;
int choice;

do{
Menu (matrix,row,col,avg,mini,maxi);;
switch (choice)
{
case 0:
case 1:
fillMatrix ( matrix, row, col);
break;
case 2:
printMatrix (matrix, row, col);
break;
case 3:
printDiag (matrix, row, col);
break;
case 4:
getSum (matrix, row, col);
break;
case 5:
getStats (matrix, row, col, avg, mini, maxi);
break;
}
} while (choice!=0);

return 0;
}
//********************
//Function Definitions
//********************
void printMatrix (int matrix[][COLS], int row, int col)
{
for (int r=0; r<row; r++)
{
for (int c=0; c<col; c++)
cout << setw(5) << matrix[r][c];
cout << endl;
}
}

void fillMatrix (int matrix[][COLS], int &row, int &col)
{
ifstream input("p8in.txt");
if (input.fail())
{
cout << "File not found." << endl;
exit(1);
}
input >> row >> col;
for (int r=0; r<ROWS; r++)
for (int c=0; c<COLS; c++)
input >> matrix[r][c];

input.close();
}

void printDiag (int matrix[][COLS], int row, int col)
{
if (row==col)
for (int r=0; r<row; r++)
{
for (int c=0; c<col; c--)
cout << setw(4) << matrix[r][c];
cout << endl;
}
}


void getSum (int matrix[][COLS], int row, int col)
{
int sum=0; //sum varaible used to calculate the sum
for (int r=0; r<row; r+1) //of the numbers inputed into the matrix table
{
for (int c=0; c<col; c+1)
sum += matrix[r][c];
}
cout << setw(4) << "Sum: " << sum << endl;
}


void getStats (int matrix[][COLS], int row, int col, int &avg, int &mini, int &maxi)
{
int sum=0;
for (int r=0; r<row; r++)
{
for (int c=0; c<col; c++)
sum += matrix[r][c];
}
avg = sum /row * col;
//mini = //lowest sum or number
//maxi = //highest sum or number
}

int Menu (int matrix[][COLS], int row, int col, int avg, int mini, int maxi)
{
int choice; //varaible used to allow user to pick which menu choices for user
cout << "0: Quit."<<endl;
cout << "1: Fill the matrix. *"<<endl;
cout << "2: Print the matrix. *"<<endl;
cout << "3: Prin the diagonal. *"<<endl;
cout << "4: Get the sum. *"<<endl;
cout << "5: Get the stats. *"<<endl;
cout << "* * * * * * * * * * * *"<<endl;
cout << "What funciton you would like to use?"<<endl;
cin >> choice;
}
Your problem is that int choice; is defined in Menu()
And you are reading it in main()
You should either pass it as a reference, return it's value or make it a global variable.
@zoran404 after I have adjusted this issue how should I proceed to make the functions called during the switch, display after selecting the appropriate case number?

Thanks,
vstexpert
Last edited on
Topic archived. No new replies allowed.