I have a program that takes in a set of 16 numbers and prints them out in a grid of 4x4. I must then check each row, column and diagonal add up to the same number, however I can't do this part, as I have no idea how that would work. Can anyone help?
current code:
void getNumbers(int numbers[]){
int idx;
for(int x = 0; x < 17; x++){
cout << "Please enter a number: " << endl;
cin >> idx;
numbers[x] = idx;
}
cout << " " << numbers[0] << " " << numbers[1] << " " << numbers[2] << " " << numbers[3] << endl;
cout << " " << numbers[4] << " " << numbers[5] << " " << numbers[6] << " " << numbers[7] << endl;
cout << " " << numbers[8] << " " << numbers[9] << " " << numbers[10] << " " << numbers[11] << endl;
cout << " " << numbers[12] << " " << numbers[13] << " " << numbers[14] << " " << numbers[15] << endl;
}
remove any syntax error if you see, the overall code must work! Enjoy ;-)
bool magicSquare(){
int idx;
int numbers[4][4];
cout << "Please enter your numbers: " << endl;
for(unsigned int i=0; i<4; i++) {
for(unsigned int j=0; j<4; j++) {
cin >> idx;
numbers[i][j] = idx;
}
}
// Checking
for(unsigned int i=0; i<4; i++) {
int row_sum = 0;
for(unsigned int j=0; j<4; j++) {
row_sum+= numbers[i][j];
}
int col_sum = 0;
for(unsigned int j=0; j<4; j++) {
col_sum+= numbers[j][i];
}
int diag_sum_left = numbers[0][0] + numbers[1][1] + numbers[2][2];
int diag_sum_right = numbers[0][2] + numbers[1][1] + numbers[2][0];
if ((col_sum != 15) ||
(row_sum !=15) ||
(diag_sum_left != 15) ||
(diag_sum_right != 15) )
return false;
}
cout << "Your answer is correct:" << endl;
for(unsigned int i=0; i<4; i++) {
for(unsigned int j=0; j<4; j++)
cout << numbers[i][j] << "\t";
cout << endl;
}
return true;
}
You can just write all the sums and the rules directly
Like this:
int horizontal1 = numbers[0]+ numbers[1]+ numbers[2]+ numbers[3];
int horizontal2 = numbers[4]+ numbers[5]+ numbers[6]+ numbers[7];
int horizontal3 = numbers[8]+ numbers[9]+ numbers[10]+numbers[11];
int horizontal4 = numbers[12]+numbers[13]+numbers[14]+numbers[15];
int vertical1 = numbers[0]+numbers[4]+numbers[8]+ numbers[12];
int vertical2 = numbers[1]+numbers[5]+numbers[9]+ numbers[13];
int vertical3 = numbers[2]+numbers[6]+numbers[10]+numbers[14];
int vertical4 = numbers[3]+numbers[7]+numbers[11]+numbers[15];
int diagonal1 = numbers[0]+numbers[5]+numbers[10]+numbers[15];
int diagonal2 = numbers[3]+numbers[6]+numbers[9]+ numbers[12];
// The result of check would be in this variable
bool result = horizontal1 == horizontal2 &&
horizontal1 == horizontal3 &&
horizontal1 == horizontal4 &&
horizontal1 == vertical1 &&
horizontal1 == vertical2 &&
horizontal1 == vertical3 &&
horizontal1 == vertical4 &&
horizontal1 == diagonal1 &&
horizontal1 == diagonal2;
Put how to check aside, you have something wrong in the posted code:
You mentioned that you have 16 numbers but you are actually asking for 17. If your numbers array has size = 16, then you will have index out of bound error.
for(int x = 0; x < 17; x++){
//^^^should be 16, put them into numbers array with numbers[0] to numbers[15]
cout << "Please enter a number: " << endl;
cin >> idx;
numbers[x] = idx;
}
For checking, the brute force way is to check for rows,columns then diagonal separately.
Related
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");
I've been trying to overcome this problem for a few hours now and I seem to have one approach to the situation. It seems that the use of selection statements worked in creating the table necessary. Although there are formatting issues.
I'd like to know if there was a way to create the same table
using only nested for-loops as mentioned by our professor.
Are the selection statements necessary or can we implement a system of nested for loops to acquire the same results?
The image below is the required table:
But the image below is what I have:
Below is my code:
for (int i = 0; i <= numChoice; ++i)
{
if (i == 0)
{
for (int k = 1; k <= numChoice; ++k)
{
cout << " " << k;
}
cout << "\n";
}
else
{
cout << i << " | ";
for (int j = 1; j <= numChoice; ++j)
{
if (j*i <= 9)
{
cout << " " << j*i << "|";
}
else if (j*i > 9 && j*i <= 100)
{
cout << " " << j*i << "|";
}
else if (j*i > 99 && j*i <= 999)
{
cout << " " << j*i << "|";
}
}
cout << "\n";
for (int k = 0; k <= numChoice; ++k)
{
if (k == 0)
{
cout << "-|";
}
else
{
cout << "----|";
}
}
cout << "\n";
}
}
The following code uses no if else constructs. The formatting can be got by using setw, used for setting the width of integers.Following code produces perfect output.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i,j;
cout<<" "<<1;//5 space chars
for(i = 2;i <= 10;++i)
cout<<" "<<i;//4 space chars
cout<<endl;
cout<<" ----|";
for(i = 2;i <= 10;++i)
cout<<"----|";
cout<<endl;
for(i = 1;i <= 10;++i)
{
cout<<setw(2)<<i<<"|";
for(j = 1;j <= 10;++j)
cout<<setw(4)<<j*i<<"|";
cout<<endl;
cout<<" -|----";
for(j = 2;j <= 9;++j)
cout<<"|----";
cout<<"|----|";
cout<<endl;
}
return 0;
}
#FranticCode. I'm also in the same class as you and was having problems with this homework assignment as well. I still don't understand it, but I figured out how to manipulate Sumeet's code to give us correct format. The ONLY thing I am having a problem with now is adding an empty space AFTER the first multiplication table and before the menu redisplay. I'll share what I have and maybe you can figure it out. Still going to ask the professor to review chapter 5 because I would like to learn it rather than just submit the homework.
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
char userSelection;
int numForTable;
int col;
int row;
do
{
cout << "MENU" << endl
<< "a) Generate Multiplication Table" << endl
<< "q) Quit the program" << endl
<< "Please make a selection: ";
cin >> userSelection;
if (userSelection == 'a')
{
cout << "Please enter a number for your multiplication table: " << endl;
cin >> numForTable;
while (numForTable < 1 || numForTable > 10)
{
cout << "Please enter a number between 1 & 10." << endl;
cin >> numForTable;
}
cout << "\n"
<< "MULTIPLICATION TABLE: " << numForTable << "'s" << endl
<< "\n"
<< " " << 1;
for (col = 2; col <= numForTable; ++col)
cout << " " << col;
cout << endl;
cout << " ----|";
for (col = 2; col <= numForTable; ++col)
cout << "----|";
cout << endl;
for (col = 1; col <= numForTable; ++col)
{
cout << setw(2) << col << "|";
for (row = 1; row <= numForTable; ++row)
cout << setw(4) << col * row << "|";
cout << endl;
cout << " -|----";
for (row = 2; row <= numForTable - 1; ++row)
cout << "|----";
cout << "|----|";
cout << endl;
}
}
else if (userSelection != 'q')
{
cout << "Invalid Selection\n" << endl;
}
else if (userSelection == 'q')
{
cout << " You have chosen to quit the program. Thank you for using!" << endl;
}
}
while (userSelection != 'q');
//system("PAUSE");
return 0;
}
got curious to see if i could add the lines as easy as i claimed, it took a bit of fiddling, but here's the result (updated code below to also have lines).
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int counter;
int counter2;
int amount;
cout << " |-----------------------------------------------------------|" << endl; // first line of table.
for(counter=1;counter<11;counter++){ // the 2 for lines create our 2 dimensional table
for(counter2=1;counter2<11;counter2++){
cout << " | " << setw(3) << counter*counter2; // setw(3) is a function of <iomanip>,
//setting minimum width to 3 for numbers.
}
cout << " |" << endl; // this here is being added to the end of each line and starts a new line.
cout << " |-----------------------------------------------------------|" << endl; // this is being inserted between each line, and starts a new line.
}
return 0;
}
Use the following construct:
for (int i=0; i<=numChoice; i++) // display first row of numbers
cout <<"\t" << i << "\t";
cout << "\n";
for (int i=0; i <=numChoice; i++) {
cout << i << "\t";
for (int j=0; j <=numChoice; j++)
cout << i*j << "\t";
cout << "\n";
}
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;
the 7 functions i need to make
this file is using arrays to retrieve data from an input file and display the data onto an output file. I'm having trouble with the void input data function. The call function inputdata(inputFile, nonNumerical, numeric1); is getting an error saying error: cannot convert 'int ()[2]' to 'double ()[2]' for argument '3' to 'void inputdata(std::ifstream&, std::string()[9], double()[2])'
#include <fstream>
#include <string>
#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;
const int courseSize = 3;
const int examSize = 6;
const int studsize =3;
const int strSize =9;
const int numSize = 2;
void inputdata(ifstream &, string [][strSize],double [][numSize]);
int main()
{
int n=0, x, y;
int strLength =0;
double scoreTotal =0;
char lettergrade;
string header, name, address, phone, social, course, studentID;
const int letterGrade_A = 90;
const int letterGrade_B = 80;
const int letterGrade_C = 70;
const int letterGrade_D = 60;
int age, yearsAtTXST, numstudents;
double testscore;
double numGrade = 0;
const int minStrLength = 1;
const int maxStrLength = 100;
const int minint = 1;
const int maxint = 100;
const int minCourse = 1;
const int maxCourse = 3;
const int minTest = 1;
const int maxTest = 5;
const double test1 = 0.10;
const double test2 = 0.15;
const double test3 =0.15;
const double test4 = 0.20;
const double final_test = 0.40;
const double maxGrade = 100.0, minGrade = 1.0;
// initialize arrays
string nonNumerical[studsize][strSize];
int numeric1[studsize][numSize];
double numeric2[studsize][courseSize][examSize];
char lettergrades[studsize][courseSize];
cout << fixed << showpoint << setprecision(2);
// opening files
ifstream inputFile;
inputFile.open("Project5_A04314548_Input.txt");
ofstream fout;
fout.open ("Project5_A04314548_Output.txt");
if( !( inputFile && fout))
{
cout << "Error opening file.\n";
}
cout << " Enter a number of students: ";
cin >> numstudents;
while( !(numstudents ==3))
{
cout << " error number of students must be 3. Enter again: ";
cin >> numstudents;
}
inputdata(inputFile, nonNumerical, numeric1);
// take in string data from input file
for(int x =0; x< studsize; x++)
{
for(int y=0; y < strSize; y++)
{
if(y >= strSize)
break;
getline(inputFile, nonNumerical[x][y]);
strLength = nonNumerical[x][y].length();
if(strLength < minStrLength || strLength > maxStrLength)
{
fout << " The given string for nonNumerical [" << x << "][" << y <<
"] is not within the proper range of 1-100 characters." << endl;
fout << " Please fix the issue and rerun the program for a correct
output." << endl << endl;
continue;
}
}
}
//above put into input data function
// take in data from input file
for(int x=0; x<studsize;x++)
{
for(int y=0; y < numSize; y++)
{
if(y>=numSize)
break;
inputFile >> numeric1[x][y];
if( numeric1[x][y] < minint || numeric1[x][y] > maxint)
{
fout << "The given integer for numerical1[" << x << "][" << y <<
"]is not within the proper range of 1-100." << endl;
fout << " please fix the problem and rerun the program for a correct
output." << endl << endl;
break;
}
}
}
// above put into data function
// take in double data from input file
for( int x1=0; x1<studsize; x1++)
{
for(int x2=0; x2 < courseSize; x2++)
{
for(int x3=0; x3<(examSize-1);x3++)
{
if(x3 >=(examSize-1))
break;
inputFile >> numeric2[x1][x2][x3];
if( numeric2[x1][x2][x3] < minGrade || numeric2[x1][x2][x3] >
maxGrade)
{
cout << "The given value fro numeric2[" << x1 << "][" << x2
<< "][" << x3 << "] is not within the proper range of 1-100" << endl
<< "Please fix the issue and rerun the program for a correct
output." << endl;
break;
}
else if( x3 == 0)
scoreTotal += (numeric2[x1][x2][x3]* test1);
else if( x3== 1)
scoreTotal += (numeric2[x1][x2][x3]* test2);
else if( x3 == 2)
scoreTotal += (numeric2[x1][x2][x3]* test3);
else if( x3 == 3)
scoreTotal += (numeric2[x1][x2][x3]* test4);
else
scoreTotal += (numeric2[x1][x2][x3]* final_test);
}
numeric2[x1][x2][5] = scoreTotal; // final numeric grade
scoreTotal = 0;
if(x2 >=courseSize)
break;
if((numeric2[x1][x2][5] > maxGrade) || (numeric2[x1][x2][5] <
minGrade) )
{
cout << "Error in calculating grade. Please fix the issue
then rerun the program. ignore broken program!";
cout << endl << endl;
continue;
}
else if(numeric2[x1][x2][5] >= letterGrade_A)
lettergrades[x1][x2] = 'A';
else if(numeric2[x1][x2][5] >= letterGrade_B)
lettergrades[x1][x2] = 'B';
else if(numeric2[x1][x2][5] >= letterGrade_C)
lettergrades[x1][x2] = 'C';
else if(numeric2[x1][x2][5] >= letterGrade_D)
lettergrades[x1][x2] = 'D';
else
lettergrades[x1][x2] = 'F';
}
}
for(int a1=0; a1< studsize; a1++)
{ fout << nonNumerical[a1][0]<< endl;
fout << right << setw(35) << "Name of Student:\t";
fout << nonNumerical[a1][1] << endl;
fout << right << setw(35) << "Student ID:\t";
fout << nonNumerical[a1][2]<< endl;
fout << right << setw(35) << "Address:\t" ;
fout << nonNumerical[a1][3] << endl;
fout << right << setw(35) << "Telephone Number:\t";
fout << nonNumerical[a1][4] << endl;
fout << right << setw(35) << "Student Soc. Security:\t";
fout << nonNumerical[a1][5] << endl;
fout << right << setw(35) << "Age:\t";
fout << numeric1[a1][0] << endl;
fout << right << setw(35) << "Number of years at Texas State:\t";
fout << numeric1 [a1][1] << endl << endl;
for(int b1=0; b1 <courseSize; b1++)
{
fout << right << setw(35) << "Course number:\t";
fout << nonNumerical[a1][(b1+6)] << endl;
for(int c1= 0; c1 <(examSize-1); c1++)
{
fout << right << setw(32) << "Exam #" << (a1 +1) << ":\t";
fout << numeric2[a1][b1][c1] << endl;
}
fout << right << setw(35) << "Numerical grade:\t";
fout << numeric2[a1][b1][5] << endl;
fout << right << setw(35) << "Letter grade:\t";
fout << lettergrades[a1][b1] << endl;
if( numeric2[a1][b1][5] < 70)
{
fout << right << setw(14) << " Warning Note: Your grade is too low
and needs improvements!" << endl << endl;
}
else if ( numeric2[a1][b1][5] >= 95)
{
fout << right << setw(14) << " Appreciation Note: Congratulations,
Your performance is Excellent!" << endl << endl;
}
else
fout << endl;
}
fout << endl;
}
inputFile.close();
fout.close();
return 0;
}
void inputdata(ifstream & inputFile, string array1[][strSize],double
array2[]
[numSize])
{
for(int x =0; x< studsize; x++)
{
for(int y=0; y < strSize; y++)
{
if(y >= strSize)
break;
getline(inputFile, array1[x][y]);
}
}
for(int a1=0; a1<studsize;a1++)
{
for(int a2=0; a2 < numSize; a2++)
{
if(a2>=numSize)
break;
inputFile >> array2[a1][a2];
}
}
}
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.