Creating a grid with for loops and 2D array
Dec 3, 2013 at 7:45am UTC
Hello, I know this might have been asked before, but I cannot find an example that could correctly guide me to where I'm trying to get. So my problem is as follows, I am trying to write a memory matching game for my final project of c++ procedural programming class. And I am working on my grid now, here is the code I have...
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
#include "stdafx.h"
#include <iomanip>
#include <iostream>
using namespace std;
void gridEasy(void );
int _tmain(int argc, _TCHAR* argv[])
{
gridEasy();
system("Pause" );
return 0;
}
void gridEasy(void )
{
for (int r = 0; r < 4; r++)
{
for (int c = 0; c < 4; c++)
{
cout << "-----------------------------" << endl;
cout << "|" << setw(9) << r + 1 << setw(9) << "|" << endl;
cout << "-----------------------------" << endl;
}
}
cout << "--------------------------------------------------------------------------------" ;
}
this is what I get...
------------------------------
| 1 |
------------------------------
------------------------------
| 1 |
------------------------------
------------------------------
| 1 |
------------------------------
------------------------------
| 1 |
------------------------------
------------------------------
| 2 |
------------------------------
------------------------------
| 2 |
------------------------------
------------------------------
| 2 |
------------------------------
------------------------------
| 2 |
------------------------------
------------------------------
| 3 |
------------------------------
------------------------------
| 3 |
------------------------------
------------------------------
| 3 |
------------------------------
------------------------------
| 3 |
------------------------------
------------------------------
| 4 |
------------------------------
------------------------------
| 4 |
------------------------------
------------------------------
| 4 |
------------------------------
------------------------------
| 4 |
------------------------------
------------------------------
but I'm trying to get a 4X4 grid like this...
--------------------------------------------------------------------
| 1 | 2 | 3 | 4 |
--------------------------------------------------------------------
| 5 | 6 | 7 | 8 |
--------------------------------------------------------------------
| 9 | 10 | 11 | 12 |
--------------------------------------------------------------------
| 13 | 14 | 15 | 16 |
--------------------------------------------------------------------
So that the User inputs a number and the number is replaced by a word and then the user can select a second number to compare the two words and see if they match.
I have been looking everywhere and I am desperate... I cannot be able to code this in anyway...Any help will be GREATLY appreciated! Thank you very much!!!
Dec 3, 2013 at 9:20am UTC
You have:
1 2 3 4 5 6 7
for (int r = 0; r < 4; r++)
{
for (int c = 0; c < 4; c++)
{
cout << "foo" << endl;
}
}
You want:
1 2 3 4 5 6 7 8
for (int r = 0; r < 4; r++)
{
for (int c = 0; c < 4; c++)
{
cout << "foo" ;
}
cout << endl;
}
Edit: Multiple lines could be inconvenient, so test:
std::cout << std::setfill( '-' ) << std::setw( 40 ) << "-\n" ;
Last edited on Dec 3, 2013 at 9:24am UTC
Topic archived. No new replies allowed.