C
Utilisateur anonyme
-
fiddy Messages postés 11653 Date d'inscription Statut Contributeur Dernière intervention -
fiddy Messages postés 11653 Date d'inscription Statut Contributeur Dernière intervention -
Bonjour, je dois faire un menu en c, je sais qu'il faut faire une boucle qui reprend tout le programme, mais quand je veux quitter le programme, il plante. Je ne sais pas comment faire. Pouvez-vous m'aider?
Merci.
Merci.
5 réponses
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
void afficher (int tabRes[30]);
int main()
{
int touche1=0;
int touche2=0;
int touche3=0;
int nombre=5;
int cpt=0;
int nbTouche=5;
int MAX=0;
int MIN=0;
int tabRes[30];
int i=0;
int nbPartieJouer=0;
int menu=5;
int gagne=0;
while(i<30)
{
tabRes[i]=0;
i++;
}
i=0;
srand(time(NULL));
while(menu!=4||nbPartieJouer==30)
{
menu=0;
while(menu<1||menu>4)
{
MAX=5;
MIN=1;
printf("|-----------------------MENU-------------------------|\n");
printf("|1.Démarrer une nouvelle série |\n");
printf("|2.Afficher l'historique |\n");
printf("|3.Nouveau tirage |\n");
printf("|4.Quitter le programme |\n");
printf("|----------------------------------------------------|\n");
printf("\n");
scanf("%d" , &menu);
}
printf("\n");
if(menu==1)
{
nbPartieJouer=0;
i=0;
while(i<30)
{
tabRes[i]=0;
i++;
}
}
if(menu==2)
{
afficher(tabRes);
printf("\n");
printf("Nombre de partie : %d \n", nbPartieJouer);
if(gagne=0)
printf("Vous perdez\n");
}
touche1=(rand() % (MAX - MIN + 1)) + MIN;
touche2=(rand() % (MAX - MIN + 1)) + MIN;
touche3=(rand() % (MAX - MIN + 1)) + MIN;
printf("Voici le resultat %d, %d, %d \n", touche1, touche2, touche3);
while(nombre<0||nombre>2)
{
printf("Combien de touche voulez-vous modifie maximum 2\n");
scanf("%d", &nombre);
printf("\n");
}
if(nombre==1)
{
while(nbTouche<0||nbTouche>3)
{
printf("Quels touche voulez-vous modifier\n");
printf("\n");
printf("1.pour la première touche\n");
printf("2.pour la deuxième touche\n");
printf("3.pour la troisième touche\n");
printf("\n");
scanf("%d", &nbTouche);
printf("\n");
}
if(nbTouche==1)
touche1=(rand() % (MAX - MIN + 1)) + MIN;
if(nbTouche==2)
touche2=(rand() % (MAX - MIN + 1)) + MIN;
if(nbTouche==3)
touche3=(rand() % (MAX - MIN + 1)) + MIN;
printf("Voici le resultat %d, %d, %d \n" ,touche1, touche2, touche3);
}
if(nombre==2)
{
while(nbTouche<0||nbTouche>3)
{
printf("Quels touche voulez-vous modifier\n");
printf("\n");
printf("1.pour la première touche\n");
printf("2.pour la deuxième touche\n");
printf("3.pour la troisième touche\n");
printf("\n");
scanf("%d", &nbTouche);
printf("\n");
}
if(nbTouche==1)
touche1=(rand() % (MAX - MIN + 1)) + MIN;
if(nbTouche==2)
touche2=(rand() % (MAX - MIN + 1)) + MIN;
if(nbTouche==3)
touche3=(rand() % (MAX - MIN + 1)) + MIN;
printf("Voici le resultat %d, %d, %d \n" ,touche1, touche2, touche3);
nbTouche=5;
while(nbTouche<0||nbTouche>3)
{
printf("Quels touche voulez-vous modifier\n");
printf("\n");
printf("1.pour la première touche\n");
printf("2.pour la deuxième touche\n");
printf("3.pour la troisième touche\n");
printf("\n");
scanf("%d", &nbTouche);
printf("\n");
}
if(nbTouche==1)
touche1=(rand() % (MAX - MIN + 1)) + MIN;
if(nbTouche==2)
touche2=(rand() % (MAX - MIN + 1)) + MIN;
if(nbTouche==3)
touche3=(rand() % (MAX - MIN + 1)) + MIN;
printf("Voici le resultat %d, %d, %d \n" ,touche1, touche2, touche3);
}
if(touche1==touche2)
tabRes[i]=10;
if(touche1==touche3)
tabRes[i]=10;
if(touche2==touche3)
tabRes[i]=10;
if(touche1==touche2)
{
if(touche1==touche3)
{
if(touche2==touche3)
{
printf("Bravo\n");
tabRes[i]=100;
gagne=1;
}
}
}
nbPartieJouer++;
afficher(tabRes);
}
return (0);
}
//parametre en entree:nbpartie
// parametre en sortie:/
void afficher (int tabRes[30]) //fonction affichage
{
int i=0;
for(i=0; i<30; i++) //parcours du tableau pour son affichage
{
printf("%d - ",tabRes[i]);
if (i == 9||i==19) //passage à la ligne suivante
printf("\n");
}
printf("\n");//saut d'une ligne pour une meilleur présentation
}
#include<stdlib.h>
#include <time.h>
void afficher (int tabRes[30]);
int main()
{
int touche1=0;
int touche2=0;
int touche3=0;
int nombre=5;
int cpt=0;
int nbTouche=5;
int MAX=0;
int MIN=0;
int tabRes[30];
int i=0;
int nbPartieJouer=0;
int menu=5;
int gagne=0;
while(i<30)
{
tabRes[i]=0;
i++;
}
i=0;
srand(time(NULL));
while(menu!=4||nbPartieJouer==30)
{
menu=0;
while(menu<1||menu>4)
{
MAX=5;
MIN=1;
printf("|-----------------------MENU-------------------------|\n");
printf("|1.Démarrer une nouvelle série |\n");
printf("|2.Afficher l'historique |\n");
printf("|3.Nouveau tirage |\n");
printf("|4.Quitter le programme |\n");
printf("|----------------------------------------------------|\n");
printf("\n");
scanf("%d" , &menu);
}
printf("\n");
if(menu==1)
{
nbPartieJouer=0;
i=0;
while(i<30)
{
tabRes[i]=0;
i++;
}
}
if(menu==2)
{
afficher(tabRes);
printf("\n");
printf("Nombre de partie : %d \n", nbPartieJouer);
if(gagne=0)
printf("Vous perdez\n");
}
touche1=(rand() % (MAX - MIN + 1)) + MIN;
touche2=(rand() % (MAX - MIN + 1)) + MIN;
touche3=(rand() % (MAX - MIN + 1)) + MIN;
printf("Voici le resultat %d, %d, %d \n", touche1, touche2, touche3);
while(nombre<0||nombre>2)
{
printf("Combien de touche voulez-vous modifie maximum 2\n");
scanf("%d", &nombre);
printf("\n");
}
if(nombre==1)
{
while(nbTouche<0||nbTouche>3)
{
printf("Quels touche voulez-vous modifier\n");
printf("\n");
printf("1.pour la première touche\n");
printf("2.pour la deuxième touche\n");
printf("3.pour la troisième touche\n");
printf("\n");
scanf("%d", &nbTouche);
printf("\n");
}
if(nbTouche==1)
touche1=(rand() % (MAX - MIN + 1)) + MIN;
if(nbTouche==2)
touche2=(rand() % (MAX - MIN + 1)) + MIN;
if(nbTouche==3)
touche3=(rand() % (MAX - MIN + 1)) + MIN;
printf("Voici le resultat %d, %d, %d \n" ,touche1, touche2, touche3);
}
if(nombre==2)
{
while(nbTouche<0||nbTouche>3)
{
printf("Quels touche voulez-vous modifier\n");
printf("\n");
printf("1.pour la première touche\n");
printf("2.pour la deuxième touche\n");
printf("3.pour la troisième touche\n");
printf("\n");
scanf("%d", &nbTouche);
printf("\n");
}
if(nbTouche==1)
touche1=(rand() % (MAX - MIN + 1)) + MIN;
if(nbTouche==2)
touche2=(rand() % (MAX - MIN + 1)) + MIN;
if(nbTouche==3)
touche3=(rand() % (MAX - MIN + 1)) + MIN;
printf("Voici le resultat %d, %d, %d \n" ,touche1, touche2, touche3);
nbTouche=5;
while(nbTouche<0||nbTouche>3)
{
printf("Quels touche voulez-vous modifier\n");
printf("\n");
printf("1.pour la première touche\n");
printf("2.pour la deuxième touche\n");
printf("3.pour la troisième touche\n");
printf("\n");
scanf("%d", &nbTouche);
printf("\n");
}
if(nbTouche==1)
touche1=(rand() % (MAX - MIN + 1)) + MIN;
if(nbTouche==2)
touche2=(rand() % (MAX - MIN + 1)) + MIN;
if(nbTouche==3)
touche3=(rand() % (MAX - MIN + 1)) + MIN;
printf("Voici le resultat %d, %d, %d \n" ,touche1, touche2, touche3);
}
if(touche1==touche2)
tabRes[i]=10;
if(touche1==touche3)
tabRes[i]=10;
if(touche2==touche3)
tabRes[i]=10;
if(touche1==touche2)
{
if(touche1==touche3)
{
if(touche2==touche3)
{
printf("Bravo\n");
tabRes[i]=100;
gagne=1;
}
}
}
nbPartieJouer++;
afficher(tabRes);
}
return (0);
}
//parametre en entree:nbpartie
// parametre en sortie:/
void afficher (int tabRes[30]) //fonction affichage
{
int i=0;
for(i=0; i<30; i++) //parcours du tableau pour son affichage
{
printf("%d - ",tabRes[i]);
if (i == 9||i==19) //passage à la ligne suivante
printf("\n");
}
printf("\n");//saut d'une ligne pour une meilleur présentation
}
quand on veut quitter le pgm, on ne le quitte pas directement on doit d'abord faire une partie et seulement après on peut le quitter.
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
Bonjour,
int tabRes[30];
while(i<30)
{
tabRes[i]=0;
i++;
}
Tu peux mettre simplement : int tabRest[30]={0};
Sinon, par la suite tu peux utiliser memset au lieu de refaire une boucle for et de perdre en efficacité et en lisibilité.
if(gagne=0)
printf("Vous perdez\n");
L'opérateur de comparaison est le double égal : if(gagne==0)
L'avantage ici, est que vous ne perdez jamais ;-))).
Sinon pour le reste, c'est pas évident de voir tant le code est illisible.
Je te conseille de mieux structurer votre code. Tu peux créer plusieurs fonctions et les appeler dans le main.
Ensuite, reposte ton code en l'encapsulant dans deux balises "code" (à droite du bouton "souligné"). Cela permettra de garder l'indentation, essentielle à la lisibilité du code.
Cordialement,
Google is your friend
int tabRes[30];
while(i<30)
{
tabRes[i]=0;
i++;
}
Tu peux mettre simplement : int tabRest[30]={0};
Sinon, par la suite tu peux utiliser memset au lieu de refaire une boucle for et de perdre en efficacité et en lisibilité.
if(gagne=0)
printf("Vous perdez\n");
L'opérateur de comparaison est le double égal : if(gagne==0)
L'avantage ici, est que vous ne perdez jamais ;-))).
Sinon pour le reste, c'est pas évident de voir tant le code est illisible.
Je te conseille de mieux structurer votre code. Tu peux créer plusieurs fonctions et les appeler dans le main.
Ensuite, reposte ton code en l'encapsulant dans deux balises "code" (à droite du bouton "souligné"). Cela permettra de garder l'indentation, essentielle à la lisibilité du code.
Cordialement,
Google is your friend