我正在尝试在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个代码有问题.. 有没有一种方法可以让它变得更好?
答案 0 :(得分:1)
这是您的代码版本。
main()
功能stderr
name
数组scanf()
对id
的调用将消耗stdin
中剩余的字节作为"%d"输入/转换说明符在输入下一个帐户时将使用前导white space
现在是代码
#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以获取更多信息和代码示例