【问题标题】:How do I run this without the loop printing the "error" message如何在没有循环打印“错误”消息的情况下运行它
【发布时间】:2017-11-25 07:24:19
【问题描述】:

所以我对编码非常陌生(实际上是几天前开始学习 c),我决定只是玩玩,看看我是否可以应用我目前学到的东西。我创建了一个“员工搜索”程序,它会提示用户输入姓名,它会检查员工是否存在。我在循环中遇到了一个问题;如果我在终端中输入“Chris”并点击回车,它会显示类似:“Employee not found”。 “克里斯找到了。” “找不到员工。”我如何让程序确认名称在“数据库”中而不重复“错误”消息。对不起新手问题。再说一次,我对此很陌生。

#include <cs50.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
    // declare array
    string employee[] = {"Damien", "Chris", "Emma"};

    // print intro message and prompt user for name
    printf("Welcome to employee search\n");
    printf("Please input an employee name: ");
    string name = get_string();

    // here is where I run into the issue where it'll repeat "employee not found"
    for(int i = 0; i < 3; i++)
    {
        if(strcmp(name, employee[i])==0)
        {
            printf("%s found\n", name);
        }
        else
        {
            printf("Employee not found\n");

        }
    }
}

【问题讨论】:

  • found." 行在做什么?这是C,即string是什么?
  • 也许在 cs50.stackexchange.com 上问这个
  • 通过在循环中设置标志并随后报告。而不是每次迭代都报告。
  • ... 甚至更好,您也许可以在 cs50.stackexchange.com 上轻松找到 answer 而无需询问
  • 这完全取决于未知的get_string(); 函数是否包含行终止 字符。这也是Is it a good idea to typedef pointers?是否的一个很好的例子。 (显然发生在cs50.h

标签: c loops break cs50


【解决方案1】:

避免在循环内打印。而是使用标志来保存状态。喜欢:

int flag = 0;  // Initialize flag to 0 (i.e. assume the name isn't found)

for(int i = 0; i < 3; i++)
{
    if(strcmp(name, employee[i])==0)
    {
        flag = 1;  // Set flag to 1 to remember that we had a match

        break;     // Stop the loop using break. We don't need to check the rest
                   // as we have found a hit
    }
}

if (flag)
{
    printf("%s found\n", name);
}
else
{
    printf("Employee not found\n");
}

【讨论】:

    【解决方案2】:
    #include <cs50.h>
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       string employee[] = {"Damien", "Chris", "Emma"};
       int i = 0;
    
    // print intro message and prompt user for name
     printf("Welcome to employee search\n");
     printf("Please input an employee name: ");
     string name = get_string();       //Declaration and initialisation
    
    for(i = 0; i < 3; i++)
    {
        if(strcmp(name, employee[i])==0)
        {
            printf("%s found\n", name);
            break;   // if any of the employee is found it exit the loop immediately with the value of i<3 
        }
    
    }
    if (i ==3 ) //means the loop has reached end without finding any of employee.
            printf("Employee not found\n");     
    }
    

    【讨论】:

    • 或许正确格式化代码并添加说明
    • 补充说明,希望对你有帮助
    • i 超出了for 循环的范围。
    • 在代码块中也可以正常工作。还是做了修改。
    【解决方案3】:
     // yes dear it a bit easy  
     // first get the input in any variable 
      string name="";
     // And  then inside a loop chek every index of array to the string which you get from the user  
    
    cout<<" Enter name ";
     cin>>name;
    
     for(int i=0; i<=c.length;i++)
    {  if(c[i]==name) 
        {  
         cout<<"found";
       }
    else{  cout<<"not found ";
       }
    
    }
    
    // c just like c={"first name","secound_name","blablala"}
    

    【讨论】:

    • 或许正确格式化代码并添加说明
    • 问题标记为 c 而不是 c++
    • 这段代码没有提供问题的答案,如果它可以工作,它也面临同样的问题。
    猜你喜欢
    • 2021-12-22
    • 2023-04-05
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    相关资源
    最近更新 更多