分段故障字符阵列
Segmentation Fault Character arrays
我正试图从C++编程书中编写一个简单的TicTacToe游戏作为练习。这应该在不使用指针或向量的情况下完成,因为我只完成了数组一章。当我尝试运行代码时,遇到了分段错误。这是我的密码。。
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
void displayArray( char array[3][3] );
main ()
{
char board[][3] =
{
{' ',' ',' '},
{' ',' ',' '},
{' ',' ',' '}
};
int p1;
int p2;
int j2;
int j1;
int i2;
int i1;
for ( int i = 0; i < 9; i++)
{
if( pow(-1,i) == 1 )
{
while ( board[i1][j1] != ' ')
{
cout << "Player 1 insert your indicesn";
cin >> p1 ;
i1 = floor(p1%10);
j1 = p1%10;
}
board[i1][j1] = 'X';
}
else
{
while ( board[i1][j1] != ' ')
{
cout << "Player 2 insert your indicesn";
cin >> p2 ;
i2 = floor(p2%10);
j2 = p2%10;
}
board[i1][j1] = 'O';
}
}
displayArray( board );
}
void displayArray( char array[3][3] )
{
cout << "Board :n" ;
for ( int i = 0; i < 3; i++ )
{
cout << array[i][0] << 't' << array[i][2] << 't' << array[i] [2] << 'n';
}
cout << 'n' ;
}
while ( board[i1][j1] != ' ')
CCD_ 1和CCD_。因此,当你做board[i1][j1]
时,你可能正在触及未知的领域。这在c++中被称为Undefined behavior
,如果您的代码这样做,运行时可以自由地做任何它想做的事情。在大多数情况下,这是一个细分故障。
还有这个部分:
while ( board[i1][j1] != ' ')
{
cout << "Player 2 insert your indicesn";
cin >> p2 ;
i2 = floor(p2%10);
j2 = p2%10;
}
是一个无限循环。你的意思是写i2
和j2
吗?
相关文章:
- 如何打印一个字符阵列和其中的特定字符
- 比strncpy更好地复制字符阵列的部分方法
- 在字符阵列C 中存储整数值
- C 程序仅在字符阵列获得“ 1000!”的输入时才在DELETE上崩溃
- 将动态阵列中的字符与char进行比较
- 界面设计:超载功能的安全性采用字符串和字符阵列
- 可以使用工会将字符阵列转换为浮动
- 将字符串阵列中的字符转换为浮点
- 我如何将整数转换为存储在字符阵列中的十六进制字节
- 访问字符阵列中不可用的内存位置(超出范围值)
- 需要将单词的文本文件存储到C 中的字符阵列中
- 恒定字符阵列的获取字符串长度函数(strlen)不是恒定表达式
- MBED RTOS将Float转换为字符串/字符阵列
- 从指针打印动态int阵列中获取怪异的字符
- 如何将整数存储在字符阵列中
- 全球字符阵列(2D)
- 如何将一个数字(大于8个字节)从字符阵列转换为其ASCII表示
- 替换字符阵列C 中的字符
- 字符阵列未正确初始化
- 向字符阵列宣布指针