[英]Why doesn't my array or my function work?
为什么我的数组(或我的函数)不起作用?
我一直在想这个问题很长时间了。
我没有收到来自 GCC 的错误或警告,我想从手卡或玩家卡中移动一张卡,但我没有得到任何可见的结果......
最小可重现示例(我相信它在 function 'centerCardStack' 或数组 'move' 中,在 main() 中编辑):
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
char playerName;
int row, col, toStack;
int validityCheck;
int move[4];
int centerCardsOne, centerCardsTwo, centerCardsThree, centerCardsFour, centerCardsFive;
int playerCardsComOne, playerCardsComTwo, playerCardsComThree, playerCardsComFour, playerCardsComFive;
void centerCardStack(int section, int card, int toCenterStack){
//edit the center stacks from a persons selection
//and edit the persons handCards or playerCards
if(playerName == 'P'){ //test who player is, later code is 'P'
if(section==1){ //test secion. 1 is playercards, 2 is handcards
if(card==1){ //test card. if card is one, first card...
if(toCenterStack==1){ //test which stack
if(playerCardsComOne-1==centerCardsOne) //test if valid
centerCardsOne++;
else //or not
validityCheck=1;
}
if(toCenterStack==2){
if(playerCardsComOne-1==centerCardsTwo)
centerCardsTwo++;
else
validityCheck=1;
}
if(toCenterStack==3){
if(playerCardsComOne-1==centerCardsThree)
centerCardsThree++;
else
validityCheck=1;
}
if(toCenterStack==4){
if(playerCardsComOne-1==centerCardsFour)
centerCardsFour++;
else
validityCheck=1;
}
}
}
}
}
void beginCode(){
srand(time(0));
playerCardsComOne = (rand() % 12) + 1;
playerCardsComTwo = (rand() % 12) + 1;
playerCardsComThree = (rand() % 12) + 1;
playerCardsComFour = (rand() % 12) + 1;
playerCardsComFive = (rand() % 12) + 1;
}
//run the above
int main(){
playerName='P';
beginCode();
printf("handcards: %2d|%2d|%2d|%2d|%2d\n", playerCardsComOne, playerCardsComTwo, playerCardsComThree, playerCardsComFour, playerCardsComFive);
printf("center---: %2d|%2d|%2d|%2d|%2d\n", centerCardsOne, centerCardsTwo, centerCardsThree, centerCardsFour, centerCardsFive);
printf("move> ");
scanf("%d", move);
row=move[0];
col=move[1];
toStack=move[2];
centerCardStack(row, col, toStack);
printf("aftermove: %2d|%2d|%2d|%2d|%2d\n", centerCardsOne, centerCardsTwo, centerCardsThree, centerCardsFour, centerCardsFive);
}
我正在使用 Linux Mint 19.3 64 位,使用 GCC 7.4.0,在配备 Intel Core 2 Duo @ 2.2 Ghz(Intel Centrino)的 Gateway NV78 笔记本电脑上运行。
我还没有看到任何对我有帮助的东西。
谁能帮我? 我从代码中得到的只是头痛......
我找到了答案!
感谢 kaylum 的建议,我使用了scanf("%d%d%d", &row, &col, &toStack);
这解决了我的问题。 我试图scanf("%d", move);
阅读整个 scanf 以分隔移动区域,但这不起作用。
现在,我可以用一个 function 编辑我不同的 int。
谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.