数组打印奇怪的字符,例如 ∟ @
Array prints weird characters such as ∟ @
C++新手,制作井字游戏,我的数组似乎打印了胡言乱语,如下面的输出部分所示。 如何做到这一点,以便我可以用字符'.'填充数组?使用 c++ 11
主.cpp
#include <iostream>
#include "Board.hpp"
//to determine win condition:
//check row
//check column
//check diagonal
//else it's a draw
int main() {
Board board1;
board1.print();
board1.makeMove(0,0,'x');
board1.print();
if(board1.makeMove(0,0,'x'))
std::cout<<"true"<<std::endl;
else
std::cout<<"false"<<std::endl;
std::cout<<"finished!"<<std::endl;
}
板.hpp
#ifndef BOARD_HPP
#define BOARD_HPP
class Board {
private:
char grid[3][3];
public:
Board();
int makeMove(int xIn, int yIn,char playerTurnIn);
void print();
};
#endif //UNTITLED_BOARD_HPP
董事会.cpp
#include "Board.hpp"
#include <iostream>
/*default constructor which initializes an empty array with .*/
Board::Board() {
char grid[3][3] = {{'.','.','.'},{'.','.','.'},{'.','.','.'}};
}
/*declare this in the board class
make sure to add Board:: for makeMove and print functions*/
int Board::makeMove(int xIn, int yIn,char playerTurnIn) {
if (grid[xIn][yIn]=='.') {
grid[xIn][yIn] = playerTurnIn;
return true;
}
else {
return false;
}
}
void Board::print() {
std::cout<<" 0 1 2"<<std::endl;
for (int row = 0; row < 3; row++) {
std::cout<<row<<' ';
for (int col = 0; col < 3; col++) {
std::cout<<grid[row][col]<<' ';
}
std::cout<<std::endl;
}
}
输出:
0 1 2
0 天 1 ( ╗
2 ∟ @ 0 1 2
0 天 1 ( ╗
2 ∟ @
假
完成!
问题出在您的构造函数中:
Board::Board() {
char grid[3][3] = {{'.','.','.'},{'.','.','.'},{'.','.','.'}};
}
在那里,您正在声明和初始化一个新数组。
试试这个:
Board::Board() : grid{{'.','.','.'},{'.','.','.'},{'.','.','.'}} {}
在 Ideone 上进行测试。谢谢@Scheff
相关文章:
- C++字符*缓冲区的大小
- HEX值到wchar_t字符(UTF-8)的转换
- 为什么 Serial.println(<char[]>);返回随机字符?
- 我的字符计数代码计算错误.为什么
- 字符串-C++后显示的随机字符
- 如何返回一串字母数字字符中的最大值(例如-1A003B3)?
- 数组打印奇怪的字符,例如 ∟ @
- 这个字符数组应该如何?(例如C++游览)
- 从文本文件中读取时,即使不存在,也会找到额外的字符(例如"@")?
- C++降低特殊字符,例如 ü
- 将字符数复制到新字符串位置的字符串操作,例如 S[0]
- 如何将常量字符* []隐藏到单个内存块,例如char* ptr
- 在Java中读取字符,例如C
- 在没有字符串或字符帮助的情况下查找数字的反面(例如:2500 reverse 0025)
- c++Unicode字符串与易混淆字符的比较.例如(U 0054)应为==(U03A4)等
- 如何谷歌奇怪的字符,例如"~"内部问题,例如在构造函数之前的 c++ 中这意味着什么?
- 是否可以在 boost::ireplace 中将特殊字符视为基本字符?(例如。 'ź'为"z")
- 反转字符串中的每个字符,特殊字符除外(例如 "?" )
- 如何检查文本文件是否只包含某些字符(例如's'、'f'、#39;#'、'
- 如何更改用户输入字符,例如。"abcDEF"并返回输出"222333"