1

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; 
}
Matten
  • 17,365
  • 2
  • 42
  • 64
user1467995
  • 17
  • 1
  • 1
  • 4
  • I don't know how to post the code!!? but here is what i have to make a box ---> – user1467995 Jun 26 '12 at 06:21
  • and heres an example of what i mean about typing in the number --> #include using namespace std; void main() { int number,i,j; cout<<"Enter number:"; cin>>number; for { etc..... – user1467995 Jun 26 '12 at 06:23
  • 1
    To put code in your post, just type it exactly as you would in your text editor (or better yet, copy if from your text editor), then select it all and push the `{}` button. Or alternatively, you can manually indent everything by an additional 4 spaces, which is what the `{}` button does. – Benjamin Lindley Jun 26 '12 at 06:26
  • This should be a good read: http://www2.research.att.com/~bs/bs_faq2.html#void-main – chris Jun 26 '12 at 06:27
  • also i know the middle number in the for loop is where i should put 'number' or 'length' where it asks to enter a number, instead of the y=79 for example it should be y<=width – user1467995 Jun 26 '12 at 06:29

2 Answers2

2

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

  • You don't actually need to return anything from main(). – twsaef Jun 26 '12 at 23:28
  • It's `int main()` (as opposed to `void main()`, which is not even standard) so you have to return something – Mantas Norvaiša Jun 27 '12 at 19:49
  • Actually, no `int main()` is a special case and will `return 0;` if you don't explicitly have a return statement. `void main()` is always wrong as you have said. See http://stackoverflow.com/questions/204476/what-should-main-return-in-c-c – twsaef Jun 27 '12 at 23:19
  • Yes, it seems that you do not have to explicitly `return 0;` as the ISO C++ standard demands that it be added to an `int main()` that doesn't specify the return. But nevertheless it's good practice to return from main. – Mantas Norvaiša Jun 28 '12 at 06:10
0
#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;
}
  • Please, add explanation to your answer. Explain the stack place of the question and comment your solution. – Yurets May 02 '15 at 18:37