2014-11-29 2 views
0

저는 현재 사용자 팀에 용을 추가 할 프로그램을 만들고 있습니다. 드래곤을 추가하려면 파일을 통해 스캔합니다. 그들을 추가하기 위해서 적어도 하나의 2D 배열을 사용해야 만한다는 것을 알고 있습니다. 누군가 내 프로그램을 올바르게 설정했는지 말해 줄 수 있습니까? C로 프로그래밍 할 때 구조와 함수에 대한 경험이 거의 없기 때문에 프로젝트를 제대로 설정하고 있는지 잘 모릅니다.C에서 함수 내에서 2 차원 배열에 구조를 추가하려면 어떻게해야합니까?

코드 :

//included libraries 

#include <stdio.h> 

#include <string.h> 

//constants 

#define MAX_LENGTH 40 

#define MAX_TEAM 1000 

//function prototypes 

int add_dragon(char name, char color, int num_dragons, int Dragon_array); 


//structures 

struct dragon { 
    char name[MAX_LENGTH]; 
    char color[MAX_LENGTH]; 
}; 

struct collection { 
    struct dragon team[MAX_TEAM]; 
    int num_dragons; 
}; 

//main function 

int main() { 

    //list variables in main function 

    int i, j, num_dragons, choice = 0; 


    //set up file 

    FILE * ifp = fopen("dragon.txt", "r"); 

    //check to see if there is a file 

    if(ifp == NULL){ 

     printf("Error! No file can be found!"); 

     return 1; 
    } 


    printf("Welcome! You are about to start collecting dragons!\n"); 

    //while loop for repeating the menu 

    while(num_dragons < MAX_TEAM){ 

     printf("What would you like to do to your team of dragons?\n\n"); 

     printf("\t 1- Add to team\n"); 

     printf("\t 2- Remove from team\n"); 

     printf("\t 3- Search for dragon on team\n"); 

     printf("\t 4- List dragons\n"); 

     scanf("%d", & choice);//enter choice 

     //conditions to prompt for a valid choice 

     while(choice > 4) 

     scanf("%d", &choice); 

     while(choice < 1) 

     scanf("%d", &choice); 

     //enter dragon name and color 
     fscanf(ifp, "%s", &num_dragons); 


    } 


    return 0; 
} 

//Pre_condition: choivr us set to zero 

//Post_condition allows user to select a choice 

//Preconditions: menu is set up and variables are declared 

//postConditions: will allow user to add a dragon to their team 

int add_dragon(char name, char color, int num_dragons, int Dragon_array){ 

    //variables 

    int i, j; 

    //if(num_dragons < MAX_TEAM)//check if numer of dragons doesn't exceed max size of the team{ 

    Dragon_array [MAX_TEAM] [MAX_TEAM]; 

    for(i = 0; i < MAX_TEAM; i ++) 
     for(j = 0; j < MAX_TEAM; j ++){ 

     num_dragons ++; 
     } 

    printf("%s the %s dragon has been added to the team\n", name, color);// confirm that the dragon was added to team 

    return num_dragons; 
} 
+0

코드에 잘못된 것이 너무 많습니다. 나는 당신이 교과서 또는 온라인 자습서에서 언어를 배우기 시작할 것을 제안합니다. C 교과서 : http://www.amazon.com/dp/0131103628/?tag=stackoverfl08-20. 온라인 자습서 : http://www.tutorialspoint.com/cprogramming/index.htm. –

답변

0
// note: 
// 1) there is a lot of code missing from main 
// 2) most functions to handle the menu choice value are missing 
// 3) the function: add_dragon() is never called 

//included header files 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 


//constants 
#define MAX_LENGTH (40) 
#define MAX_TEAM (1000) 

//function prototypes 
int add_dragon(char* pName, char* pColor); 


//structures 
struct dragon 
{ 
    char name[MAX_LENGTH]; 
    char color[MAX_LENGTH]; 
}; 

struct collection 
{ 
    struct dragon team[MAX_TEAM]; 
    int num_dragons; 
}; 

// global instance of struct collection. 
// you 'may' want to place this instance in main 
// then pass a pointer to myCollection to each of the subroutines 
struct collection myCollection; 

//main function 
int main() 
{ 

    //list variables in main function 
    int choice = 0; 

    // initialize to no dragons 
    myCollection.num_dragons = 0; 
    memset(myCollection.team, 0x00, MAX_TEAM*sizeof(structDragon)); 

    //set up file 
    FILE * ifp = fopen("dragon.txt", "r"); 
    if(ifp == NULL) 
    { // then, fopen failed 
     perror("fopen failed for reading dragon.txt"); 
     return 1; 
    } 

    // implied else, fopen for read successful 

    printf("Welcome! You are about to start collecting dragons!\n"); 

    //while loop for repeating the menu 
    while(myCollection.num_dragons < MAX_TEAM) 
    { 
     printf("What would you like to do to your team of dragons?\n\n"); 
     printf("\t 1- Add to team\n"); 
     printf("\t 2- Remove from team\n"); 
     printf("\t 3- Search for dragon on team\n"); 
     printf("\t 4- List dragons\n"); 


     do 
     { 
      if(1 != scanf(" %d", & choice))//enter choice // note leading ' ' in format string 
      { 
       perror("scanf failed while getting choice"); 
       exit(EXIT_FAILURE); 
      } 

      // implied else, scanf successful 
     } while((4<choice) &&(1>choice)); 

     //enter dragon name and color 
     //fscanf(ifp, " %s", &num_dragons); 

     // !!! code seems to be missing here !!! 
    } // end while 

    return 0; 
} // end function: main 


//Pre_condition: choivr us set to zero 
//Post_condition allows user to select a choice 
//Preconditions: menu is set up and variables are declared 
//postConditions: will allow user to add a dragon to their team 


int add_dragon(char* pName, char* pColor) 
{ 

    //variables 

    if(MAX_TEAM > myCollection.num_dragons) 
    { // then, room for another dragon 
     strcpy(myCollection.team[myCollection.num_dragons].name, pName); 
     strcpy(myCollection.team[myCollection.num_dragons].color, pColor); 
     myCollection.num_dragons++; 
    } // end if 

    printf("%s the %s dragon has been added to the team\n", pName, pColor);// confirm that the dragon was added to team 

    return myCollection.num_dragons; 
} // end function: add_dragon 
0

첫 번째 문제는 여기에 있습니다 : while(num_dragons < MAX_TEAM). num_dragons은 선언되었지만 0으로 초기화되지 않으므로 1234213과 같은 가비지 값을 포함 할 수 있으므로 항상 MAX_TEAM보다 작습니다. 그런 다음 함수를 선언하면 인수는 char s가되지만 문자열을 전달하려고합니다. 따라서 char *이 필요합니다. 나는 또한 당신이 Dragon_array 인 지 궁금하다. 왜냐하면 당신이 그것을 매개 변수로 전달하기 때문에, 그러나 함수의 몸 안에 그것을 선언하기 때문이다.

원하는 부분을 자세히 설명하십시오.

관련 문제