How to draw this in C + +?
Jul 25, 2014 at 6:10pm UTC
Hi, I was doing some c + + and I'm doing the typical game of battleship, my question is, to draw the ocean, so it would be like
-->
http://subefotos.com/ver/?7d6b6617a03939b6d253b55ee1a678fdo.png
+--------+
| | | | |
|-+-+-+-|
| | | | |
|-+-+-+-|
| | | | |
|-+-+-+-|
| | | | |
+--------+
How do I get the rows and columns are displayed?
here is my piece of code
Thank you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#include <iostream>
using namespace std;
int main ()
{
int columns;
int rows;
cout<<"Enter the number of rows: " <<endl;;
cin>>rows;
cout<<"Enter the number of columns:" ;
cin>>columns;
for (int i = 0; i < rows; i++)
{
if (i == 0 || i == rows -1)
{
cout <<"+" ;
}
else
{
cout<<"-" ;
}
}
cout << endl;
for (int k = 0; k <columns; k++)
{
for (int j = 0; j < columns; j++)
{
if (j ==0 || j == columns-1)
cout <<"|" ;
else
{
cout <<" | " ;
}
}
cout << endl;
}
for (int i = 0; i < rows; i++)
if (i == 0 || i == rows - 1)
cout <<"+" ;
else
cout<<"-" ;
cout<<endl;
system("PAUSE" );
return 0;
}
Last edited on Jul 25, 2014 at 6:33pm UTC
Jul 25, 2014 at 6:29pm UTC
Lines 8-12: Is there some reason you've reversed rows and columns?
Makes your program hard to follow.
Something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#include <iostream>
using namespace std;
int main ()
{ int cols;
int rows;
cout<<"Enter the number of rows: " ;
cin>>rows;
cout<<"Enter the number of columns: " ;
cin>>cols;
for (int i = 0; i <= cols; i++)
{ if (i == cols)
cout<<"+" ;
else
cout<<"+-" ;
}
cout << endl;
for (int k = 0; k <rows; k++)
{ for (int j = 0; j <= cols; j++)
{ cout <<"| " ;
}
cout << endl;
}
for (int i = 0; i <= cols; i++)
{ if (i == cols)
cout<<"+" ;
else
cout<<"+-" ;
}
cout<<endl;
system("PAUSE" );
return 0;
}
Enter the number of rows: 5
Enter the number of columns: 6
+-+-+-+-+-+-+
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
+-+-+-+-+-+-+
Last edited on Jul 25, 2014 at 6:40pm UTC
Jul 25, 2014 at 6:35pm UTC
thank you very much.
but, if you look on the picture to place in the post, into the ocean foul separate rows.
Jul 25, 2014 at 10:35pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <iostream>
#include <iomanip>
using namespace std;
void draw_horiz_line (int cols)
{ cout << " " ;
for (int i = 0; i <= cols; i++)
{ if (i == cols)
cout<<"+" ;
else
cout<<"+--" ;
}
cout << endl;
}
void draw_col_headings (int cols)
{ cout << " " ;
for (int i=0; i<cols; i++)
cout << setw(3) << i;
cout << endl;
}
int main ()
{ int cols;
int rows;
cout<<"Enter the number of rows: " ;
cin>>rows;
cout<<"Enter the number of columns: " ;
cin>>cols;
draw_col_headings (cols);
draw_horiz_line (cols);
for (int k = 0; k <rows; k++)
{ cout << setw(2) << k;
for (int j = 0; j <= cols; j++)
{ cout <<"| " ;
}
cout << endl;
draw_horiz_line (cols);
}
system("PAUSE" );
return 0;
}
Enter the number of rows: 5
Enter the number of columns: 6
0 1 2 3 4 5
+--+--+--+--+--+--+
0| | | | | | |
+--+--+--+--+--+--+
1| | | | | | |
+--+--+--+--+--+--+
2| | | | | | |
+--+--+--+--+--+--+
3| | | | | | |
+--+--+--+--+--+--+
4| | | | | | |
+--+--+--+--+--+--+
Last edited on Jul 25, 2014 at 10:37pm UTC
Topic archived. No new replies allowed.