簡體   English   中英

由於Visual Studio Ultimate 2013中的scanf_s,程序崩潰了

[英]Program is crashing because of scanf_s in Visual Studio Ultimate 2013

我剛剛開始使用C語言,因為我的大學有一些編程課程。 當我們在Dev C ++程序中編程時,我從高中知道一點C。 現在我們需要使用Visual Studio,我編寫的下一個程序在Dev C ++中工作正常,但在輸入名稱后崩潰了。 在開發C ++中,我從strcatscanf刪除_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_sscanf語義略有不同:對於s[格式,您必須傳遞目標數組的大小:

scanf_s(" %s", name, sizeof(name));

類似地, strcat_s作為目標數組和源字符串之間的第三個參數,即目標數組中元素的數量。

以下是您的代碼的修改版本:

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

int main(void) {
    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, sizeof(name));
        teamForming = rand() % 2;
        if (teamForming == 0) {
            if (numPlayer1 == 8) {
                strcat_s(redTeam, sizeof(redTeam), name);
                strcat_s(redTeam, sizeof(redTeam), " ");
                numPlayer2++;
                break;
            }
            strcat_s(blueTeam, sizeof(blueTeam), name);
            strcat_s(blueTeam, sizeof(blueTeam), " ");
            numPlayer1++;
        } else {
            if (numPlayer2 == 8) {
                strcat_s(blueTeam, sizeof(blueTeam), name);
                strcat_s(blueTeam, sizeof(blueTeam), " ");
                numPlayer1++;
                break;
            }
            strcat_s(redTeam, sizeof(redTeam), name);
            strcat_s(redTeam, sizeof(redTeam), " ");
            numPlayer2++;
        }
    }
    printf("\nBlue team : %s.\n", blueTeam);
    printf("\nRed team : %s.\n", redTeam);
    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM