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;
}
Related
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?
I want to display array using a method, if the array has under 200 elements it display all the elements, which works fine for me. The problem is if the array has over 200 elements i want to display the first 100 elements and the last 100 elements of an array. It works if I use an array of 500 elements or even 10000, but I type something like 9999 or 8999 I get long negative integer numbers on the bottom half of my display list but the top half half works. Any advice?
int main()
{
string fileName,text, size;
fstream inText;
int lengthOf = 0;
cout << "Please Enter An Input File Name: ";
getline(cin, fileName);
inText.open(fileName.c_str() , fstream::in);
if(!inText)
{
cout << "Could Not Open " << fileName << " File" << endl;
exit(EXIT_FAILURE);
}
else
{
inText >> lengthOf;
int * myArray = new int[lengthOf];
for(int i = 0; i < lengthOf; i++)
{
inText >> myArray[i];
}
cout << "Data File Array " << endl;
displayArray(myArray,lengthOf);
}
return 0;
}
void displayArray (int a[], int s)
{
if(s <= 200)
{
for (int i = 0; i < s; ++i)
{
if(i%10 == 0)
{
cout << endl;
}
cout << setw(6) << a[i] << " ";
}
cout << endl;
}
else
{
for(int i = 0; i < 100; i++)
{
if(i%10 == 0)
{
cout << endl;
}
cout << setw(6) << a[i] << " ";
}
cout << endl;
for (int i = s-100; i < s; ++i)
{
if (i%10 == 0)
{
cout << endl;
}
cout << setw(6) << a[i] << " ";
}
cout << endl;
}
}
Printing the array is straight forward, example:
int main()
{
int a[551]; //some random number
int s = 551;
for (int i = 0; i < s; ++i) a[i] = i;
for (int i = 0; i < s; ++i)
{
if (i % 10 == 0) cout << "\n";
if (i % 100 == 0) cout << "\n";
cout << std::setw(6) << a[i] << " ";
}
return 0;
}
When reading the file you can use std::vector to store the integers, this way you don't have to know how big the array should be before hand. The example below reads text and then tries to convert to integer, this way you know if there was an error in input file.
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
std::string fileName;
cout << "Please Enter An Input File Name: ";
getline(cin, fileName);
std::ifstream inText(fileName);
std::vector<int> vec;
std::string temp;
while (inText >> temp)
{
try {
int i = std::stoi(temp);
vec.push_back(i);
}
catch (...) {
cout << temp << " - error reading integer\n";
}
}
for (size_t i = 0; i < vec.size(); ++i)
{
if (i % 10 == 0) cout << "\n";
if (i % 100 == 0) cout << "\n";
cout << std::setw(6) << vec[i] << " ";
}
return 0;
}
Are You sure the problem is with this method? The code seems to work, I can't reproduce Your problem. Maybe You could paste Your whole code (if its not too big)?
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 for a lab project the goal is to print out shapes using a "*."
The four basic shapes are a square, two downwards pointing right triangles, and a hollow square. Everything seems to be working fine for any integer I input but I'm not entirely sure I'm doing the loop for the hollow square correctly and would like it if someone could give me some advice.
#include <iostream>
using std::cout; using std::endl; using std::cin;
int main()
{
cout << "Input arbitrary integer ";
int n;
cin >> n;
cout << endl;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++)
{
cout << "*";
}
cout << endl;
}
cout << endl;
for (int i = 1; i <= n; i++){
for (int j = n; j >= i; j--)
{
cout << "*";
}
cout << endl;
}
cout << endl;
for (int i = 1; i <= n; i++){
for (int j = 2; j <= i; j++)
{
cout << " ";
}
for (int j = n; j >= i; j--)
{
cout << "*";
}
cout << endl;
}
cout << endl;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++)
{
if (i == 1)
cout << "*";
else if (j == 1)
cout << "*";
else if (i == n)
cout << "*";
else if (j == n)
cout << "*";
else
cout << " ";
}
cout << endl;
}
}
My program has errors and wont let me compile. The only errors start at system("pause) though but I dont see why I'm having errors because I did it the same as I always do my programs. Can anyone see what might be the issue? heres the code:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
//Declarations
int SIZE = 10;
int NUMBERS[10];
int i;
int j;
int temp;
for (int i = 0; i < SIZE; i++)
{
cout << "Please enter a number: " << endl;
cin >> NUMBERS[i];
}
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (NUMBERS[j] > NUMBERS[j+1])
{
temp = NUMBERS[j];
NUMBERS[j] = NUMBERS[j+1];
NUMBERS[j+1] = temp;
}
}
}
cout << "Sorted List" << endl;
cout << "===========" << endl;
for (int i = 0; i < SIZE; i++)
cout << "Number " << i + 1 << ": " << NUMBERS[i] << endl;
}
system("pause");
return 0;
}
for (int i = 0; i < SIZE; i++) {
// ^^^ add this missing bracket
cout << "Number " << i + 1 << ": " << NUMBERS[i] << endl;
}
^^^ // closing bracket, but has no opening match
Of course you can skip braces in this case ( as there is only one single line in for body) so this is also solution:
for (int i = 0; i < SIZE; i++)
cout << "Number " << i + 1 << ": " << NUMBERS[i] << endl;