Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is just the portion of my program. This program displays Not Found even if the id number is found.

C#
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
C++
Enter ID to search: 111

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

I just want to remove the "Not found"
Posted

1 solution

Remove the else block inside the for loop, and add the "Not found" message inside an if block after the loop has completed:
C++
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";
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900