I'm a beginner in C, and got a crash of my program when it gets to the end.
It is a mini game in which the user have to guess a number. When it's found, the program asks the user if he wants to play a new game.
Please find my 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++;
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;
}
The problem is that the program crashes at the last scanf()
. I also got this warning while compiling :
"passing argument 1 of 'strcmp' makes pointer from integer without a cast"
but I didn't manage to get rid of it.
EDIT :
Thanks for your help guyz, looks like I have to get more focused on my syntax... Now my code is not crashing, I've applied the corrections. The only remaining problem is that this 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... I don't know why but it closes after last printf before the if..
EDIT 2 :
Here is the solution, thanks to @Sourav Gosh
Did you try scanf(" %1c", &newPartie);?, mind the space before %. – Sourav Ghosh.
Point 1
In your code,
while (continuerPartie = 1)
is not doing what you think it's doing. Maybe you wanted to use the equality operator (==
) instead. Otherwise, it's an infinite loop.
That said, you should be initializing continuerPartie
to 1
isntead of 0
to ensure the while()
loop is satified in the first iteration.
Point 2
strcmp(newPartie, "Y" == 0)
is not the proper use.
Either use
strncmp()
with &newPartie
and n
as 1
, or (not a very good approach)==
like if ((newPartie=='Y')|| (newPartie=='y'))
Also, the ==
equality comparison in strcmp()
/strncmp()
should look like strncmp(a,b,c) == 0
Point 3
Change
scanf("%1c", &newPartie);
to
scanf(" %1c", &newPartie);
to ignore the previous newline stored in the input buffer and scan the non-whitespace character.