Hello stack overflow users! I have a question about my program, and a problem I am encountering. I have practically finished the program, but I can't seem to have a separate void function for my dimensions (width and length of rocket). I currently have them inside my void function that creates my rocket, void draw box, but I cant seem to figure out how to have a separate void function for my dimensions. Any tips or things I am doing wrong to fix this? Any help is appreciated, thanks once again!
#include <iostream>
using namespace std;
void dimensions (int, int);
void drawBox (int , int , int);
int main ()
{
int numXs, numSpaces, numRows, width, height;
//dimensions (width, height);
drawBox (numXs, numSpaces, numRows);
return 0;
}
void dimensions (int width, int height)
{
cout << "Enter width: ";
cin >> width;
cout << "Enter height: ";
cin >> height;
cout << endl;
}
void drawBox (int numXs, int numSpaces, int numRows)
{
int count, rowCount, spaceCount, width, height;
cout << "Enter width: ";
cin >> width;
cout << "Enter height: ";
cin >> height;
cout << endl;
for (count = 0; count < width; count++)
{
cout << "X";
}
cout << endl;
for (rowCount = 0; rowCount < height; rowCount++)
{
cout << "X";
if ( width % 2 == 0)
{
for (spaceCount = 0; spaceCount < width - 2; spaceCount++)
{
cout << " ";
}
cout << "X" << endl;
}
else
{
for (spaceCount = 0; spaceCount < width - 2; spaceCount++)
{
cout << "X";
}
cout << "X" << endl;
}
}
cout << "X";
for (spaceCount = 0; spaceCount < numSpaces; spaceCount++)
{
cout << " ";
}
for (count = 0; count < width - 1; count++)
{
cout << "X";
}
cout << endl;
//second box is being created below
for (count = 0; count < width; count++)
{
cout << "X";
}
cout << endl;
for (rowCount = 0; rowCount < height; rowCount++)
{
cout << "X";
if ( width % 2 == 0)
{
for (spaceCount = 0; spaceCount < width - 2; spaceCount++)
{
cout << " ";
}
cout << "X" << endl;
}
else
{
for (spaceCount = 0; spaceCount < width - 2; spaceCount++)
{
cout << "X";
}
cout << "X" << endl;
}
}
cout << "X";
for (spaceCount = 0; spaceCount < numSpaces; spaceCount++)
{
cout << " ";
}
for (count = 0; count < width - 1; count++)
{
cout << "X";
}
cout << endl;
}
Pass your variables by reference:
void dimensions(int& height, int& width)
{
height = 5;
width = -6; // Because int is signed, this is possible.
}
int main()
{
int tall = -42;
int length = 22;
dimensions(tall, length);
std::cout << "tall: " << tall << "\n";
std::cout << "length: " << length << "\n";
return 0;
}
Have you tried making height and width global variables, so that you can use them in both functions?
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");
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: ";
}
Okay so we have to change this program from reading input from a user to reading it off of a file, and so far i have changed a good chunk of the code that will read off of the file but every time i go to run the code i get these 5 errors that I can't figure out so here is my code
// Author:
// Source file:
// Description:
// Compiler used:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
struct records
{
int code;
double amount;
};
// Function Prototypes
void displayTitle();
double getBegBal(ifstream&);
void displayBal(records);
records getData(ifstream&);
double processCheck(double, double);
double processDeposit(double, double);
double processATM(double, double);
double processSvcChg(double);
//Global Constants
const double CHARGE = 10,
ATMFEE = 2;
int main()
{
//Variable Declarations
int transCode;
double balance,
transAmt;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
records trans;
ifstream inFile;
inFile.open("c:\\checkIn.dat");
displayTitle();
balance = getBegBal(inFile);
getData(inFile);
while (!inFile.eof())
{
trans = getData(inFile);
switch (trans.code)
{
case 1: balance = processCheck(balance, trans.amount); break;
case 2: balance = processDeposit(balance, trans.amount); break;
case 3: balance = processATM(balance, trans.amount); break;
}
displayBal(trans);
if (balance < 0)
balance = processSvcChg(balance);
getData(inFile);
}
return 0;
}
void displayTitle()
{
cout << "\n Check Register\n\n";
}
double getBegBal(ifstream& inFile)
{
//double bal;
records balance;
cout << " Enter beginning balance ";
inFile >> balance.amount;
return balance.amount;
}
void displayBal(records balance)
{
cout << "\t\tBalance = $" << setw(10) << balance.amount;
}
records getData(ifstream& inFile)
{
records rec;
cout << "\n\n Enter transaction code (0 to exit) ";
inFile >> rec.code;
if (rec.code > 0)
{
cout << "\n Enter transaction amount ";
}
return rec;
}
double processCheck(double bal, double amt)
{
cout << "\n Check = " << setw(10) << amt;
return (bal - amt);
}
double processDeposit(double bal, double amt)
{
cout << "\n Deposit = " << setw(10) << amt;
return (bal + amt);
}
double processATM(double bal, double amt)
{
records trans;
cout << "\n ATM = " << setw(10) << trans.amount;
bal = bal - amt;
displayBal(trans);
bal = bal - ATMFEE;
cout << "\n ATM Fee = " << setw(10) << ATMFEE;
return (bal);
}
double processSvcChg(double bal)
{
records trans;
cout << "\n Service chg =" << setw(8) << CHARGE;
bal = bal - CHARGE;
displayBal(trans);
return (bal);
}
error #2-3 are here
int transCode;
double balance,
transAmt;
the error is saying 'transCode': unreferenced local variable and
'transAmt': unreferenced local variable
errors #4-5 are here
double processATM(double bal, double amt)
{
records trans;
cout << "\n ATM = " << setw(10) << trans.amount;
bal = bal - amt;
displayBal(trans);// the error points here saying that the variable trans is uninitialized
bal = bal - ATMFEE;
cout << "\n ATM Fee = " << setw(10) << ATMFEE;
return (bal);
}
double processSvcChg(double bal)
{
records trans;
cout << "\n Service chg =" << setw(8) << CHARGE;
bal = bal - CHARGE;
displayBal(trans); // the error points here saying that the variable trans is uninitialized
return (bal);
}
Please and thank you for your help!
You initialized width = 0; and passed it to getWidth(). Therefore, width % 2 != 0 is evaluated as false and the prompt in getWidth() won't be displayed.
getWidth() won't need any arguments unless your assignment requires it because it is intended to just read the width and rethrn it.
do statement is useful to evaluate condition after executing loop body once.
int getWidth()
{
int width = 0;
do {
cout << "Enter width " << endl;
cin >> width;
} while (width % 2 != 0);
return width;
}
Then, use getWidth() in main() function like this:
width = getWidth();
It's working for me.
int displayMenu();
void displaySquare(int width);
void displayTriangle(int width);
int getWidth();
void displayUpsideDownTriangle(int width);
void displayDiamond(int width);
int main()
{
int width, shapes;
do {
shapes = displayMenu();
width = 0;
switch (shapes)
{
case 1:
width = getWidth();
displaySquare(width);
break;
case 2:
width = getWidth();
displayTriangle(width);
break;
case 3:
width = getWidth();
displayUpsideDownTriangle(width);
break;
case 4:
width = getWidth();
displayDiamond(width);
break;
case -9:
cout << "End of Program " << endl;
default:
cout << "Please choose one of the shapes..." << endl;
}
} while (shapes != -9);
system("pause");
return 0;
}
//this function sets up the display for the user
int displayMenu() {
int shapes;
cout << "\n~~ Shape Display menu ~~ " << endl << endl;
cout << " 1....Square\n" <<
" 2....Triangle\n " <<
" 3....Upside Down triangle\n " <<
" 4....Diamond\n\n " <<
" -9....Exit Program " << endl;
cout << endl;
cout << " Make a selection " << endl;
cin >> shapes;
return shapes;
}
int getWidth()
{
int width = 1;
do {
if (width % 2 != 0)
{
cout << "Enter width " << endl;
cin >> width;
}
else
{
cout << "Please enter odd number only. \nEnter width " << endl;
cin >> width;
}
} while (width % 2 == 0);
return width;
}
void displaySquare(int width)
{
int rows, columns;
for (rows = 0; rows < width; ++rows)
{
for (columns = 0; columns < width; ++columns)
{
cout << "# ";
}
cout << endl;
}
}
void displayTriangle(int width)
{
int rows, Spacing, ColHashtag;
for (rows = 1; rows < width; rows++) //controls the rows
{
for (Spacing = (width - rows); Spacing >= 1; Spacing--) // spaces out the rows to make an isoceles triangle
{
cout << " ";
}
for (ColHashtag = 1; ColHashtag <= (rows * 2) - 1; ColHashtag++) //controls the columns
{
cout << "#";
}
cout << endl;
}
}
void displayUpsideDownTriangle(int width)
{
int rows, Columns, spacing;
//sets up the rows for the top
for ((rows = width - 1); rows >= 1; rows--)
{
for (Columns = 1; Columns <= width - rows; Columns++) // sets up the columns
{
cout << " "; // spaces out the symbols to make an isoceles triangle
}
for (spacing = 1; spacing <= 2 * rows - 1; spacing++)
{
cout << "#";
}
cout << endl;
}
}
void displayDiamond(int width)
{
displayTriangle(width);
displayUpsideDownTriangle(width);
}
replace int main() and getWidth() with this
int main()
{
int width, shapes,wt;
do {
cout << "Enter width " << endl;
cin >> width;
wt=getWidth(width);
if(wt!=0)
{
shapes = displayMenu();
}
else
{
shapes=-9;
}
switch (shapes)
{
case 1:
displaySquare(wt);
break;
case 2:
displayTriangle(wt);
break;
case 3:
displayUpsideDownTriangle(wt);
break;
case 4:
displayDiamond(wt);
break;
case -9:
cout << "End of Program " << endl;
break;
default:
cout << "Please choose one of the shapes..." << endl;
}
} while (shapes != -9);
system("pause");
return 0;
}
int getWidth(int width)
{
while (width % 2 != 0) {
cout << "Enter width " << endl;
cin >> width;
}
return width;
}
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.
So i have this code which makes a box, but want to make the corners +, the lengths |, and the widths - . Also want to input a number so you can draw them like cout<<"enter the length number" etc... how would i do that?
Here is what i have to make a box:
#include <iostream.h>
#include <string.h>
void main()
{
for(int z=1; z<=79; z++)
{
cout << "";
}
cout << endl;
for(int i=1; i<=5; i++)
{
cout << "";
for(int j=1; j<=77; j++)
{
cout << " ";
}
cout << "" << endl;
}
for(int y=1; y<=79; y++)
{
cout << "";
}
cout << endl;
}
Draws a rectangle where int height is the height and int width is the width
#include <iostream>
void draw_rect(int width,int height)
{
using std::cout;
cout << "+";
for (int i = 0; i < width - 2; i++)
{
cout << "-";
}
cout << "+\n";
for (int i = 0; i < height - 2; i++)
{
cout << "|";
for (int j = 0; j < width - 2; j++)
{
cout << " ";
}
cout << "|\n";
}
cout << "+";
for (int i = 0; i < width - 2; i++)
{
cout << "-";
}
cout << "+\n";
}
int main ()
{
draw_rect(8,6);
return 0;
}
And for how to get user input read this:
Basic C++ IO
#include <iostream>
using namespace std;
void draw_rect( int width, int height)
{
int i;
cout << char(218);
for (i=0; i<width-2; i++)
cout << char(196);
cout << char(191) << endl;
for (i=0; i<height-2; i++)
{
cout << char(179);
for (int j=0; j<width-2; j++)
cout << " ";
cout << char(179) << endl;
}
cout << char(192);
for(i=0; i<width-2; i++)
cout << char(196);
cout << char(217) << endl;
}
int main()
{
draw_rect(20,10);
return 0;
}