c - 添加另一个循环后执行停止(C,Project Hangman)

标签 c loops execution

您好 stackoverflow 社区, 我是编码新手,刚刚参加了为期两周的 c 类(class)。 我们必须做一个项目,我选择的项目是刽子手。 到目前为止,一切正常,但在添加一个循环(for 或 while)来输出找到的字母后,执行在输入字母后停止(因此在第 68 行之后),不知道为什么。 顺便说一句,我在 win 7 上使用 cygwin。 非常感谢您的帮助。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
unsigned sleep( unsigned seconds );
void Welcome()
{
      printf("      WELCOME\n"); 
      //sleep(1);
}
void showLogo() 
{

      printf("            TO\n"); 
      //sleep(2);
      printf("--------------------------------------------  _______\n");
      printf("| H  H   A   N   N  GGGG M   M   A   N   N |  |/   | \n");
      printf("| H  H  A A  NN  N G     MM MM  A A  NN  N |  |/   | \n");
      printf("| HHHH AAAAA N N N G  GG M M M AAAAA N N N |  |    O \n");
      printf("| H  H A   A N  NN G   G M   M A   A N  NN |  |   \\|/\n");
      printf("| H  H A   A N   N  GGG  M   M A   A N   N |  |    | \n");
      printf("--------------------------------------------  |   / \\\n");
      printf("                                           __|_________\n\n");
}

void RandomWords()
{   
//sleep(2);
FILE *h =  fopen("words.txt","r");
int zeichen = 0;
int anzahl_worte = 0;
if( fscanf(h, "%d\n", &anzahl_worte)!=1) //anzahl_worte(= number of words) been read out of words.txt
{
    printf("No valid word file. Missing number of words.");
}
char** var = calloc(anzahl_worte, sizeof(char*));
for(int i = 0; i < anzahl_worte; i++)
{
    var[i] = calloc(30, sizeof(char)); //every array has now 20 places free
    fscanf(h,"%49s", var[i]); //arrays are being filled with words

}
int zufall = rand() % anzahl_worte;
strcpy(var[0], var[zufall]);//arrays been chosen by random  
zeichen = strlen(var[0]);   
printf("Anzahl der gesuchten Buchstaben: %d\n", zeichen);
printf("Das gesuchte Wort ist übrigens : %s\n", var[0]);
fflush(stdout);
char strich[30];
char* p[30];

for(int i = 0; i < zeichen; i++) //"_" for every letter 
{   
    strich[i] = '_';
    p[i] = &strich[i];
    printf("%c ", strich[i]);
                                //every "_" has a Pointer
                                //should later be replaced with a letter
}
//printf("\n%c\n", *p[0]);
char* letter[1];  //found letter will be saved here
int k = 0;  //position of the found letter in the word
//char** position = calloc(zeichen, sizeof(char**));
char* lpointer; //Addresse of the found letter 

printf("\nGeben Sie bitte Ihren Buchstaben ein\n");
scanf("%s", letter[0]); //Your chosen letter 
lpointer = strpbrk(var[0], letter[0]);  //letters been searched in the word 

if(lpointer != NULL)
{       
    while (lpointer != NULL)
        {
            k = lpointer - var[0];
            printf ("An der %d. Stelle gefunden\n",k+1);
            *p[k] = *lpointer; 
            lpointer = strpbrk(lpointer+1, letter[0]);
        }   


}   

        for(int w = 0; w < zeichen; w++)
 {
     printf("found letters: %c ", *p[w]);
 }
/*printf("Gefundene Buchstaben %c ", *p[0]);
fflush(stdout);
printf("%c ", *p[1]);
fflush(stdout);
printf("%c ", *p[2]);
fflush(stdout);
printf("%c ", *p[3]);
fflush(stdout);
printf("%c ", *p[4]);
fflush(stdout);*/


fflush(h);
fclose(h);



}   




int main (void)
{
    srand(time(NULL));

Welcome();

showLogo();

RandomWords();


}

最佳答案

您对这封信的 scanf 会调用未定义的行为。

char* letter[1];

声明一个由一个 char* 组成的单位初始化数组。然后,scanf 尝试将用户输入放在 letter[0] 指向的位置。它尚未初始化,因此您可以将用户输入放在内存中的任何位置。

实际上,它很可能是一个很小的数字,甚至是 NULL,或者指向尚未分配给您的进程的一 block 内存。无论哪种方式,可能的结果都是段错误。

您需要为要在其中放置输入的字符串分配一些存储空间。

char letter[2]; // Room for one letter and a '\0'

scanf("%1s", letter); // Only scan one letter

letter 可以用在 char* 用作字符串的任何地方,例如:

lpointer = strpbrk(lpointer+1, letter);

关于c - 添加另一个循环后执行停止(C,Project Hangman),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43324428/

相关文章:

c - linux下如何创建makefile

javascript - 有没有办法让javascript循环重复按下按钮?

javascript - 如何以三人为一组数数(图案)

JQuery 循环函数仅适用于最后一个元素

c++ - 在后台启动程序

python - Maya 脚本编辑器中的执行行范围

c - 如何使用 vim 获取整个项目中文本的所有引用

C中的自定义字符编码

c - undefined symbol : PyExc_ImportError when embedding Python in C

python - C函数执行在哪里停止