Exception handling in c++ how do I implement it here - c++

I just created a program in c++ about adding matrices but I just dont know how to exception handle the part where to choose a number from 1 to 10 so that the user can only choose a number from 1 to 10 and if he puts a wrong input in it shows an error message and is asked to input a number again
#include <iostream>
using namespace std;
int r, c, a[10][10], b[10][10], sum[10][10], i, j;
void matrix(string s) {
cout << "Enter number of rows (between 1 and 10): ";
cin >> r;
cout << "Enter number of columns (between 1 and 10): ";
cin >> c;
}
void storeValues(string s) {
cout << endl << "Enter elements of " << s << " matrix:" << " " << endl;
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
cout << "Enter element " << i + 1 << j + 1 << " : ";
if (s == "1st") {
cin >> a[i][j];
}
else
cin >> b[i][j];
}
}
void addMatrices() {
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
sum[i][j] = a[i][j] + b[i][j];
}
void displayResult() {
cout << endl << "Sum of two matrix is: " << endl;
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
cout << sum[i][j] << " ";
if (j == c - 1)
cout << endl;
}
}
int main()
{
string s = "1st";
matrix(s);
storeValues(s);
s = "2nd";
storeValues(s);
addMatrices();
displayResult();
return 0;
}

You could easily make a function that handles bounded input for you, something like:
int bounded_input(int lower, int upper, std::string message) {
int r;
bool valid = false;
cout << message << " (between " << lower << " and " << upper << "): ";
do {
cin >> r; // NOTE: You will want to check for invalid input like "three" ...
if (r < lower || r > upper) {
cout << "Invalid, please enter between " << lower << " and " << upper << ":";
} else {
valid = true;
}
} while (!valid);
return r;
}
Then you can simply call this for all of your bounds:
int row_bound = bounded_input(1, 10, "Enter row bounds");

Related

Showing values in an array

I have been coding another programming challenge from a book. It is about asking a user to input n numbers inside an array. Every time the user inputs, the current numbers entered should show up. Numbers less than or equal to zero should not be accepted. I have managed to do the first condition. However, it even shows the "empty" slots. I have tMy code will explain it. Here:
#include <iostream>
using namespace std;
int main() {
int size = 0, input_num [100], i, j;
cout << "Enter an array size: ";
cin >> size;
if (size <= 0) {
while (size <= 0) {
cout << "Enter an array size again: ";
cin >> size;
}
}
cout << "\n\nYou may now enter " << size << " numbers ";
cout << "\n------ ------ ------ ------\n";
for (i = 0; i < size; i++) {
cout << "Enter a number: ";
cin >> input_num [i];
cout << "\nEntered Numbers: ";
for (j = 0; j < size; j++) {
cout << input_num [j] << " " ;
}
cout << "\n";
}
}
In your output section, it should be j <= i, not j < size.
cout << "\nEntered Numbers: ";
for (j = 0; j <= i; j++) {
cout << input_num [j] << " " ;
}
cout << "\n";

The output is not right when the number of rows and columns do not match

The col-sum and tranpose functions work perfectly fine when i the numbers of rows and columns match but then it does not work when one differs from another
#include<iostream>
using namespace std;
// protype
void showcases();
void accept(int Array[][40], int& a, int& b);
void outputs(int Array[][40], int& a, int& b);
int sum(int Array[][40], int& a, int& b);
void row_sum(int Array[][40], int& a, int& b);
void col_sum(int Array[][40], int& a, int& b); /// something wrong with this function. test it.
void transposed(int Array[][40], int& a, int& b); /// something wrong with this function. test it.
int main()
{
// variable declaration
int a = 0, b = 0, choice = 0, A[20][40], n = 0, x = 0, y = 0, z = 0;
// For loop execution
for (int k = 0; choice != 7; k++)
{
//To showcase the given options
showcases();
//input the user choice
cin >> choice;
cout << "----------------------------------------------" << endl;
/// if else ? you don't like them? + the unreasonable empty lines between the ifs
//To perform a certain operation when a number is selected
if (choice == 1)
{
accept(A, a, b);
cout << "----------------------------------------------" << endl << endl; /// remove this
}
else if (choice == 2)
{
outputs(A, a, b);
cout << "----------------------------------------------" << endl << endl; /// remove this
}
else if (choice == 3)
{
cout << "Matrix elements sum = " << sum(A, a, b) << endl;
cout << "----------------------------------------------" << endl << endl; /// remove this
}
else if (choice == 4)
{
cout << "The sum of matrix rows are as following: -" << endl;
row_sum(A, a, b);
cout << "----------------------------------------------" << endl << endl; /// remove this
}
else if (choice == 5)
{
cout << "The sum of matrix rows are as following: -" << endl;
col_sum(A, a, b);
cout << "----------------------------------------------" << endl << endl; /// remove this
}
else if (choice == 6)
{
cout << "The transpose of the Matrix: " << endl;
transposed(A, a, b);
cout << "----------------------------------------------" << endl << endl; /// remove this
}
else if (choice == 7)
{
cout << "The application will exit now!!" << endl;
cout << "----------------------------------------------" << endl << endl; /// remove this
return -1; /// remvoe this
}
else if (choice >= 8 || choice <= 0)
{
cout << "Your choice is invalid" << endl;
cout << "----------------------------------------------" << endl << endl;
}
/// add something here. figure it out
/// add something here. figure it out
}
/// add something here. figure it out
system("pause");
return 0;
}
// To input the elements
void accept(int Array[][40], int& a, int& b)
{
cout << "Enter number of rows for matrix: ";
cin >> a;
cout << "Enter number of columns for matrix: ";
cin >> b;
cout << endl;
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{
cout << "Enter elements of matrix " << "[" << i + 1 << "]" << " [" << j + 1 << "]: ";
cin >> Array[i][j];
}
}
}
// TO output the elements
void outputs(int Array[][40], int& a, int& b)
{
for (int i = 0; i < a; i++)
{
cout << "[";
for (int j = 0; j < b; j++)
{
cout << Array[i][j];
if (j != b - 1)
cout << " ";
else if (j == b - 1)
cout << "]";
}
cout << endl;
}
}
// To find the total sum
int sum(int Array[][40], int& a, int& b)
{
int s = 0, i, j;
for (i = 0; i < a; i++)
{
for (j = 0; j < b; j++)
{
s += Array[i][j];
}
}
return s;
}
// To find the row sum
void row_sum(int Array[][40], int& a, int& b)
{
int row_s = 0;
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{
row_s += Array[i][j];
}
cout << "R" << i + 1 << " Sum" << " = " << row_s;
row_s = 0;
cout << endl;
}
return;
}
// To find the column sum
void col_sum(int Array[][40], int& a, int& b)
{
int col_s = 0;
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{
col_s += Array[j][i];
}
cout << "C" << i + 1 << " Sum" << " = " << col_s;
col_s = 0;
cout << endl;
}
return;
}
// To transpose (To change the elements of rows and columns) the matrix
void transposed(int Array[][40], int& a, int& b)
{
for (int i = 0; i < a; i++)
{
cout << "[";
for (int j = 0; j < b; j++)
{
cout << Array[j][i];
if (j != b - 1)
cout << " ";
else if (j == b - 1)
cout << "]";
}
cout << endl;
}
return;
}
// To display (To showcase) the available choiced
void showcases()
{
cout << "Menu" << endl;
cout << "1. input elements" << endl;
cout << "2. Display matrix" << endl;
cout << "3. Sum of matrix elements" << endl;
cout << "4. Sum of matrix rows" << endl;
cout << "5. Sum of matrix columns" << endl;
cout << "6. The transpose of elements" << endl;
cout << "7. Exit" << endl;
cout << "Choose among the options above: ";
}
when both the number of rows and columns are 3 this is my output when i select 2 to display the elements
[1 2 3]
[4 5 6]
[6 7 8]
and after transpose this is the new matrix
[1 4 6]
[2 5 7]
[3 6 8]
and col sum works fine
C1 Sum = 11
C2 Sum = 14
C3 Sum = 17
but if number of rows and columns differ this is the output
Eg: no of rows:4
no of columns: 3
output after asking to display the output
[1 2 3]
[4 5 6]
[7 8 9]
[0 2 4]
output after the transpose
[1 4 7]
[2 5 8]
[3 6 9]
[-858993460 -858993460 -858993460]
sum of columns
C1 Sum = 12
C2 Sum = 15
C3 Sum = 18
C4 Sum = 1717986916
Any help is appreciated sorry if the format is wrong it was not letting me upload and it keep telling me either my code is not formmated or your post is mostly code
Now i do not get the error anymore
{
// Variable Declaration
int num1 = 0, num2 = 0, choice = 0, A[10][20], B[10][20], n = 0, x = 0;
for (int k = 0; choice != 7; k++)
{
// Function Call
showcases();
// Input Choice Number
cin >> choice;
cout << "----------------------------------------------" << endl;
// Output Choice & Function Call
if (choice == 1)
{
input_element(A, num1, num2);
}
else if (choice == 2)
{
display_matrix(A, num1, num2);
}
else if (choice == 3)
{
sum(A, num1, num2);
}
else if (choice == 4)
{
row_sum(A, num1, num2);
}
else if (choice == 5)
{
col_sum(A, num1, num2);
}
else if (choice == 6)
{
cout << "The transpose of the matrix is: " << endl;
transp(A, num1, num2);
cout << endl;
}
else if (choice == 7)
{
cout << "The application will exit now!!" << endl;
}
else
{
cout << "Your choice is invalid" << endl;
}
cout << "----------------------------------------------" << endl << endl;
}
// Exit
system("pause");
return 0;
}
// Input Function
void input_element(int A[][20], int& m, int& n)
{
cout << "Enter number of rows for matrix: ";
cin >> m;
cout << "Enter number of columns for matrix: ";
cin >> n;
cout << endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cout << "Enter elements of matrix " << "[" << i + 1 << "]" << " [" << j + 1 << "]: ";
cin >> A[i][j];
}
}
}
// Dispaly Matrix Function
void display_matrix(int A[][20], int& m, int& n)
{
for (int i = 0; i < m; i++)
{
cout << "[";
for (int j = 0; j < n; j++)
{
cout << A[i][j];
if (j != n - 1)
cout << " ";
else if (j == n - 1)
cout << "]";
}
cout << endl;
}
}
// Sum Function
void sum(int A[][20], int& m, int& n)
{
int sum = 0;
for (int row = 0; row < m; row++)
{
for (int column = 0; column < n; column++)
{
sum += A[row][column];
}
}
cout << "Matrix Elements Sum = " << sum << endl;
}
// Row Sum Function
void row_sum(int A[][20], int& m, int& n)
{
cout << "The sum of the matrix rows are the following: -\n";
int row_sum = 0;
for (int i = 0; i < m; i++)
{
row_sum = 0;
for (int j = 0; j < n; j++)
{
row_sum += A[i][j];
}
cout << "R" << i + 1 << " Sum = " << row_sum << endl;
}
return;
}
// Column Sum Function
void col_sum(int A[][20], int& m, int& n)
{
cout << "The sum of the matrix columns are the following : -\n";
int col_sum = 0;
for (int j = 0; j < n; j++)
{
col_sum = 0;
for (int i = 0; i < m; i++)
{
col_sum += A[i][j];
}
cout << "C" << j + 1 << " Sum = " << col_sum << endl;
}
return;
}
// Transpose Function
void transp(int A[][20], int& m, int& n)
{
for (int j = 0; j < n; j++)
{
cout << "[";
for (int i = 0; i < m; i++)
{
cout << A[i][j];
if (i != (m - 1))
{
cout << " ";
}
else if (i == (m - 1))
{
cout << "]";
}
}
cout << endl;
}
}
// Showcase Menu Function
void showcases()
{
cout << "Menu" << endl;
cout << "1. Input Elements" << endl;
cout << "2. Display Matrix" << endl;
cout << "3. Sum Of Matrix Elements" << endl;
cout << "4. Sum Of Matrix Rows" << endl;
cout << "5. Sum Of Matrix Columns" << endl;
cout << "6. Transpose Of Matrix" << endl;
cout << "7. Exit" << endl;
cout << "Choose among the options above: ";
}

banker's algorithm in c++

I have project in banker algorithm implement in c++
But I have small mistake :') in resource request
I didn't know what the mistake. The first if statement doesn't work :(
#include<iostream>
#include<vector>
using namespace std;
int main() {
int work[4];
int allocation[5][4];
int max[5][4];
int need[5][4];
int p, pr, r, a, aval[4], req[4];
bool state[5], test;
vector < int > avl;
//----------------------------------------
test = true;
for (int i = 0; i < 4; i++)
work[i] = aval[i];
for (int i = 0; i < 5; i++)
state[i] = false;
//----------------------------enter p r---------------------------------
cout << "Enter the number of processes in the system :";
cin >> p;
cout << "\nEnter the number of recourses :";
cin >> r;
//---------------------enter alloc---
cout << "\nEnter the allocation " << endl;
if (r = 1)
{
cout << "\t A \n \t ";
}
else if (r = 2)
{
cout << "\t A B \n \t ";
}
else if (r = 3)
{
cout << " A B C\n \t ";
}
else if (r = 4)
{
cout << " A B C D\n \t ";
}
for (int i = 0; i < p; i++)
{
cout << endl << "\t" << "P" << i << ":";
for (int j = 0; j < 4; j++)
{
cin >> allocation[i][j];
cout << " ";
}
}
//-----------------------------entet max----------------
cout << "\nEnter the MAX" << endl;
if (r = 1)
cout << " A \n \t ";
else if (r = 2)
cout << " A B \n \t ";
else if (r = 3)
cout << " A B C\n \t ";
else if (r = 4)
cout << " A B C D\n \t ";
for (int i = 0; i < p; i++)
{
cout << endl << "P" << i << ":";
for (int j = 0; j < 4; j++)
{
cin >> max[i][j];
need[i][j] = max[i][j] - allocation[i][j];
}
}
//-----------------enter ava--------------
cout << "\nEnter the avaliable number : " << endl;
for (int i = 0; i < 4; i++)
{
cin >> aval[i];
cout << " ";
}
//-----------------enter request--------------
cout << "\nEnter the number of process want be request : ";
cin >> pr;
cout << "\nEnter the request number : " << endl;
for (int i = 0; i < 4; i++)
{
cin >> req[i];
cout << " ";
}
//-----------------------------------COUT---------------------
cout << endl << "There are " << p << " processes in the system." << endl << endl;
cout << "There are " << r << " resource types." << endl << endl;
//----------------------------------cout allocation---------------
cout << " The allocation Matrix : " << endl << endl;
cout << "\t A B C D";
for (int i = 0; i < p; i++)
{
cout << endl << "\tP" << i << ":";
for (int j = 0; j < 4; j++)
{
cout << allocation[i][j] << " ";
}
cout << endl;
}
//----------------------------------cout max---------------
cout << endl << " The Max Matrix is... " << endl << endl;
cout << "\t A B C D";
for (int i = 0; i < p; i++)
{
cout << endl << "\tP" << i << ":";
for (int j = 0; j < 4; j++)
{
cout << max[i][j] << " ";
}
cout << endl;
}
//-------------------------cout need-------------------------------------------
cout << endl << " The Need Matrix is... " << endl << endl;
cout << "\t A B C D";
for (int i = 0; i < p; i++)
{
cout << endl << "\tP" << i << ":";
for (int j = 0; j < 4; j++)
{
cout << need[i][j] << " ";
}
cout << endl;
}
//----------------------------- cout aval ---------------------
cout << endl << "The Available Vector is..." << endl << endl;
cout << "A B C D" << endl;
for (int i = 0; i < 4; i++)
{
cout << aval[i] << " ";
}
//-----------------------------------SAFE STATE-----------------------
int k = 0;
for (k = 0; k < p; k++) {
if (state[k] == false) {
test = false;
for (int j = 0; j<r; j++) {
if (need[k][j] > work[j])
break;
if (need[k][j] <= aval[j])
test = true;
}
}
}
if (test == true) {
for (int j = 0; j < r; j++)
{
work[j] = work[j] + allocation[k][j];
}
state[k] = true;
cout << endl << endl << "THE SYSTEM IS IN A SAFESTATE!" << endl;
}
if (test == false) {
state[k] = false;
cout << endl << endl << "THE SYSTEM IS NOT IN A SAFE STATE!";
}
//-----------------------------------request------------------------
cout << "\nThe Request Vector is..." << endl;
cout << " A B C D" << endl;
cout << pr << ":";
for (int i = 0; i < 4; i++)
{
cout << req[i] << " ";
}
bool test2 = false;
for (int i = 0; i < p; i++) {
if (pr == p) {
for (int j = 0; j < 4; j++)
{
if (req[j] <= avl[j] && req[j] <= need[i][j])
{
test2 = true;
}
else
{
break;
}
}
if (test2 = true)
{
for (int n = 0; n < r; n++)
{
aval[n] = aval[n] - req[n];
allocation[i][n] = allocation[i][n] + req[n];
need[i][n] = need[i][n] - req[n];
}
cout << "THE REQUEST CAN BE GRANTED!" << endl << endl;
cout << "The Available Vector is...";
cout << "A B C D" << endl;
for (int x = 0; x < r; x++)
{
cout << aval[x] << " ";
}
}
else
{
cout << "THE REQUEST CANNOT BE GRANTED!" << endl << endl;
}
}
}
//------------------------------------------------------------------------------
system("pause");
return 0;
}
When checking if two primitive types are equal, you need to use "==" instead of "="
e.g, change your if statements from
if ( r = 1 )
to
if (r == 1)
Apart from above
work[i] = aval[i]; - aval[i] - has not been initialised
Read up on switch instead of r == 1 do this etc. ( I took into account the above statement and the chained if statements
Ditto with = true. Learn the difference between comparison and assignment
Perhaps learn how to use the debugger
You need to use cout << flush so that the output is sent.
.... I could add others - this is enough to get on with
Here is the whole working program. I made some changes in it and made the code more easy to understand.
int p, r;
bool test;
vector < int > avl;
//----------------------------enter p r---------------------------------
system("clear");
cout << "Enter the number of processes in the system: ";
cin >> p;
cout << "Enter the number of recourses: ";
cin >> r;
//test
int allocation[p][r];
int max[p][r];
int need[p][r];
int aval[r];
int state[p];
test = true;
//test
//---------------------enter alloc---
system("clear");
cout << "\nEnter the allocation " << endl;
if (r == 1)
{
cout << "\t A \n \t ";
}
else if (r == 2)
{
cout << "\t A B \n \t ";
}
else if (r == 3)
{
cout << "\t A B C \n \t ";
}
else if (r == 4)
{
cout << "\t A B C D \n \t ";
}
for (int i = 0; i < p; i++)
{
cout << endl << "\t" << "P" << i << ": ";
for (int j = 0; j < r; j++)
{
cin >> allocation[i][j];
cout << " ";
}
}
system("clear");
//-----------------------------entet max----------------
cout << "\nEnter the MAX" << endl;
if (r == 1)
cout << "\t A \n \t ";
else if (r == 2)
cout << "\t A B \n \t ";
else if (r == 3)
cout << "\t A B C\n \t ";
else if (r == 4)
cout << "\t A B C D\n \t ";
for (int i = 0; i < p; i++)
{
cout << endl << "\t" << "P" << i << ": ";
for (int j = 0; j < r; j++)
{
cin >> max[i][j];
need[i][j] = max[i][j] - allocation[i][j];
}
}
system("clear");
//-----------------enter ava--------------
cout << "\nEnter the avaliable number : " << endl;
cout<<"\tAvail: ";
for (int i = 0; i < r; i++)
{
cin >> aval[i];
cout << " ";
}
//-----------------------------------COUT---------------------
system("clear");
system("clear");
cout << "There are " << p << " processes in the system." << endl;
cout << "There are " << r << " resource types." << endl << endl;
//----------------------------------cout allocation---------------
cout << " The allocation Matrix : " << endl << endl;
if (r == 1)
cout << "\t A \n \t ";
else if (r == 2)
cout << "\t A B \n \t ";
else if (r == 3)
cout << "\t A B C\n \t ";
else if (r == 4)
cout << "\t A B C D\n \t ";
for (int i = 0; i < p; i++)
{
cout << endl << "\tP" << i << ": ";
for (int j = 0; j < r; j++)
{
cout << allocation[i][j] << " ";
}
cout << endl;
}
//----------------------------------cout max---------------
cout << endl << " The Max Matrix is... " << endl << endl;
if (r == 1)
cout << "\t A \n \t ";
else if (r == 2)
cout << "\t A B \n \t ";
else if (r == 3)
cout << "\t A B C\n \t ";
else if (r == 4)
cout << "\t A B C D\n \t ";
for (int i = 0; i < p; i++)
{
cout << endl << "\tP" << i << ": ";
for (int j = 0; j < r; j++)
{
cout << max[i][j] << " ";
}
cout << endl;
}
//-------------------------cout need-------------------------------------------
cout << endl << " The Need Matrix is... " << endl << endl;
for(int i = 0; i < p; i++)
{
for(int j = 0; j < r; j++)
{
need[i][j] = max[i][j] - allocation[i][j];
}
}
if (r == 1)
cout << "\t A \n \t ";
else if (r == 2)
cout << "\t A B \n \t ";
else if (r == 3)
cout << "\t A B C\n \t ";
else if (r == 4)
cout << "\t A B C D\n \t ";
for (int i = 0; i < p; i++)
{
cout << endl << "\tP" << i << ": ";
for (int j = 0; j < r; j++)
{
cout << need[i][j] << " ";
}
cout << endl;
}
//----------------------------- cout aval ---------------------
cout << endl << "The Available Vector is..." << endl << endl;
cout<<"\tAvail: ";
for (int i = 0; i < r; i++)
{
cout << aval[i] << " ";
}
cout<<endl;
//--------SAFESTATE----------SAFESTATE----------SAFESTATE-------------------
int count = p;
cout<<endl;
cout<<"Safe sequence: ";
do{
for(int loop_var = 0; loop_var < p; loop_var++)
{
test = false;
for (int j = 0; j < r; j++) {
if(state[loop_var] == true)
{
break;
}
if(need[loop_var][j] > aval[j])
{
test = false;
state[loop_var] = false;
break;
}
else
{
test = true;
}
}
if((test))
{
count--;
state[loop_var] = true;
for(int sb = 0; sb < r; sb++)
{
aval[sb] = aval[sb] + allocation[loop_var][sb];
}
if(count == 0)
{
cout<<"P"<<loop_var<<" ";
}
else if(count > 0)
{
cout<<"P"<<loop_var<<"-->";
}
}
}
}while(count != 0);
cout<<endl;
//--------SAFESTATE----------SAFESTATE----------SAFESTATE-------------------
cout<<endl;
cout<<"The new available vector is: ";
for(int i = 0; i < r; i++)
{
cout<<aval[i]<<" ";
}
cout << endl << endl;
return 0;

Error: No operator = matches these operands

How come when I do fractionAry[i] = new Fraction(num1, denom1) it works and stores the created fraction into index of i.
But when I do fractionAry[i][j] = new Fraction(num1, denom1), the compiler gives me an error saying that no operator matches these operands?
If I would like to store the fraction into j, how would I do that? I am not entirely sure how to store a fraction into an array of array of fractions...
Here's my code, thanks for the help ahead of time.
void createArray() {
Fraction** fractionAry;
int aryCount;
int arySize;
int i, j;
int num1, denom1;
cout << "\nHow many arrays of fractions (treating these as array of arrays of fractions? ";
cin >> aryCount;
if (aryCount > 0) {
fractionAry = new Fraction*[aryCount];
for (i = 0; i < aryCount; i++) {
cout << "\nCreating array index # " << i
<< "\n How many fractions(s)? ";
cin >> arySize;
if (arySize > 0) {
fractionAry[i] = new Fraction[arySize + 1];
fractionAry[i][0] = arySize;
for (j = 1; j < arySize + 1; j++) {
cout << "\n Enter the numerator: ";
cin >> num1;
cout << " Enter the denominator: ";
cin >> denom1;
while (denom1 == 0) {
cout << "\nCan't set to 0! Enter a new denominator: ";
cin >> denom1;
}
fractionAry[i] = new Fraction(num1, denom1);
// fractionAry[i][j] = new Fraction(num1, denom1); I would like to do this instead
}
cout << "\nFor array index #" << i << endl;
for (j = 0; j < arySize + 1; j++) {
cout << " Element index #" << j << " : " << *(*(fractionAry + i) + j) << endl;
}
}
}
}
}
Instead of having fractionAry[i][j] = new Fraction(...) use fractionAry[i][j] = Fraction(...). The type at fractionAry[i][j] is Fraction not Fraction* which is what the new operator returns.
void createArray() {
Fraction** fractionAry;
int aryCount;
int arySize;
int i, j;
int num1, denom1;
cout << "\nHow many arrays of fractions (treating these as array of arrays of fractions? ";
cin >> aryCount;
if (aryCount < 1)
{
//print error message or exit function
}
fractionAry = new Fraction*[aryCount];
for (i = 0; i < aryCount; i++)
{
cout << "\nCreating array index # " << i
<< "\n How many fractions(s)? ";
cin >> arySize;
if (arySize < 1)
{
//Print error message decrement i and continue or exit function
}
fractionAry[i] = new Fraction[arySize + 1];
//I don't understand the purpose of assigning the below value or making the array one size bigger
fractionAry[i][0] = arySize;
///
for (j = 1; j < arySize + 1; j++)
{
cout << "\n Enter the numerator: ";
cin >> num1;
cout << " Enter the denominator: ";
cin >> denom1;
while (denom1 == 0)
{
cout << "\nCan't set to 0! Enter a new denominator: ";
cin >> denom1;
}
fractionAry[i][j] = Fraction(num1, denom1);
}
cout << "\nFor array index #" << i << endl;
for (j = 0; j < arySize + 1; j++)
{
cout << " Element index #" << j << " : " << fractionAry[i][j] << endl;
}
}
}

How to do a function that will return me to main menu from any place of program?

How to do a function that will clean screen and return me to a main menu of program even during entering elements of matrix. And how to disable pressing other keys except arrows and Enter during navigating the menu?
This is my C++ code:
while(running)
{
gotoXY(18,1); cout << "Main Menu";
gotoXY(20,3); cout << " Enter matrix";
gotoXY(20,4); cout << " Randomize matrix";
gotoXY(20,5); cout << " Exit";
system("pause>nul"); // the >nul bit causes it the print no message
if(GetAsyncKeyState(VK_DOWN) || GetAsyncKeyState(VK_RIGHT)) //down button pressed
{
gotoXY(18,pos); cout << " ";
pos++;
gotoXY(18,pos); cout << "->";
menu_item++;
if (pos == 6)
{
gotoXY(18,pos); cout << " ";
pos = 3;
gotoXY(18,3); cout << "->";
menu_item = 0;
}
continue;
}
if(GetAsyncKeyState(VK_UP) || GetAsyncKeyState(VK_LEFT)) //up button pressed
{
gotoXY(18,pos); cout << " ";
pos--;
gotoXY(18,pos); cout << "->";
menu_item--;
if (pos == 2)
{
gotoXY(18,pos); cout << " ";
pos = 5;
gotoXY(18,5); cout << "->";
menu_item = 2;
}
continue;
}
if(GetAsyncKeyState(VK_RETURN)){ // Enter key pressed
switch(menu_item){
case 0: {
gotoXY(20,10);
int i, j, n;
double **a, *b;
cout << "Enter NUMBER of equations: ";
cin >> n;
a = (double **)malloc(n*sizeof(double));
b = (double *)malloc(n*sizeof(double));
cout << "Enter Matrix A\n";
for(i = 0; i < n; i++)
{
a[i] = (double *)malloc(n*sizeof(double));
//Ввод a
for(j = 0; j < n; j++)
{
cout <<"a["<< i + 1 << "][" << j + 1 << "] = ";
//cin >>a[i][j];
a[i][j] = proverkafloat();
}
}
cout << "\tSee input\r\n";
cout << "Matrix A:\r\n";
for(i = 0; i < n; i++)
{
cout << "|\t";
ShowVector(n, a[i]);
};
cout << endl;
cout << "Enter Vector B\n";
for(i = 0; i < n; i++)
{
cout << "b[" << i + 1 << "] = ";
cin >> b[i]
}
cout << "\n\n";
cout << "\tSee input\r\n";
cout << "\n";
cout << "Vector B:\r\n";
cout << "|\t";
ShowVector(n, b);
system("pause");
system("CLS");
gotoXY(18,pos); cout << "->";
break;
}
case 1:
{
gotoXY(20,10);
int i, j;
double **a, *b, n;
cout << "Enter NUMBER of equations: ";
cin >> n;
srand(time(0));
a = (double **)malloc(n*sizeof(double));
b = (double *)malloc(n*sizeof(double));
//Randomizing Matrix A
for(i = 0; i < n; i++)
{
a[i] = (double *)malloc(n*sizeof(double));
//Ввод a
for(j = 0; j < n; j++)
{
a[i][j] = (double)(rand() % 100 + (-50))/((double)(rand() % 100 + (-50)));
}
}
cout << "\tSee input\r\n";
cout << "Matrix A:\r\n";
for(i = 0; i < n; i++)
{
cout << "|\t";
ShowVector(n, a[i]);
};
//Randomizing Vector B
for(i = 0; i < n; i++)
{
b[i] = (double)(rand() % 100 + (-50))/((double)(rand() % 100 + (-50)));
}
cout << "\n\n";
cout << "\tSee input\r\n";
cout << "\n";
cout << "Vector B:\r\n";
cout << "|\t";
ShowVector(n, b);
system("pause");
system("CLS");
gotoXY(18,pos); cout << "->";
break;
}
case 2:
{
gotoXY(20,10);
cout << "The program has now terminated!!";
running = false;
getch();
}
}
}
}
gotoXY(20,21);
return 0;
}
void gotoXY(int x, int y)
{
CursorPosition.X = x;
CursorPosition.Y = y;
SetConsoleCursorPosition(console,CursorPosition);
}
void ShowVector(int n, double * vec)
{
for(int i = 0; i < n; i++)
cout << fixed << setprecision(2) << vec[i] << " ";
cout << "\t";
cout << "|";
cout << "\r\n";
}
The best method to "return" from a canceled menu processing function is to return.
One issue may be stack unwinding. When the main menu processing function calls a function for menu selection process, it pushes the return address on the stack. If the selection processing function fails or needs to return quickly, it can't use a goto because there will still be that return address on the stack. This issue is more pronounced when you have nested menus.
This is similar to error handling and catching. You could try having a catch statement in your main menu function.