-4

So I am very new to coding (literally started learning c a few days ago) and I decided to just play around and see if I can apply what I learned so far. I created an "employee search" program that prompts the user for a name and it will check to see if the employee exists. I ran into an issue within the loop; if I were to type "Chris" into the terminal and click enter, it'll say something like: "Employee not found." "Chris found." "Employee not found."How do I go about making the program confirm the name is within the "database" without it repeating the "error" message. Sorry for the newbie question. Again, I'm very new to this.

#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");

        }
    }
}
alk
  • 69,737
  • 10
  • 105
  • 255
ApacheR12
  • 1
  • 2

3 Answers3

2

Avoid printing inside the loop. Instead use a flag to save the status. Like:

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");
}
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
1
#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");     
}
Zahid Khan
  • 2,130
  • 2
  • 18
  • 31
-2
 // 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"}