首页 > 解决方案 > 如何根据用户的输入/选择打印和编辑数组列表中的特定元素?

问题描述

我在我的代码中创建了一个原型,允许用户添加、编辑和/或打印他们的输入。我已经完成了选项 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);
}

标签: carraysloopsarraylist

解决方案


我可以给你一个提示。您需要将您的用户添加到一个数组中,以便您可以在 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
          }
     }

这只是伪代码,希望这可以帮助您解决问题。记住,无论看起来多么艰难,都不要放弃。不要沮丧。一开始编程很复杂。


推荐阅读