Problème exécution programme C, "a cessé de fonctionner".

Fermé
Nox_MD - 15 nov. 2012 à 20:50
Hxyp Messages postés 401 Date d'inscription vendredi 28 janvier 2011 Statut Membre Dernière intervention 27 avril 2014 - 16 nov. 2012 à 02:43
Bonjour,

voilà, je suis un peu débutant en c et je dois créer des codes qui, une fois terminés, guideront l'évolution de coordonnées d'un mobile (mais ils appellent ça un "robot traceur" sur une grille (et à chaque nouvelle position des robots, j'écris une lettre sur la grille à ces coordonnées). Désolé, c'est vraiment pas clair mais je vais vous montrer le code.

J'ai un robot.h :

#include <stdio.h>
#ifndef ROBOT_H
#define ROBOT_H

typedef struct robot {
  int posX ;
  int posY ;
  char nom[256] ;
  FILE *instructions ;
} Robot ;

void robot_initialiser (Robot *bot, int x, int y, char *name, char *filename) ;

void robot_detruire (Robot *bot) ;

int robot_get_positionX (Robot *bot) ;

int robot_get_positionY (Robot *bot) ;

char robot_avancer (Robot *bot) ;

void robot_afficher (Robot *bot) ;

#endif


Un robot.c :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "robot.h"
#include "dimensions.h"

/*=====================================================*/

void robot_initialiser (Robot *bot, int x, int y, char *name, char *filename)
{

  (*bot).posX = x;
  (*bot).posY = y;
  strcpy ((*bot).nom, name);
  (*bot).instructions = fopen (filename, "r");
  if ((*bot).instructions == NULL) {
    perror ("Failed to open config.txt in read mode");
	exit (-1);
  }
}
	
/*====================================================*/

void robot_detruire (Robot *bot)
{

  fclose((*bot).instructions);
 
} 
/*====================================================*/

int robot_get_positionX (Robot *bot)
{
  return (*bot).posX;

}
/*====================================================*/

int robot_get_positionY (Robot *bot)
{
  return (*bot).posY;
 
} 
/*====================================================*/

char robot_avancer (Robot *bot)
{
  char res = 0;
  
  if (feof((*bot).instructions) == 0) {
  
    fscanf ((*bot).instructions,"%c",&res);
	
	switch (res) {
	
	case 'G' :
	  if ((*bot).posX > 0) (*bot).posX--;
	  break;
	
	case 'D' :
	  if ((*bot).posX < WIDTH-1) (*bot).posX++;
	  break;
	  
	case 'H' :
      if ((*bot).posY > 0) (*bot).posY--;
	  
	  break;
	  
	case 'B' :
	  if ((*bot).posY < HEIGHT-1) (*bot).posY++;
	
	  break;
	}
  }
  
  return res;
 
} 
/*===============================================*/

void robot_afficher (Robot *bot)
{
  printf ("[%s] (%d;%d)\n",(*bot).nom, (*bot).posX, (*bot).posY);
    
}	


Et un fichier "exo5.c" avec mon algo:

#include <stdio.h>
#include <stdlib.h>
#include "robot.h"
#include "dimensions.h"

  void initialisation (char grille [WIDTH] [HEIGHT]) {
    
	int i, j;
	
	for (i=0; i<WIDTH; i++) {
	  
	  for (j=0; j<HEIGHT; j++)
	    grille [i][j] = '-';
		
	}
  }

void affichage (char grille [WIDTH] [HEIGHT]) {

  int i, j;
  
  for (j=0; j<HEIGHT; j++) {
  
    for (i=0; i<WIDTH; i++)
	  printf ("%c ", grille [i] [j]);
	  
	printf ("\n");
  }
}

	
int main () {

	Robot bot[6];
	char stop[6];
	int nb;
	char grille [WIDTH] [HEIGHT];
	
	initialisation (grille);
	robot_initialiser(&bot[1],4,2,"Ewall","instructionsE.txt");
	robot_initialiser(&bot[2],9,2,"Swell","instructionsS.txt");
	robot_initialiser(&bot[3],10,2,"Tiger","instructionsT.txt");
	robot_initialiser(&bot[4],17,2,"Ipade","instructionsI.txt");
	robot_initialiser(&bot[5],24,7,"Astra","instructionsA1.txt");
	robot_initialiser(&bot[6],27,7,"Astro","instructionsA2.txt");
  
	do {
		for (nb=1;nb<7;nb++) {
			stop[nb] = robot_avancer (&bot[nb]);
			if (stop[nb] != 0) {
			
			grille [bot[nb].posX] [bot[nb].posY] = bot[nb].nom [0];	
				
			}
		}
		system("cls");
		affichage (grille);
		printf ("Appuyez sur la touche ENTREE pour continuer\r\n");
		scanf ("%*c");
	}while ((stop[1] != 0) && (stop[2] != 0) && (stop[3] != 0) && (stop[4] != 0) && (stop[5] != 0) && (stop[6] !=0));

	for (nb=1;nb<7;nb++) {
		robot_detruire (&bot[nb]);
	}
  
	return 0;
}


Et mon problème, c'est qu'après la compilation et la création de l'exécutable, l'exécution du fichier entraîne un "...a cessé de fonctionner" et appuyer sur annuler me ramène sur la console. Je vois pas d'où ça peut venir, j'ai pensé à une sortie de programme à la lecture de mes fichiers txt dans robot.c mais... je vois pas. (En plus dsl mais j'ai pas encore mis les commentaires dans le code pour que la compréhension aille plus vite donc c'est pas top ^^').
Merci d'avance pour votre aide. =)


1 réponse

Hxyp Messages postés 401 Date d'inscription vendredi 28 janvier 2011 Statut Membre Dernière intervention 27 avril 2014 54
16 nov. 2012 à 02:43
Bonjour, il nous manque le dimensions.h pour le compiler.
0