저는 현재 사용자 팀에 용을 추가 할 프로그램을 만들고 있습니다. 드래곤을 추가하려면 파일을 통해 스캔합니다. 그들을 추가하기 위해서 적어도 하나의 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;
}
코드에 잘못된 것이 너무 많습니다. 나는 당신이 교과서 또는 온라인 자습서에서 언어를 배우기 시작할 것을 제안합니다. C 교과서 : http://www.amazon.com/dp/0131103628/?tag=stackoverfl08-20. 온라인 자습서 : http://www.tutorialspoint.com/cprogramming/index.htm. –