即使已找到,我如何删除未找到的东西? [英] How do I remove the not found even if it is found already?

查看:123
本文介绍了即使已找到,我如何删除未找到的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这只是我程序的一部分.即使找到ID号,该程序也会显示Not Found.

This is just the portion of my program. This program displays Not Found even if the id number is found.

void search(void)
{
    char ID[10];
    int i, found;

    cout<<"Enter ID to search: ";
    gets(ID);
    found=0;
    for(i=0;i<top;i++)
    {
        if(!strcmp(ID, info[i].ID))
        {
            display(i);
            found=1;
            cout<<"\n";
        }
        else
        {
            cout<<"Not found\n";
        }
    }
}


这是错误的输出


Here''s the incorrect output

Enter ID to search: 111

Not found
Name: jude
ID: 111
Semester: 1
Major: IT


我只想删除未找到"


I just want to remove the "Not found"

推荐答案

删除for循环内的else块,并在循环完成后的if块:
Remove the else block inside the for loop, and add the "Not found" message inside an if block after the loop has completed:
for (i = 0; i < top; i++)
{
    if (!strcmp(ID, info[i].ID))
    {
        display(i);
        found = 1;
        cout << "\n";
    }
}
if (found == 0)
{
    cout << "Not found\n";
}


这篇关于即使已找到,我如何删除未找到的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆