如何将值放入数组结构?

时间:2017-04-08 03:32:57

标签: c pointers struct

我正在尝试在C中建立帐户系统。 我正在尝试这种方式来输入结构数组。

struct account {                  // Account Structure
int id;
int money;
char *name[30];
};
account * accountarray[50];
int number;


void MakeAccount() {
int id;
int input_money;
char name[50];

printf("--Make Account--\n");
printf("Input ID : ");
scanf("%d", &id);
printf("Insert Money : ");
scanf("%d", &input_money);
printf("Your Name? : ");
scanf("%s", name);

accountarray[number++] = NULL;                // I think there's a problem in this side
accountarray[number++]->id = id;
accountarray[number++]->money = input_money;
*accountarray[number++]->name = name;
}

当我输入值时它会停止...我认为下面的4个代码有问题.. 有没有一种方法可以让它变得更好?

3 个答案:

答案 0 :(得分:1)

这是您的代码版本。

  1. 它干净地编译
  2. 由于没有main()功能
  3. ,因此无法执行
  4. 正确执行错误检查
  5. 它正确地将错误消息输出到stderr
  6. 它正确地声明了'帐户'数据
  7. 它适当地使用空行来分组代码块和活动块
  8. 它正确使用字符串函数来复制name数组
  9. 发生错误时退出程序
  10. 为了便于阅读,它在程序缩进中是一致的
  11. 它给了魔法'编号有意义的名称(并在整个代码中使用那些有意义的名称)
  12. 它避免了任何溢出输入缓冲区的可能性
  13. 它包含必要的头文件和注释,为什么要包含每个头文件
  14. 它不消耗最终的换行符'但是,用户输入的序列,scanf()id的调用将消耗stdin中剩余的字节作为"%d"输入/转换说明符在输入下一个帐户时将使用前导white space
  15. 现在是代码

    #include <stdio.h>    // printf(), scanf(), perror()
    #include <stdlib.h>   // exit(), EXIT_FAILURE
    #include <string.h>   // strcpy()
    
    // eliminate 'magic' numbers
    #define MAX_STR_LEN 30
    #define MAX_ACCTS   50
    
    // define the struct
    struct account
    {                  // Account Structure
        int id;
        int money;
        char name[ MAX_STR_LEN ];
    };
    
    // prototypes
    void MakeAccount( void );
    
    // === file global data ===
    // declare MAX_ACCTS instances of the struct
    struct account accountarray[ MAX_ACCTS ];
    // declare a counter
    int number = 0;
    
    
    void MakeAccount()
    {
        int id;
        int input_money;
        char name[ MAX_STR_LEN ];
    
        int scanfStatus;
    
        printf("--Make Account--\n");
    
        printf("Input ID : ");
        scanfStatus = scanf("%d", &id);
    
        if( 1 != scanfStatus )
        { // then scanf failed
            // output error message, including the OS reason for the error to 'stderr'
            perror( "scanf for account id failed" );
            exit( EXIT_FAILURE );
        }
    
        //implied else, scanf successful
    
        printf("Insert Money : ");
        scanfStatus = scanf("%d", &input_money);
    
        if( 1 != scanfStatus )
        { // then scanf failed
            // output error message, including the OS reason for the error to 'stderr'
            perror( "scanf for account money failed" );
            exit( EXIT_FAILURE );
        }
    
        printf("Your Name? : ");
        scanfStatus = scanf("%29s", name); // note MAX CHARACTERS 1 less than length of input buffer
    
        if( 1 != scanfStatus )
        { // then scanf failed
            // output error message, including the OS reason for the error to 'stderr'
            perror( "scanf for account name failed" );
            exit( EXIT_FAILURE );
        }
    
        accountarray[number].id = id;
        accountarray[number].money = input_money;
        strcpy( accountarray[number].name, name);
    } // end function: MakeAccount
    

答案 1 :(得分:0)

我认为您可能不需要这里的指针,因为您假设名称的最大大小为30和50个帐户。所以这是我认为你可以进行的修改:

 struct account {                  // Account Structure
   int id;
   int money;
   char name[30];  // an array of characters
 };
 struct account accountarray[50]; // Note: its type is "struct account"
 int number;

 void MakeAccount() {
  int id;
  int input_money;
  char name[30];

  printf("--Make Account--\n");
  printf("Input ID : ");
  scanf("%d", &id);
  printf("Insert Money : ");
  scanf("%d", &input_money);
  printf("Your Name? : ");
  scanf("%s", name);

  accountarray[number++].id = id;
  accountarray[number++].money = input_money;
  memcpy(accountarray[number++].name, name, strlen(name)); // Note I used memcpy to copy one array of characters into another
}

答案 2 :(得分:0)

我认为你试图制作比以前更复杂的东西,我这样做了

#include <stdio.h>

struct account {                  // Account Structure
    int id;
    int money;
    char name[30];
};

int number;


int main() {
int id,x,input_money;
char name[50];
struct account accountarray[50];

printf("How many Number of accounts do you want to enter? ");
scanf("%d",&x);
printf("--Make Account--\n");
for (int i = 0; i < x; i++)
{

    printf("Input ID : ");
    scanf("%d", &accountarray[i].id);
    printf("Insert Money : ");
    scanf("%d", &accountarray[i].money);
    printf("Your Name? : ");
    scanf("%s", accountarray[i].name);
    printf("\n");
}
printf("****RECORD INSERTED****\n");
for (int i = 0; i < x; i++)
{
    printf("\nID: %d",accountarray[i].id);
    printf("\nNAME: %s",accountarray[i].name);
    printf("\nMONEY: %d",accountarray[i].money);

}

}

您可以查看https://github.com/ashim888/csit-c以获取更多信息和代码示例