I've started recently to learn how to program in C. I'm following a class online and one of the projects we have to do is create a game where the computer comes up with a random number between two constants, and the user is supposed to find it with the minimal amount of tries possible.
I'm trying to implement a way of counting the tries each time the program passes through the loop (adding +1 to the counter) but all i get is that the number of tries is -472188416 whenever I execute the program as you can see here Program after execution. I don't understand why... Thank you in advance.
Here is the code I made :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
const int MIN = 1;
int nombreMystere = 0, nombreUser = 0, continuerPartie = 1, MAX = 0, Niveau = 0;
srand(time(NULL));
int compteurCoups = 1;
do
{
printf("Bienvenue qu jeu du numero mystere\n");
printf("Trouvez le bon numero en un minimum de coups\n\n");
printf("Tapez 1 pour le mode facile\n");
printf("Tapez 2 pour le mode moyen\n");
printf("tapez 3 pour le mode difficile\n");
switch(Niveau)
{
case 1:
MAX = 10;
nombreMystere = (rand() % (MAX - MIN + 1)) + MIN;
case 2:
MAX = 100;
nombreMystere = (rand() % (MAX - MIN + 1)) + MIN;
case 3:
MAX = 1000;
nombreMystere = (rand() % (MAX - MIN + 1)) + MIN;
default:
printf("Veuillez introduire un nombre entre 1 et 3 : ");
scanf("%d", &Niveau);
break;
}
do
{
printf("Quel est le nombre ? ");
scanf("%d", &nombreUser);
if (nombreUser > nombreMystere)
{
printf("C'est moins !\n");
nombreUser++;
}
else if (nombreUser < nombreMystere)
{
printf("C'est plus !\n");
nombreUser++;
}
else
{
printf("Bravo, vous avez trouve le nombre mystere en %d essais !!!\n"), compteurCoups;
printf("Voulez vous rejouer ?\n");
printf("Si oui appuyer sur 1, sinon si vous voulez quitter appuyez sur 2\n");
scanf("%d", &continuerPartie);
printf("\n");
}
}while (nombreUser != nombreMystere);
}while (continuerPartie == 1);
return 0;
}
You increase nombreuser in stead of compteurCoups.