c - 如何根据用户的输入/选择打印和编辑数组列表中的特定元素?
问题描述
我在我的代码中创建了一个原型,允许用户添加、编辑和/或打印他们的输入。我已经完成了选项 1(添加员工)、4(打印所有员工)和 5(退出)。我坚持使用选项 2(编辑员工)和 3(打印员工)任何建议或帮助都会令人难以置信(新程序员)!
void loadMenu()
{
int i = 0;
do
{
printf("\nMAIN MENU\n");
printf("1.Add Employee\n");
printf("2.Edit Employee\n");
printf("3.Print Employee\n");
printf("4.Print ALL employees\n");
printf("5.Exit\n");
scanf_s("%d", &choice);
switch (choice)
{
case 1: printf("**Enter -1 for MAIN MENU** \n\n");
NameInput();
break;
case 2: printf("Choose employee: \n");
for (int i = 0; i < Times; i++)
{
printf("%d. %s \n", i + 1, emps[i].emps.name);
}
scanf_s("%d", &choice);
case 3: printf("Choose employee: \n\n");
for (int i = 0; i < Times; i++)
{
printf("%d. %s \n", i + 1, emps[i].emps.name);
}
scanf_s("%d", &choice);
case 4: PayInput();
break;
case 5: printf("Quitting program!");
exit(0);
break;
default: printf("Invalid choice tray again \n\n");
break;
}
} while (choice != 5);
}
解决方案
我可以给你一个提示。您需要将您的用户添加到一个数组中,以便您可以在 for 循环中比较用户的选择。我将在伪代码上写这个给你一个想法:
// Users array so you can have ["John, Paul, Ringo"]
const char *users_list[100];
users_list[0] = "John";
users_list[1] = "Paul";
users_list[2] = "Ringo";
// Input's user
selectedEmployee = "your user input"
// The total elements inside of your array.
int all_users = sizeof(users_list) / sizeof(users_list[0]);
for (int i = 0; i < all_users; i++)
{
if(employee[i] == selectedEmployee){
// If the users wrote "paul" is going to print paul user".
print selectedEmployee
}
}
这只是伪代码,希望这可以帮助您解决问题。记住,无论看起来多么艰难,都不要放弃。不要沮丧。一开始编程很复杂。
推荐阅读
- r - R:For循环从多个数据帧中捕获数据
- java - 如何从 corda/corda-zulu-5.0-snapshot 图像创建自定义图像?
- javascript - ReactGridLayout:ReactGridLayout.children[0].x 必须是数字
- javascript - 如何在 Iphone 设备中让我的输入字段在 1 秒后聚焦?
- rest - 使用带有证书的 https Rest API
- r - 收集功能后数据尽可能整齐
- javascript - 表单代码上的颜色更改文本验证问题
- java - 无法使用 poi 数据驱动将多个字符串写入 excel,但我可以通过提及行号和列号来写入每个单元格
- spring-aop - BeanNameAutoProxyCreator 抛出 NullPointerException
- amazon-s3 - 如何在没有任何手动活动的情况下将数据从 Google 云存储传输到 S3?