Problème Code C (pointeurs)
Résolu
nairod88
Messages postés
10
Date d'inscription
Statut
Membre
Dernière intervention
-
nairod88 Messages postés 10 Date d'inscription Statut Membre Dernière intervention -
nairod88 Messages postés 10 Date d'inscription Statut Membre Dernière intervention -
Bonjour,
En fait, je débute dans la programmation en C, et je suis face à un problème que je n'arrive pas à résoudre...
Voici le code qui pose problème :
fonction main :
fonction augmentation_stat :
fonction table_stat :
Le problème vient de l'utilisation de la variable long def, en effet, je n'arrive pas à utiliser cette variable dans les 3 fonctions, le passage de la fonction main à la fonction augmentation_stat se fait "sans problème" grâce à des pointeurs, mais je ne sais pas comment faire pour passer cette variable de la fonction augmentation_stat à la fonction table_stat...
Merci si vous vous penchez sur mon problème ;)
En fait, je débute dans la programmation en C, et je suis face à un problème que je n'arrive pas à résoudre...
Voici le code qui pose problème :
fonction main :
#include <stdio.h> #include <stdlib.h> #include <time.h> int main(int argc, char *argv[]) { srand(time(NULL)); long def = 5, vit = 5; double att = 10, pv = 30, pv_max = 30; long nbre_attaque = 1; long level = 1, stat_pv = 1, stat_def = 1, stat_vit = 1, stat_att = 1; long def_e = 0, vit_e = 0; double att_e = 0, pv_e = 0, pv_max_e = 0; long nbre_attaque_e = 0; augmentation_stat(&pv, &att, &vit, &def, &stat_pv, &stat_def, &stat_vit, &stat_att, level); system("PAUSE"); return 0; }
fonction augmentation_stat :
#include <stdio.h> #include <stdlib.h> #include "augmentation_stat.h" void augmentation_stat (double *pv, double *att, long *vit, long *def, long *stat_pv, long *stat_def, long *stat_vit, long *stat_att, long level) { //1 long points_plus = 0 ; long pts_att = 0, pts_def = 0, pts_vit = 0, pts_pv = 0, total = 0; long choix = 0; int terminer = 0; int continuer = 0; printf ("\n\nVous avez actuellement : \n* %lf PV", *pv); printf ("\n* %lf d'attaque", *att); printf ("\n* %ld de defense", *def); printf ("\n* %ld de vitesse\n\n", *vit); if (level == 2 || level == 4 || level == 5 || level == 6 || level == 8 || level == 9) { //2 points_plus = 3 ; } //-2 else if (level == 3 || level == 7) {//3 points_plus = 2 ; } //-3 else if (level == 10) { //4 points_plus = 4 ; } //-4 else printf ("\n\nERREUR, augmentation_stat.c, level < 0 ou > 10\n\n"); printf ("Vous etes au niveau %ld et vous avez %ld points a distribuer.", level, points_plus); do {//5 terminer = 0 ; printf ("\nCombien de points souhaitez-vous distribuer :\n"); // ATTAQUE : printf ("\nEn attaque ? "); scanf ("%ld", &pts_att); total += pts_att ; if (total == points_plus) { //6 printf ("\nVous avez distribue tous vos points, etes vous sur de votre choix ?"); printf ("\n\n1.oui\n2.non\n"); scanf ("%ld", &choix); if (choix == 1) { //7 continuer = 1; } //-7 else {//8 pts_att = 0; total = 0; terminer = 1; } //-8 }//-6 else if (total > points_plus) { //9 printf ("\nVous n'avez pas assez de points"); pts_att = 0; total = 0; terminer = 1; } //-9 // DEFENSE : if (terminer == 0 && continuer == 0) //s'il n'y a pas eu d'erreur, continuer. { //10 printf ("\nEn defense ? "); scanf ("%ld", &pts_def); total += pts_def ; if (total == points_plus) { //11 printf ("\nVous avez distribue tous vos points, etes vous sur de votre choix ?"); printf ("\n\n1.oui\n2.non\n"); scanf ("%ld", &choix); if (choix == 1) { //12 continuer = 1; } //-12 else { //13 pts_att = 0; pts_def = 0; total = 0; terminer = 1; } //-13 }//-11 else if (total > points_plus) { //14 printf ("\nVous n'avez pas assez de points"); pts_att = 0; pts_def = 0; total = 0; terminer = 1; }//-14 }//-10 //VITESSE : if (terminer == 0 && continuer == 0) //s'il n'y a pas eu d'erreur, continuer. { //15 printf ("\nEn vitesse ? "); scanf ("%ld", &pts_vit); total += pts_vit ; if (total == points_plus) { //16 printf ("\nVous avez distribue tous vos points, etes vous sur de votre choix ?"); printf ("\n\n1.oui\n2.non\n"); scanf ("%ld", &choix); if (choix == 1) { //17 continuer = 1; } //-17 else { //18 pts_att = 0; pts_def = 0; pts_vit = 0; total = 0; terminer = 1; } //-18 }//-16 else if (total > points_plus) { //19 printf ("\nVous n'avez pas assez de points"); pts_att = 0; pts_def = 0; pts_vit = 0; total = 0; terminer = 1; } //-19 } //-15 //POINT DE VIE : if (terminer == 0 && continuer == 0) //s'il n'y a pas eu d'erreur, continuer. { //20 printf ("\nEn PV ? "); scanf ("%ld", &pts_pv); total += pts_pv ; if (total == points_plus) {//21 printf ("\nVous avez distribue tous vos points, etes vous sur de votre choix ?"); printf ("\n\n1.oui\n2.non\n"); scanf ("%ld", &choix); if (choix == 1) { //22 continuer = 1; }//-22 else { //23 pts_att = 0; pts_def = 0; pts_vit = 0; pts_pv = 0; total = 0; terminer = 1; }//-23 }//-21 else if (total > points_plus) { //24 printf ("\nVous n'avez pas assez de points"); pts_att = 0; pts_def = 0; pts_vit = 0; pts_pv =0; total = 0; terminer = 1; } //-24 }//-20 } while (terminer == 1) ; //MISE A NIVEAU DES STATS: *stat_pv += pts_pv ; *stat_att += pts_att ; *stat_def += pts_def ; *stat_vit += pts_vit ; *stat_def = 4; printf ("%ld\n", *def); table_stat (*stat_vit, *stat_att, *stat_def, *stat_pv, att, pv, def, vit); printf ("%ld\n", *def); } //-1
fonction table_stat :
#include <stdio.h> #include <stdlib.h> #include "table_stat.h" void table_stat (long stat_vit, long stat_att, long stat_def, long stat_pv, double *att, double *pv, double *pv_max, long *def, long *vit) { //PV : switch (stat_pv) { case 1: *pv = 30; break; case 2: *pv = 35; break; case 3: *pv = 40; break; case 4: *pv = 45; break; case 5: *pv = 52; break; case 6: *pv = 57; break; case 7: *pv = 62; break; case 8: *pv = 70; break; case 9: *pv = 75; break; case 10: *pv = 80; break; default: printf("ERREUR STAT pv NON COMPRISE ENTRE 1 ET 10"); break; } *pv_max = *pv ; //VITESSE : switch (stat_vit) { case 1: *vit = 1; break; case 2: *vit = 2; break; case 3: *vit = 3; break; case 4: *vit = 4; break; case 5: *vit = 5; break; case 6: *vit = 6; break; case 7: *vit = 7; break; case 8: *vit = 8; break; case 9: *vit = 9; break; case 10: *vit = 10; break; default: printf("ERREUR STAT vit NON COMPRISE ENTRE 1 ET 10"); break; } //ATTAQUE : switch (stat_att) { case 1: *att = 8; break; case 2: *att = 9; break; case 3: *att = 10; break; case 4: *att = 11; break; case 5: *att = 12; break; case 6: *att = 13; break; case 7: *att = 14; break; case 8: *att = 15; break; case 9: *att = 16; break; case 10: *att = 17; break; default: printf("ERREUR STAT att NON COMPRISE ENTRE 1 ET 10"); break; } //DEFENSE : switch (stat_def) { case 1: *def = 10; break; case 2: *def = 11; break; case 3: *def = 12; break; case 4: *def = 13; break; case 5: *def = 14; break; case 6: *def = 15; break; case 7: *def = 16; break; case 8: *def = 17; break; case 9: *def = 18; break; case 10: *def = 19; break; default: printf("ERREUR STAT def NON COMPRISE ENTRE 1 ET 10"); break; } }
Le problème vient de l'utilisation de la variable long def, en effet, je n'arrive pas à utiliser cette variable dans les 3 fonctions, le passage de la fonction main à la fonction augmentation_stat se fait "sans problème" grâce à des pointeurs, mais je ne sais pas comment faire pour passer cette variable de la fonction augmentation_stat à la fonction table_stat...
Merci si vous vous penchez sur mon problème ;)
A voir également:
- Problème Code C (pointeurs)
- Code ascii - Guide
- Code puk bloqué - Guide
- Comment déverrouiller un téléphone quand on a oublié le code - Guide
- Code activation windows 10 - Guide
- Code blocks - Télécharger - Langages