0

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.

Hal93
  • 11
  • 4
  • 1
    `newPartie` is a `char`.Use `if (newPartie == 'Y') || newPartie == 'y')` instead of `if (strcmp(newPartie, "Y" == 0)|| strcmp(newPartie, "y" == 0))`. `strcmp` requires both its arguments to be of type `char*` and both needs to be NUL-terminated. Also, use`while (continuerPartie == 1)` instead of `while (continuerPartie = 1)`.The former compares `continuerPartie` with 1 while the latter assigns 1 to it(which makes it an infinite loop as 1 is a non-zero number and all non-zero numbers are considered to be true) and change `int continuerPartie = 0;` to `int continuerPartie = 1;` to make loop run. – Spikatrix May 15 '15 at 11:55

2 Answers2

2

There are several errors in your program:

while (continuerPartie = 1)

It continually assigns continuerPartie the value of 1.

Change it to:

while (continuerPartie == 1)

Also, newPartie is a char, not string.

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

should be:

   if ((newPartie=='Y')|| (newPartie=='y'))
Ayushi Jha
  • 4,003
  • 3
  • 26
  • 43
1

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)
  • (better) use simple equality operator == 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.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261