arrays - 使用 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.
解决方案
正如评论中提到的,您的代码中有几个问题:
- 永远不要
fflush(stdin)
。它会导致未定义的行为。 - 切勿用于
scanf()
读取输入。改为使用fgets()
。 - 您应该正确缩进您的代码,以便更容易阅读。
这是您的代码的更好版本:
#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;
}
你可以对int
s 和float
s 做同样的事情:
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
推荐阅读
- discord - 我不断收到此错误 [CLIENT_MISSING_INTENTS]:必须提供有效的意图
- arrays - 使用嵌套填充创建二维字符串数组
- azure - 部署时如何发送包含在 Azure DevOps 管道上完成的任务的电子邮件?
- java - 下载一个 Maven 依赖项时出现问题
- android - 使用 Jetpack compose 按钮打开相机
- vue.js - 如何根据从服务器端接收到的数据来改变滑块的图像
- typo3 - Typo3 tx_news eventnews:location_simple 和 Organizer_simple
- python - 如何查看终端的pycharm中使用了哪个venv?
- docker - 在 Docker 中使用文本检查点文件 - 如何修改此文件?
- winforms - 双击标题栏以最大化窗口时触发 Winform MouseMove 事件(左键按下)