【发布时间】:2016-06-28 19:10:49
【问题描述】:
我刚刚开始学习 C 语言,因为我的大学有一些编程课程。当我们在 Dev C++ 程序中编程时,我从高中开始就对 C 有一点了解。现在我们需要为此使用 Visual Studio,我编写的下一个程序在 Dev C++ 中运行良好,但在输入名称后崩溃。在开发 C++ 中,我从 strcat 和 scanf 中删除了 _s,它运行良好。有人可以帮我解决这个问题吗?这个程序是关于输入一个名字的,它应该随机地将你安排在这两个团队中的一个中。
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
int main() {
int maxPlayers = 16, player, teamForming, numPlayer1 = 0, numPlayer2 = 0;
char blueTeam[256] = "", redTeam[256] = "", name[16];
for (player = 1; player <= maxPlayers; player++) {
printf("\nEnter a name of %d. player : ", player);
scanf_s(" %s", name);
teamForming = rand() % 2;
if (teamForming == 0) {
if (numPlayer1 == 8) {
strcat_s(redTeam, name);
strcat_s(redTeam, " ");
numPlayer2++;
break;
}
strcat_s(blueTeam, name);
strcat_s(blueTeam, " ");
numPlayer1++;
} else {
if (numPlayer2 == 8) {
strcat_s(blueTeam, name);
strcat_s(blueTeam, " ");
numPlayer1++;
break;
}
strcat_s(redTeam, name);
strcat_s(redTeam, " ");
numPlayer2++;
}
}
printf("\nBlue team : %s.\n", blueTeam);
printf("\nRed team : %s.\n", redTeam);
return 0;
}
【问题讨论】:
-
我相信
scanf_s需要s、S、c和C格式的两个参数。第一个是结果指针(同scanf),第二个是缓冲区大小参数。
标签: c visual-studio scanf dev-c++