[英]C Program doesn't continue to follow code in main() function after calling a function inside of it
Won't execute the following code in the main() function.不会在 main() function 中执行以下代码。 Any input is appreciated.任何输入表示赞赏。 PS I was in the beginning stages of creating a tic-tac-toe-game. PS 我正处于创建井字游戏的初期阶段。
void printBoard(int slotNumber);
int main()
{
int slotNumberMain = -1;
printf("prints this\n");
printBoard(slotNumberMain);
printf("but not this\n");
return 0;
}
void printBoard(int slotNumber)
{
int boardSlots[3][3];
int i;
int j = 0;
for (i = 1; i <= 9; i++){
char box[4] = {'[', ' ', ']', '\0'};
boardSlots[j][i - 1] = i;
if (slotNumber == i){
char box[4] = {'[', 'X', ']', '\0'};
}
printf("%s", box);
if (i % 3 == 0){
j++;
printf("\n");
}
}
}
Edit: The reason there is a slotNumber variable is because originally I had added a enterNumber() function (which would be called at the end of printBoard() and have had you enter a number and mark the box with an 'x' after recalling printBoard(slotNumberMain)) however I haven't added the code here, because I realized that the problem was strictly within printBoard() and main.)编辑:有一个 slotNumber 变量的原因是因为最初我添加了一个 enterNumber() function (将在 printBoard() 的末尾调用,并让您输入一个数字并在召回后用“x”标记框printBoard(slotNumberMain)) 但是我没有在这里添加代码,因为我意识到问题完全在 printBoard() 和 main 中。)
Edit 2: I have solved the issue, it was due to the boardSlots array being overrun like someone had suggested.编辑 2:我已经解决了这个问题,这是由于 boardSlots 数组像有人建议的那样被溢出。
Thanks to everyone who attempted to help.感谢所有试图提供帮助的人。
I have solved the issue, it was due to the boardSlots array being overrun like someone had suggested.我已经解决了这个问题,这是由于 boardSlots 数组像有人建议的那样被溢出。
void printBoard(int slotNumber)
{
int boardSlots[3][3];
int i;
int j = 1;
char box[4] = {'[', ' ', ']', '\0'};
for (i = 1; i <= 3 && j <= 3; i++){
boardSlots[j - 1][i - 1] = i * j;
if (slotNumber == i){
char box[4] = {'[', 'X', ']', '\0'};
}
printf("%s", box);
if (i == 3){
j++;
i = 0;
printf("\n");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.