IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

Appeller une fonction


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Inscrit en
    Août 2004
    Messages
    201
    Détails du profil
    Informations forums :
    Inscription : Août 2004
    Messages : 201
    Par défaut Appeller une fonction
    Bonjour

    c tout simple, je voudrais appeller une fonction a l'interieur d'une autre, mais quand je compile jai pleins d'erreurs

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    #include <stdio.h>
    int main()
    {
    int i, num, choice;
     
    printf("1,2,3,4 ? ");
    scanf("%i", & choice);
     
     
    EnteringChoice(){  
    printf("enter a number: ");
    scanf("%i", & num);
    }
     
    if (choice == 1){
     
    printf("counting forward from 1 to %i", num);
    for (i=0 ; i <= num; i++)
    printf("\n %i", i);
    }
     
    if (choice == 2){
     
    printf("counting backwards from %i to 1", num);
    for (i=0 ; i <= num; i++)
    printf("\n %i", i);
    }
     
    if (choice == 3){
     
    printf("Displaying odd numbers from 1 to %i", num);
    }
     
    if (choice == 4){
     
    printf("Displaying even numbers from 1 to %i", num);
     
    }
    Je voudrais appeller la fonction EnteringChoice a l'interieur de chaque if statement

    Comment faire ?

    Merci

  2. #2
    Expert éminent
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 395
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 395
    Par défaut
    On n'appelle pas une fonction ainsi.
    Là, tu as essayé de définir la fonction dans le main : très mauvais.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    #include <stdio.h>
     
    int EnterNum(void)
    {  
    	int res;
    	int num;
     
    	printf("enter a number: ");
    	fflush(stdout);
    	res = scanf("%i", & num);
    	if(res != 1)
    		num = 0;
    	return num;
    }
     
    int main(void)
    {
    	int i, num, choice;
     
    	printf("1,2,3,4 ? ");
    	fflush(stdout);
    	scanf("%i", & choice);
     
    	if(choice == 1)
    	{
    	num = EnterNum();
    	printf("counting forward from 1 to %i", num);
    	for(i=0 ; i <= num; i++)
    		printf("\n %i", i);
    	}
     
    	if(choice == 2)
    	{
    	num = EnterNum();
    	printf("counting backwards from %i to 1", num);
    	for(i=0 ; i <= num; i++)
    		printf("\n %i", i);
    	}
     
    	if(choice == 3)
    	{
    	num = EnterNum();
    	printf("Displaying odd numbers from 1 to %i", num);
    	}
     
    	if(choice == 4)
    	{
    	num = EnterNum();
    	printf("Displaying even numbers from 1 to %i", num);
    	}
    	return 0;
    }
    Plus d'infos ici sur les fonctions d'entrées-sorties :
    http://emmanuel-delahaye.developpez....#fflush_stdout
    http://emmanuel-delahaye.developpez....tes.htm#saisie
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  3. #3
    Membre chevronné Avatar de nemo69500
    Profil pro
    Inscrit en
    Juin 2005
    Messages
    639
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2005
    Messages : 639
    Par défaut
    et pour optimiser un peu ton code essaye de te renseigner sur les switch , case , se sera boucoup mieu que les if if if if

  4. #4
    Expert confirmé
    Avatar de Thierry Chappuis
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Mai 2005
    Messages
    3 499
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Suisse

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 499
    Par défaut
    Citation Envoyé par nemo69500
    et pour optimiser un peu ton code essaye de te renseigner sur les switch , case , se sera boucoup mieu que les if if if if
    Ou alors:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    if (condition)
    {
        /* ... */
    }
    else if (condition)
    {
        /* ... */
    }
    else if (condition)
    {
        /* .. */
    }
    else
    {
        /* ... */
    }
    J'ai pas regardé le code généré pour comparer cette structure à un switch, mais pour moi, c'est équivalent.

    Thierry
    "The most important thing in the kitchen is the waste paper basket and it needs to be centrally located.", Donald Knuth
    "If the only tool you have is a hammer, every problem looks like a nail.", probably Abraham Maslow

    FAQ-Python FAQ-C FAQ-C++

    +

  5. #5
    Membre confirmé
    Inscrit en
    Août 2004
    Messages
    201
    Détails du profil
    Informations forums :
    Inscription : Août 2004
    Messages : 201
    Par défaut
    Merci pour votre aide les amis.

    je voulais poser deux autre questions:

    1. est ce que le void est vraiment utile ?
    2. comment faire pour inviter l'utilisateur a appuyer sur n'importe quelle touche pour continuer, apres que chaque if statement ait ete effectue ? (cad, rester dans le programme pour effectuer un autre choix et ne quitter que par le choix 5)
    Merci

    Mon code maintenant ressemble a ceci:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    79
    80
    81
    82
    83
    84
    85
    86
    87
    #include <stdio.h>
    int EnterNum(void)
    {
            int res;
            int num;
     
            printf("\n\t\t   Enter a number: ");
            fflush(stdout);
            res = scanf("%i", & num);
            if(res != 1)
                    num = 0;
            return num;
    }
     
    int CountForward(void)
    {
            int i, num, count;
            for(i = 0; i <= num; i++)
            count = printf("\n %i", i);
    }
     
    int CountBackwards(void)
    {
            int i, num, count;
            for(i= num; i >= 0; i--)
            count = printf("\n %i", i);
    }
     
    int CountEven(void)
    {
            int i, num, count;
            for(i = 0; i <= num; i = i + 2)
            count = printf("\n %i", i);
    }
     
    int CountOdd(void)
    {
            int i, num, count;
            for(i = 1; i <= num; i = i + 2)
            count = printf("\n %i", i);
    }
     
    int main(void)
    {
            int i, num, count, choice;
     
            printf("\t\t1. Count forward\n");
            printf("\t\t2. Count backwards\n");
            printf("\t\t3. Show even numbers\n");
            printf("\t\t4. Show odd numers\n");
            printf("\t\t5. Quit\n\n");
            printf("\t\t   Enter your choice: ");
            fflush(stdout);
            scanf("%i", & choice);
     
            if(choice == 1)
            {
            num = EnterNum();
            printf("counting forward from 1 to %i", num);
            count = CountForward();
            }
     
            if(choice == 2)
            {
            num = EnterNum();
            printf("counting backwards from %i to 1", num);
            count = CountBackwards();
            }
     
            if(choice == 3)
            {
            num = EnterNum();
            printf("Displaying odd numbers from 1 to %i", num);
            count = CountOdd();
            }
     
            if(choice == 4)
            {
            num = EnterNum();
            printf("Displaying even numbers from 1 to %i", num);
            count = CountEven();
            }
     
            if(choice == 5)
            {
            return 0;
            }

  6. #6
    Membre expérimenté Avatar de Ksempac
    Inscrit en
    Février 2007
    Messages
    165
    Détails du profil
    Informations forums :
    Inscription : Février 2007
    Messages : 165
    Par défaut
    Utilise une boucle do-while qui boucle tant que choice ne vaut pas 5. Mais tu n'ecoutes pas grand chose, les posteurs au-dessus t'ont deja fait remarqué que tes "if-if-if-if" pouvaient etre remplacé par un "switch case" ou un "if-else if-else if-else" afin d'ameliorer les performances

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    79
    80
    81
    82
    83
    84
    85
    86
    87
    #include <stdio.h>
    int EnterNum(void)
    {
            int res;
            int num;
     
            printf("\n\t\t   Enter a number: ");
            fflush(stdout);
            res = scanf("%i", & num);
            if(res != 1)
                    num = 0;
            return num;
    }
     
    int CountForward(void)
    {
            int i, num, count;
            for(i = 0; i <= num; i++)
            count = printf("\n %i", i);
    }
     
    int CountBackwards(void)
    {
            int i, num, count;
            for(i= num; i >= 0; i--)
            count = printf("\n %i", i);
    }
     
    int CountEven(void)
    {
            int i, num, count;
            for(i = 0; i <= num; i = i + 2)
            count = printf("\n %i", i);
    }
     
    int CountOdd(void)
    {
            int i, num, count;
            for(i = 1; i <= num; i = i + 2)
            count = printf("\n %i", i);
    }
     
    int main(void)
    {
            int i, num, count, choice;
            do
            {
                      printf("\t\t1. Count forward\n");
                     printf("\t\t2. Count backwards\n");
                     printf("\t\t3. Show even numbers\n");
                     printf("\t\t4. Show odd numers\n");
                     printf("\t\t5. Quit\n\n");
                     printf("\t\t   Enter your choice: ");
                     fflush(stdout);
                     scanf("%i", & choice);
     
                     if(choice == 1)
                     {
                     num = EnterNum();
                     printf("counting forward from 1 to %i", num);
                     count = CountForward();
                     }
     
                     else if(choice == 2)
                     {
                     num = EnterNum();
                     printf("counting backwards from %i to 1", num);
                     count = CountBackwards();
                     }
     
                     else if(choice == 3)
                     {
                     num = EnterNum();
                     printf("Displaying odd numbers from 1 to %i", num);
                     count = CountOdd();
                     }
     
                     else if(choice == 4)
                     {
                     num = EnterNum();
                     printf("Displaying even numbers from 1 to %i", num);
                     count = CountEven();
                     }
             }
             while(choice!=5)
            return 0;
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. appeler une fonction connaissant son nom (en string)
    Par Guigui_ dans le forum Général Python
    Réponses: 1
    Dernier message: 20/07/2004, 00h46
  2. [DLL] problème pour appeler une fonction d'une DLL
    Par bigboomshakala dans le forum MFC
    Réponses: 34
    Dernier message: 19/07/2004, 11h30
  3. Appeler une fonction avec "action" ds un
    Par drinkmilk dans le forum ASP
    Réponses: 4
    Dernier message: 20/04/2004, 14h54
  4. [JSP] Appeler une fonction
    Par Patrick95 dans le forum Servlets/JSP
    Réponses: 10
    Dernier message: 23/12/2003, 13h44
  5. Appeler une fonction avec/sans parenthèses
    Par haypo dans le forum Algorithmes et structures de données
    Réponses: 8
    Dernier message: 29/12/2002, 18h48

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo