簡體   English   中英

我的 c 程序一直崩潰,我不知道為什么

[英]My c program keeps crashing and I do not know why

我在 c 中寫了一個像劊子手這樣的程序,我正在嘗試運行它。問題是它工作正常,直到我給它一封信來查詢這個詞,然后它崩潰並顯示 -1073741819 (0xC0000005)。 有人可以幫我解決這個問題嗎,我認為它真的很小,我看不到。 感謝你們對我的幫助!

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

void Rules(void);
void maskWord (char starword[], int size);
int playRound(char starword[], char answer[]);
void updateStarWord(char starword[], char answer[], char userguess);
int occurancesInWord(char userguess, char answer[]);

int c=0;
char answer[128];
int N;

int main()
{
    int ch,size;
    char starword[1000];
    do
    {
        printf("---Welcome to Hangman!---\n");
        printf("1.Start Game\n2.Instructions\n3.Exit\n");
        scanf("%d",&ch);
        if(ch>0&& ch<4)
        {
            switch(ch)
            {
                case 1:
                    maskWord(starword,size);break;
                case 2:
                    Rules();break;
            }
        }
        else
            system("cls");
    }
    while(ch!=3);
    return 0;
}

void Rules(void)
{
    do
    {
        do
        {
            printf("\nThe word to guess is represented by a row of dashes representing each letter of the word.");
            printf("\nRules may permit or forbid proper nouns, such as names, places, brands, or slang.");
            printf("\nIf the guessing player suggests a letter which occurs in the word, the other player writes it in all its correct positions.");
            printf("\nIf the suggested letter does not occur in the word, the other player draws one element of a hanged stick figure as a tally mark.\n");
        }
        while(getchar()!='\n');
    }
    while(getchar()!='\n');
}

void maskWord (char starword[], int size)
{
    printf("Enter word to guess: ");
    fflush(stdout);
    scanf(" %s", answer);
    int N = strlen(answer);
    int mask[N];
    for (int i=0; i < N; ++i)
    {
        mask[i] = 0;
    }
    playRound(mask,N);
}

int playRound(char starword[], char answer[])
{
    // Loop over each round of guessing
    int gameover = 0;
    while (! gameover)
    {
        // Print word with *s for unguessed letters
        printf("The word is : ");
        for(int j=0; j < answer; ++j)
        {
            if (starword[j])
            {
                printf("%c", answer[j]);
            }
            else
            {
                printf("*");
            }
        }
        printf("\n");
        // Get player's next guess
        char guess;
        printf("\nGive a letter: ");
        fflush(stdout);
        scanf(" %c", &guess);
        updateStarWord(starword,answer,guess);
    }

}

void updateStarWord(char starword[], char answer[], char userguess)
{
    // Mark true all mask positions corresponding to guess
    int k;
    for(k=0; k < answer; ++k)
    {
        if ((answer[k]) ==(userguess))
        {
            starword[k] = 1;
        }
    }
}

您的 for 循環沒有意義,因為終止它的條件是k < answer 您正在將 integer ( k ) 與指針 ( answer ) 進行比較。 編譯器應該已經就此警告過您,因此請確保您的編譯器警告已打開並且您正在關注它們。 指針和整數是不同的東西,比較它們幾乎不是你想要做的。

如果answer以 null 結尾,您可能可以用answer[k]替換該條件。 或者updateStarWord可能需要采用一個參數來指示answer的長度,然后條件將是k < answer_length

暫無
暫無

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

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