【问题标题】:How can I use a structure to add to a 2d array within a function in C?如何使用结构添加到 C 函数中的二维数组?
【发布时间】:2014-11-29 01:09:27
【问题描述】:

我目前正在开发一个将龙添加到用户团队的程序。要添加通过文件扫描的龙。为了添加它们,我知道我必须使用至少一个 2D 数组。有人可以告诉我我的程序设置是否正确吗?当谈到用 C 编程时,我对结构和函数的经验很少,因此我不知道我是否正确设置了我的项目。

代码:

//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 file function struct structure


【解决方案1】:
// 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

【讨论】:

    【解决方案2】:

    您的第一个问题在这里:while(num_dragons &lt; MAX_TEAM)num_dragons 已声明,但未初始化为 0,因此它可以包含像 1234213 这样的垃圾值,因此它总是小于 MAX_TEAM。然后,当你声明你的函数时,你的参数是chars,但我猜你想给它传递字符串。所以你需要一个char *。我也很好奇谁是你的Dragon_array,因为你将它作为参数传递,然后你在函数体内声明它。

    试着详细解释你想做什么

    【讨论】:

      猜你喜欢
      • 2018-02-22
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-28
      • 1970-01-01
      • 2015-02-16
      相关资源
      最近更新 更多