Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ I';我无法获得以下代码的输出_C++ - Fatal编程技术网

C++ I';我无法获得以下代码的输出

C++ I';我无法获得以下代码的输出,c++,C++,我想这样打印数字: 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12| 13| 14| 15| 16| #include<iostream> #include<conio.h> #include<cstdlib> #include<stdio.h> #include<time.h> #define GRID_SIZE 4 using namespace std; class Game { private

我想这样打印数字:

1| 2| 3| 4|
5| 6| 7| 8|
9| 10| 11| 12|
13| 14| 15| 16|
#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;
}
因此,我使用的代码如下:

1| 2| 3| 4|
5| 6| 7| 8|
9| 10| 11| 12|
13| 14| 15| 16|
#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;
}
问题就在这里

char grid[GRID_SIZE][GRID_SIZE];
您的意思是网格仅由单个字符组成

既然你想让网格由数字组成,为什么不写这个

int grid[GRID_SIZE][GRID_SIZE];
还有这个

grid[x][y] = n;
n++;
grid[x][y] = to_string(n);
n++;
如果您确实希望网格是多个字符,那么您需要
string
而不是
char
,如下所示

string grid[GRID_SIZE][GRID_SIZE];
还有这个

grid[x][y] = n;
n++;
grid[x][y] = to_string(n);
n++;
问题就在这里

char grid[GRID_SIZE][GRID_SIZE];
您的意思是网格仅由单个字符组成

既然你想让网格由数字组成,为什么不写这个

int grid[GRID_SIZE][GRID_SIZE];
还有这个

grid[x][y] = n;
n++;
grid[x][y] = to_string(n);
n++;
如果您确实希望网格是多个字符,那么您需要
string
而不是
char
,如下所示

string grid[GRID_SIZE][GRID_SIZE];
还有这个

grid[x][y] = n;
n++;
grid[x][y] = to_string(n);
n++;

原因就在这一行:

grid[x][y] = to_string(n).c_str()[0];
仅保存一个字符,当n>10时:

n = 10
to_string(n) => "10"
to_string(n).c_str()[0] => "1";

您正在获取字符串的第一个元素,在生成网格时会出现此错误。约翰在上面的回答是解决你问题的好方法。

原因如下:

grid[x][y] = to_string(n).c_str()[0];
仅保存一个字符,当n>10时:

n = 10
to_string(n) => "10"
to_string(n).c_str()[0] => "1";
您正在获取字符串的第一个元素,在生成网格时会出现此错误。上面约翰的回答是解决你问题的好方法