c - 我的 C 代码没有捕捉到“最终客户”结束
问题描述
我正在用 C 编写代码以从用户那里读取客户数据并在屏幕上打印客户数据,直到读取客户名称“最终客户”(即 first_name last_name)。
我认为包括 if 循环和 strcmp() 将捕获输入“最终客户”并防止打印客户记录。但是,当我运行它时,它并没有结束。这是我的主要功能:
#include <stdio.h>
#include <string.h>
struct account {
struct
{
char lastName[10];
char firstName[10];
} names;
int accountNum;
double balance;
};
void nextCustomer(struct account *acct);
void printCustomer(struct account acct);
int main()
{
struct account record;
int flag = 0;
do {
nextCustomer(&record);
if ((strcmp(record.names.firstName, "End") == 0) && //I thought this would be able to catch the End Customer//
(strcmp(record.names.lastName, "Customer") == 0))
flag = 1;
if (flag != 1)
printCustomer(record);
} while (flag != 1);
}
这些是我分别读取客户数据和打印客户数据的两个功能:
void nextCustomer(struct account *acct)
{
struct account {
struct
{
char lastName[10];
char firstName[10];
} names;
int accountNum;
double balance;
} record;
printf("Enter names (firstName lastName): ");
scanf("%s %s", (acct->names).firstName, (acct->names).lastName);
printf("Enter account number: ");
scanf("\n");
scanf("%d", &acct->accountNum);
printf("Enter balance: ");
scanf("%lf", &acct->balance);
}
void printCustomer(struct account acct)
{
printf("Customer record: \n %s %s %d %.2lf\n", acct.names.firstName, acct.names.lastName, acct.accountNum, acct.balance);
}
谢谢!
解决方案
您当前的代码将nextCustomer
完全执行,直到完成对所有输入(姓名、帐号和余额)的扫描。只有在那之后,代码才会检查名字和姓氏。您需要在扫描后立即检查名称,因此如果您获得“最终客户”,您可以提前退出。
检查此代码以获取解决方案:https ://onlinegdb.com/HJr_YS9Yw
推荐阅读
- python - 分组而不为 re.findall() 创建新目标
- python-3.x - 找到列表中每个项目的平方 .o/p 应该是一个列表 .write 程序使用 lambda ,使用理解,使用循环
- java - 拍照到外部存储
- google-cloud-datastore - 除了按 id 获取之外,Objectify 过滤器不起作用
- sql - SQL: Nested Select vs Double JOIN by Performance
- java - 如何动态渲染数据并实时更新变化
- go - Go Swagger 能够为异步响应式代码编写响应
- excel - 如果两个垂直测试和excel中的两个表格的条件
- sql-server - 如何从 xml 节点中提取数据作为列中的表?
- ms-access - Access 2019:EMF 文件支持刚刚消失?