Emmanuel Delahaye C is a sharp tool |
Tu peux commencer par corriger ceci :
-------------- Build: Debug in hello ---------------
Compiling: main.c
Linking console executable: bin\Debug\hello.exe
C:\dev\hello\main.c:7: warning: function declaration isn't a prototype
C:\dev\hello\main.c: In function `main':
C:\dev\hello\main.c:8: warning: implicit declaration of function `srand'
C:\dev\hello\main.c:9: warning: implicit declaration of function `rand'
C:\dev\hello\main.c:9: warning: ISO C90 forbids mixed declarations and code
C:\dev\hello\main.c:10:23: warning: character constant too long for its type
C:\dev\hello\main.c:21: warning: empty body in an if-statement
C:\dev\hello\main.c:22: error: subscripted value is neither array nor pointer
C:\dev\hello\main.c:23: error: syntax error before "else"
C:\dev\hello\main.c:27: warning: format argument is not a pointer (arg 2)
C:\dev\hello\main.c:28: warning: implicit declaration of function `recherchechiffre'
C:\dev\hello\main.c:39: warning: ISO C forbids nested functions
C:\dev\hello\main.c:48: warning: ISO C90 forbids mixed declarations and code
C:\dev\hello\main.c:50: warning: ISO C forbids nested functions
C:\dev\hello\main.c: In function `recherchechiffre':
C:\dev\hello\main.c:53: error: subscripted value is neither array nor pointer
C:\dev\hello\main.c:55: error: subscripted value is neither array nor pointer
C:\dev\hello\main.c:63: error: syntax error before '}' token
Process terminated with status 1 (0 minutes, 0 seconds)
5 errors, 11 warnings
|
Quelques corrections, et surtout, des commentaires :
Code :
- /*
- Voilà on a un devoir à faire sur la création d'un jeu Mastermind, l'énoncé est
- le suivant :
-
- Le jeu se présente sous la forme d'un plateau ayant 12 rangées de 5 trous
- pouvant accueillir des pions de numéros différents (1, 2, 3, 4, 5, 6, 7, 8) :
- * L'ordinateur fabrique une combinaison secrète de 5 pions choisies parmis 8
- numéros.
- * L'utilisateur cherche par tatonnements succéssifs, à trouver la combinaison
- secrète. Après chaque essai, l'ordinateur donne 2 explications :
- * le nombre de pions bien choisies et bien placées. Pour cela il
- affiche le chiffre 2 à la position du numéros correcte.
- * le nombre de pions bien choisies mais mal placées. Pour cela il
- affiche le chiffre 1 à la position du numéros.
- * Sinon sur les autres pions il affiche 0.
- 12 essais maximum.
-
- */
- #include<stdio.h>
- #include<time.h>
- /* -ed- ajoute */
- #include<stdlib.h>
- /* -ed- eviter les prototypes separes dans le meme ficiher source. Travailler
- si la disposition du code. Les fonctions non exportéee sont 'static'.
-
- int gagne (int chiffretrouve[]);
- int rechercherchiffre (int a, int combinaison[], int chiffretrouve[]);
- */
- /* -ed- eviter les globales. Au moins les rendre 'static' */
- static int lirechiffre;
- static int a, b;
- static int gagne (int chiffretrouve[])
- {
- long i = 0;
- int joueurgagne = 1;
- for (i = 0; i < 5; i++)
- {
- if (chiffretrouve[i] == 0)
- joueurgagne = 0;
- }
- return joueurgagne;
- }
- static int recherchechiffre (int c, int combinaison, int chiffretrouve[])
- {
- long i = 0;
- int bonchiffre = 0;
- /* -ed- absurde. 'combinaison' n'est pas un tableau.
- A corriger. Je ne sais pas ce que tu veux faire... */
- for (i = 0; combinaison[i] != 0; i++)
- {
- if (c == combinaison[i])
- {
- bonchiffre = 1;
- chiffretrouve[i] = 1;
- }
- return bonchiffre;
- }
- /* -ed- ajoute ';' */
- return (rand () % (b - a) + a);
- }
- /* -ed- ajoute 'void' (pas de parametres) */
- int main (void)
- {
- srand (time (NULL));
- {
- /* -ed- en C90, les variables doivent etre definies en debut de bloc */
- int c = rand () % 8;
- /* -ed-
- int combinaison = '45738';
-
- ceci n'a aucun sens. C'est soit
- int combinaison = 45738;
-
- soit plus vraisemblablement :
- */
- char combinaison[5] = "45738";
- int chiffretrouve[5] = { 0 };
- long couprestant = 12;
- long i = 0;
- return 0;
- while (couprestant > 12 && !gagne (chiffretrouve))
- {
- printf ("\n\n Quel est la combinaison? \n\n" );
- for (i = 0; i < 5; i++)
- {
- /* -ed- supprime ';' */
- if (chiffretrouve[i])
- printf ("\n\n %i \n\n", combinaison[i]);
- else
- printf ("\n\n 0 \n\n" );
- }
- printf ("\n\n Entrez 5 chiffres entre 1 et 8 \n\n" );
- c = scanf ("%i", c);
- /* -ed- Huh ! Je vois mal comment on peut entrer 5 chiffres avec ce code.
- de plus, la valeur retournée par scanf() n'est pas ce que tu crois.
- Il faut relire la doc... Enfin, scanf() attends une adresse.
-
- De toutes façons, ton approche globale est à revoir. Il faut
- abandonner l'idée que la combinaison est une valeutr numérique,
- mais il faut plutût la traiter conne une chaine de caractères.
- */
- if (!recherchechiffre (c, combinaison, chiffretrouve))
- {
- couprestant--;
- }
- if (gagne (chiffretrouve))
- printf ("\n\n Gagné la combinaison était bien : %i \n\n",
- combinaison);
- else
- printf ("\n\n Perdu la combinaison était : %i \n\n", combinaison);
- }
- }
- return 0;
- }
|
Message édité par Emmanuel Delahaye le 01-04-2009 à 09:16:28 ---------------
Des infos sur la programmation et le langage C: http://www.bien-programmer.fr Pas de Wi-Fi à la maison : http://www.cpl-france.org/
|