I am trying to write a program that reads in a list of employee ids, corresponding phone numbers and last names.
read in a list of employee id numbers, corresponding phone numbers, and last names (and place them into arrays)
the id number should be an integer between 2000 and 5000
the phone number should be an extension only consisting of an integer between 0100 and 9100
the employee last name should consist of no more then 20 characters
Assume that the user does not know how many employees there are and the number may be different from one day to the next. The user does know that there are a maximum number of 50 employees.
after all the id numbers, phone extensions and last names have been read into separate arrays, the program will display a user interface that will allow a user to enter a sequence of employee id numbers, and for each employee id number entered, the program will look up the id and print out the corresponding phone extension and last names. If the id is not found, then an appropriate message should be printed. This user interface should be terminated when the user enters –999 as an employee id number.
when the user finishes querying our "database" of employee id numbers, phone numbers, and last names and enters an employee id of –999, the program should print out a status report containing the total number of queries, the total number of successful queries (hits), the total number of unsuccessful queries (misses), and for each employee id number in the list, your program should print out the number of hits it had for that employee id.
This is what I have so far. I keep getting a warning saying that that search: not all control paths return a value. How can I correct this?
Code:
#include <iostream>
using namespace std;
int search(int emp_id[], int n, int key);
int main()
{
typedef char mystring[20];
mystring sarr[50];
int emp_id[50];
int phone[50];
int i=0;
int count=0;
int count1=0;
int n;
int ph;
int key;
int employeeid;
int invalid=0;
bool emp_flag=true;
bool phone_flag=true;
cout << "Enter in employee ID: " << endl;
cout << "When finished enter in -999 " << endl;
cin >> employeeid;
while(employeeid != -999)
{
while(emp_flag==true)
{
if(employeeid>=2000 && employeeid<=5000)
{
emp_id[i]=employeeid;
emp_flag=false;
}
else
{
cout << "Invalid ID " << endl;
cout << "Enter in employee ID: " << endl;
cin >> employeeid;
emp_flag=true;
}
}
cout << "Enter phone extension: " << endl;
cin >> ph;
while(phone_flag==true)
{
if(ph>=100 && ph<=9100)
{
phone[i]=ph;
phone_flag=false;
}
else
{
cout<< "You have entered an invalid phone extension. " << endl;
cout << "Enter a valid phone extension: " << endl;
cin >> ph;
phone_flag=true;
}
}
cout << "Enter employee's name: " << endl;
cin >> sarr[i];
cout << "Enter a valid employee ID: " << endl;
cin >> employeeid;
i++;
count++;
emp_flag=true;
phone_flag=true;
}
n=count;
cout << "Enter employee ID to retrieve their telephone extension: " << endl;
cin >> key;
while(key!=-999)
{
for(i=0; i<n; i++)
if(emp_id[i]==key)
count1++;
i = search(emp_id, n, key);
if(emp_id[i]==key)
{
cout << "Employee ID: "<< key << ' ' <<"Telephone Extension: "<< phone[i]<<' '<<" Employee Name: "<< sarr[i] << endl << endl;
}
if(emp_id[i]!=key && key!=-999)
{
cout << "Not Found" << endl;
invalid++;
}
cout << "Enter employee ID to retrieve their telephone extension: " << endl;
cin >> key;
}
cout << "Total Queries: " << count1 << endl;
cout << "Number of hits: " << count1 << endl;
cout << "Number of misses: " << invalid << endl;
//***** cout << "Employee Breakdown: " << employeeid << ???? << endl;
return 0;
}
int search(int emp_id[], int n, int key)
{
int i;
for(i=0; i<50; i++)
if(key==emp_id[i])
{
return(i);
}
}