首页 > 解决方案 > 使用 put in for 循环以表格格式打印名称,但仅正确打印姓氏

问题描述

创建一个结构来指定银行客户的数据。要存储的数据是:账号、姓名、账户余额。假设银行最多有 200 个客户。

struct CustomerData {
   int   acNum;
   float balance;
   char name[];
} n[2];

void main() {
    for(int i = 0;i<2; i++) {
        printf("give Ac. no. of %d customer\n",i+1);
        scanf("%d",&n[i].acNum);
        printf("balance of customer %d\n",i+1);
        scanf("%f",&n[i].balance);
        printf("Name of customer %d\n",i+1);
        fflush(stdin);
        gets(n[i].name);
    }

    printf(" Name      Acc. no    Balance \n");

    for(int i =0;i<2;i++) {
        printf("%c      %d           %f\n",puts(n[i].name),n[i].acNum,n[i].balance);
    }
}

输出:

give Ac. no. of 1 customer
50054
balance of customer 1
11316
Name of customer 1
sahil
give Ac. no. of 2 customer
15655
balance of customer 2
100
Name of customer 2
Rishav
 Name      Acc. no    Balance
'=
       50054           11316.000000
Rishav
       15655           100.000000

Process returned 34 (0x22)   execution time : 25.120 s
Press any key to continue.

标签: arrayscstringfor-loopstruct

解决方案


正如评论中提到的,您的代码中有几个问题:

  1. 永远不要fflush(stdin)。它会导致未定义的行为。
  2. 切勿用于scanf()读取输入。改为使用fgets()
  3. 您应该正确缩进您的代码,以便更容易阅读。

这是您的代码的更好版本:

#include <stdio.h>
#include <string.h> // for strcspn()

struct CustomerData {
    int acNum;
    float balance;
    char name[255]; // Give it a size
};

我们需要定义一个从输入中读取一行的函数(记住不要使用scanf()):

char *readstr(char *str, int size)
{
    if (!fgets(str, size, stdin)) {
        fprintf(stderr, "Input error\n");
        return NULL;
    }
    
    str[strcspn(str, "\n")] = '\0'; // fgets() reads the ending '\n' so null-terminate the string
    return str;
}

你可以对ints 和floats 做同样的事情:

int readint(int *i)
{
    char buffer[255];
    if (!fgets(buffer, 255, stdin)) {
        fprintf(stderr, "Input error\n");
        return 0; // failed
    }

    buffer[strcspn(buffer, "\n")] = '\0';
    
    if (sscanf(buffer, "%d", i) != 1)
        return 0; // failed
    
    return 1; // success
}

int readfloat(float *f)
{
    char buffer[255];
    if (!fgets(buffer, 255, stdin)) {
        fprintf(stderr, "Input error\n");
        return 0; // failed
    }

    buffer[strcspn(buffer, "\n")] = '\0';
        
    if (sscanf(buffer, "%f", f) != 1)
        return 0; // failed
        
    return 1; // success
}

现在,您可以在您的main():

int main() // main() should always return an int
{
    const int num_customers = 2; // In case you wanted to change it later
    struct CustomerData n[num_customers];
    
    for(int i = 0; i < num_customers; i++) {
        printf("give Ac. no. of %d customer: ", i+1);
        readint(&n[i].acNum);
        
        printf("balance of customer %d: ", i+1);
        readfloat(&n[i].balance);
        
        printf("Name of customer %d: ", i+1);
        readstr(n[i].name, sizeof(n[i].name));
    }
    
    printf("\nName\tAcc. no\tBalance\n");
    
    for(int i = 0; i < num_customers; i++)
        printf("%s\t%d\t%f\n", n[i].name, n[i].acNum, n[i].balance);
}

示例输出:

give Ac. no. of 1 customer: 1
balance of customer 1: 12
Name of customer 1: john
give Ac. no. of 2 customer: 2
balance of customer 2: 65.5
Name of customer 2: wick

Name    Acc. no Balance
john    1       12.000000
wick    2       65.500000

推荐阅读