scanf crashes program and comparing string C -


i'm beginner in c, , got crash of program when gets end.

it mini game in user have guess number. when it's found, program asks user if wants play new game.

please find code below :

#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++;      {         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;    } 

the problem program crashes @ last scanf(). got warning while compiling :

"passing argument 1 of 'strcmp' makes pointer integer without cast"

but didn't manage rid of it.

edit :

thanks guyz, looks have more focused on syntax... code not crashing, i've applied corrections. remaining problem code :

    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;     } 

is not executed... don't know why closes after last printf before if..

edit 2 :

here solution, @sourav gosh

did try scanf(" %1c", &newpartie);?, mind space before %. – sourav ghosh.

point 1

in code,

while (continuerpartie = 1) 

is not doing think it's doing. maybe wanted use equality operator (==) instead. otherwise, it's infinite loop.

that said, should initializing continuerpartie 1 isntead of 0 ensure while() loop satified in first iteration.

point 2

strcmp(newpartie, "y" == 0) 

is not proper use.

either use

  • strncmp() &newpartie , n 1, or (not approach)
  • (better) use simple equality operator == if ((newpartie=='y')|| (newpartie=='y'))

also, == equality comparison in strcmp()/strncmp() should strncmp(a,b,c) == 0

point 3

change

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

to

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

to ignore previous newline stored in input buffer , scan non-whitespace character.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -