Prog trouver un nombre !!!
Bonjour,
je souheterais faire un programme pour trouver un nombre , je vous donne ce que j'ai fait , car il y a des erreurs pouvez vous me les corriger ?
Le voici :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #include <stdio.h>
#define f() fflush (stdout)
int main()
{
int i,a,b;
do{
printf("Choisir un nombre entre 0 et 100 : \n");
scanf("%d",&a);
}while(a<=0 || a>=100);
{
printf("Entrer un nombre : \n");f();
scanf("%d",&b);
if (a<b)
printf("Trop grand : \n");f();
else
{
if (a>b)
printf("Trop petit : \n");f();
}
}
printf("Bravo,vous avez trouvé : \n");
return(0);
} |
Merci a vous...
PS: Les accolades ne tombent pas comme il faut lors du copier coller mais vous comprendrez..
[Merci d'utiliser les balises Code et non pas les balises Citation - gangsoleil]
Re: Prog trouver un nombre !!!
Bonjour,
Ton code étant très mal indenté, il y a des problèmes que tu n'as probablement pas vu.
C'est notamment le cas au niveau du if, comme le montre un compilateur bien réglé (j'ai nommé ton prgramme test.c) :
Citation:
> gcc -Wall -pedantic -ansi -O2 test.c
test.c: In function `main':
test.c:18: parse error before "else"
test.c:6: warning: unused variable `i'
test.c:24: warning: control reaches end of non-void function
test.c: At top level:
test.c:25: parse error before string constant
test.c:25: warning: type defaults to `int' in declaration of `printf'
test.c:25: warning: conflicting types for built-in function `printf'
test.c:25: ISO C forbids data definition with no type or storage class
>
VOici ton code indenté de facon correcte :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include <stdio.h>
#define f() fflush (stdout)
int main()
{
int i,a,b;
do{
printf("Choisir un nombre entre 0 et 100 : \n");
scanf("%d",&a);
}while(a<=0 || a>=100);
{
printf("Entrer un nombre : \n");
f();
scanf("%d",&b);
if (a<b)
printf("Trop grand : \n");
f();
else
{
if (a>b)
printf("Trop petit : \n");
f();
}
}
printf("Bravo,vous avez trouvé : \n");
return(0);
} |
Ton problème se situe ici :
Code:
1 2 3 4 5
|
if (a<b)
printf("Trop grand : \n");
f();
else |
Ton else ne correspond à aucun if. Si tu veux exécuter plusieurs instructions suite à un test, il faut absolument faire un bloc d'instructions, ce qui se déclare en C avec des accolades :
Code:
1 2 3 4 5 6 7
|
if (a<b)
{
printf("Trop grand : \n");
f();
}
else |
Re: Prog trouver un nombre !!!
Citation:
Envoyé par wareq
je souheterais faire un programme pour trouver un nombre , je vous donne ce que j'ai fait , car il y a des erreurs pouvez vous me les corriger ?
Remarques et commentaires (-ed-). [En retard, suite à coupure de courant...)]
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include <stdio.h>
/* -ed-
#define f() fflush (stdout)
Non. Obfuscation inutile.
De plus, fflush() stdout est inutile apres un "\n".
<a href="http://emmanuel-delahaye.developpez.com/notes.htm" target="_blank">http://emmanuel-delahaye.developpez.com/notes.htm</a>
suppression
*/
int main ()
{
int
/* -ed-
i,
variable non utilisee...
*/
a, b;
do
{
printf ("Choisir un nombre entre 0 et 100 : \n");
/* -ed-
fonction de saisie extremement fragile,
surtout quand elle est mal utilisee...
(tape 'a<enter>', pour voir...)
<a href="http://emmanuel-delahaye.developpez.com/notes.htm" target="_blank">http://emmanuel-delahaye.developpez.com/notes.htm</a>
<a href="http://emmanuel-delahaye.developpez.com/notes.htm" target="_blank">http://emmanuel-delahaye.developpez.com/notes.htm</a>
*/
scanf ("%d", &a);
/* -ed-
si tu y tiens, il faut tester la valeur retournee et agir en
consequence ...
*/
}
/* -ed-
pas coherent. Tu demande 0 a 100 et tu n'acceptes
que 1 a 99... Revoir le test.
*/
while (a <= 0 || a >= 100);
{
printf ("Entrer un nombre : \n");
scanf ("%d", &b);
if (a < b)
printf ("Trop grand : \n");
else
{
if (a > b)
printf ("Trop petit : \n");
}
}
printf ("Bravo,vous avez trouvé : \n");
/* -ed-
manque une boucle quelque part... avec un compteur d'essais,
(le fameux i non utilise ?)
*/
/* -ed-
return (0);
parentheses inutiles (return n'est pas une fonction)
*/
return 0;
} |
Pose des questions si tu ne comprends pas.