【问题标题】:scanf crashes program and comparing string Cscanf 使程序崩溃并比较字符串 C
【发布时间】:2015-05-15 11:46:50
【问题描述】:

我是 C 的初学者,当程序运行到最后时,我的程序崩溃了。

这是一个用户必须猜数字的迷你游戏。找到后,程序会询问用户是否想玩新游戏。

请在下面找到我的代码:

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

int main() {
int MAX = 100, MIN = 0;
int nombreCoup = 0;
int nombreMystere = 0;
int nombreJoueur;
char newPartie;
int continuerPartie = 0;
int niveauDiffic;

srand(time(NULL));
nombreMystere = (rand() % (MAX - MIN + 1)) + MIN;

printf("Bonjour, ceci est un mini jeu dans lequel vous allez devoir deviner un nombre choisi aleatoirement entre 0 et 100.:\n");

while (continuerPartie = 1)
{

    printf("Choisissez votre niveau de difficulte entre 1 et 3 :\n");
    scanf("%d", &niveauDiffic);

    switch (niveauDiffic)
    {
        case 1:
            printf("Vous avez selectionne la difficulte Facile. Le jeu commence.\n");
            break;
        case 2:
            printf("Vous avez selectionne la difficulte Normale. Le jeu commence.\n");
            niveauDiffic = 2;
            MAX = 1000;
            break;
        case 3:
            printf("Vous avez selectionne la difficulte Difficile. Le jeu commence.\n");
            niveauDiffic = 3;
            MAX = 10000;
            break;
        default:
            printf("Mauvaise entrée, la difficulte sera donc reglee sur Facile. \n");
            break;          
    }


    printf ("\n On y va : \n");
    scanf ("%d", &nombreJoueur);
    nombreCoup++;

    do {
        if (nombreJoueur > nombreMystere) {
            printf ("Le nombre mystere est plus petit !\n");
            nombreCoup = nombreCoup + 1 ;
            printf("Retente ta chance !  \n \n");
            scanf ("%d", &nombreJoueur);
        }

        else {
            printf ("Le nombre mystere est plus grand ! \n");
            nombreCoup = nombreCoup + 1 ;
            printf("Retente ta chance ! \n \n");
            scanf ("%d", &nombreJoueur);    
        }


    } while (nombreJoueur != nombreMystere);

    printf ("Vous avez trouver le nombre mystere en %d coups ! \nVoulez vous rejouer ? (Y/N)  ", nombreCoup);
    scanf("%1c", &newPartie);
    printf ("%c", newPartie);

    if (strcmp(newPartie, "Y" == 0)|| strcmp(newPartie, "y" == 0))
    {
        printf ("Et c'est reparti !!'");
        }

    else    {
        continuerPartie = 0;
        }

}
return 0;   
}

问题是程序在最后一个scanf() 崩溃。我在编译时也收到了这个警告:

“传递 'strcmp' 的参数 1 使指针从整数不进行强制转换”

但我没能摆脱它。

编辑:

感谢您的帮助 guyz,看来我必须更加专注于我的语法......现在我的代码没有崩溃,我已经应用了更正。唯一剩下的问题是这段代码:

    printf ("Vous avez trouver le nombre mystere en %d coups ! \nVoulez vous rejouer ? (Y/N)  ", nombreCoup);
scanf("%1c", &newPartie);
printf ("%c", newPartie);

if ((newPartie == 'Y') || newPartie == 'y')
{
    printf ("Et c'est reparti !!'");
    }

else    {
    continuerPartie = 0;
    }

未执行...我不知道为什么,但它在 if 之前的最后一个 printf 之后关闭..

编辑 2:

感谢@Sourav Gosh,这是解决方案

您是否尝试过 scanf(" %1c", &newPartie);?,注意 % 之前的空格。 – 苏拉夫·戈什。

【问题讨论】:

  • newPartie 是一个char。使用if (newPartie == 'Y') || newPartie == 'y') 而不是if (strcmp(newPartie, "Y" == 0)|| strcmp(newPartie, "y" == 0))strcmp 要求它的两个参数都是 char* 类型,并且都需要 NUL 终止。此外,使用while (continuerPartie == 1) 而不是while (continuerPartie = 1)。前者将continuerPartie 与1 进行比较,而后者将1 分配给它(这使得它成为一个无限循环,因为1 是一个非零数字并且所有非零数字都被考虑是真的)并将int continuerPartie = 0;更改为int continuerPartie = 1;以使循环运行。

标签: c string segmentation-fault equality strcmp


【解决方案1】:

你的程序有几个错误:

while (continuerPartie = 1)

它不断地为 continuerPartie 赋值 1。

改成:

while (continuerPartie == 1)

另外,newPartie 是一个字符,而不是字符串。

    if (strcmp(newPartie, "Y" == 0)|| strcmp(newPartie, "y" == 0))

应该是:

   if ((newPartie=='Y')|| (newPartie=='y'))

【讨论】:

    【解决方案2】:

    第 1 点

    在您的代码中,

    while (continuerPartie = 1)
    

    没有做你认为它正在做的事情。也许您想改用 等式运算符 (==)。否则就是死循环。

    也就是说,您应该将 continuerPartie 初始化为 1 而不是 0,以确保在第一次迭代中满足 while() 循环。

    第 2 点

    strcmp(newPartie, "Y" == 0)
    

    不是正确的用途。

    随便用

    • strncmp()&amp;newPartien 作为 1,或者(不是一个很好的方法
    • (更好) 使用简单的相等运算符== 就像if ((newPartie=='Y')|| (newPartie=='y'))

    另外,strcmp()/strncmp() 中的 == 相等比较应该类似于 strncmp(a,b,c) == 0

    第 3 点

    改变

      scanf("%1c", &newPartie);
    

     scanf(" %1c", &newPartie);
    

    忽略存储在输入缓冲区中的前一个换行符扫描非空白字符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-03
      • 1970-01-01
      相关资源
      最近更新 更多