dimanche 5 juillet 2020

I'm not able to get the output for the following code

I want to print the numbers like this: 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12| 13| 14| 15| 16|

So, I'm using a code like this:

#include<iostream>
> #include<conio.h>
> #include<cstdlib>
> #include<stdio.h>
> #include<time.h>
> #define GRID_SIZE 4 using namespace std;
> 
> class Game { private: char grid[GRID_SIZE][GRID_SIZE]; public: void
> generateGrid(); void showGrid(); Game(); };
> 
> void Game::generateGrid() { int n = 1;
>        for( int x=0; x<GRID_SIZE; x++)
>       {
>              for( int y= 0; y<GRID_SIZE; y++)
>              {
>                  grid[x][y] = to_string(n).c_str()[0];
>                  n++;
>              }
>       } }
> 
> void Game::showGrid() {
>       printf("------------\n");
>       for(int x=0; x<GRID_SIZE; x++)
>      {
>               for(int y=0; y<GRID_SIZE; y++)
>               {
>                      cout<< " " << grid[x][y] << " |";
>               }
>       cout << "\n------------\n";
>      }
> 
> }
> 
> 
> Game
> 
>     ::Game()
>     {
>              generateGrid()
> 
> ;
>          showGrid(); }
> 
> int main() {
>        Game game;
> 
>       return 0;
> 
> }

I have used to_string(number).c_str()[0]; to convert it into char data type. So that I can change that afterwards.

But here, the output I'm receiving is like: 1| 2| 3| 4| 5| 6| 7| 8| 9| 1| 1| 1| 1| 1| 1| 1|

Sorry for the code like this, I don't know how to write code in stack overflow.

Aucun commentaire:

Enregistrer un commentaire