Thread: help with array & function program

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    12

    help with array & function program

    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);
    		}
    
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I was about two sentenses into your problem when I realized that either this is some sort of homework or you secretly don't too much C/C++ and got the software engineer job anyway Even though I think you should do your own homework I'll give some direction. If it is possible you should be reading and writing the data structures in a binary file. If that isn't possible you can always use hash tables to keep track of data. You can look up both things on the boards.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    12
    Yes it is a homework assignment. One that I have spent many hours on trying to figure out. I have gotten this far and was just asking for some direction from those of you that are more advanced programmers than myself. I'm stuck and just wanted a little help figuring it out.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The warning is that your search function does not return a value if you fail to find an item. It should return the item if you find it and an invalid value if you do not so that you can test for it in the calling function:
    Code:
    int search(int emp_id[], int n, int key)
    {
      int i;
      for(i=0; i<50; i++) {
        if(key==emp_id[i])
          return(i);
      }
      return(-1); // Or any other invalid ID
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    12
    Thanks Prelude for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM