I need clues for making a programe
suppose there is sample data set in file sp13Input.txt.


A 555-1234
B 555-1111
C 555-2222
D 555-3333
E 555-4444
F 555-5555
G 555-4321
z 000-0000

we have to make linear search,by giving the number and it will display the name of the person whose telephone number is entered.need clues,ideas

Start by writing some code that inputs the number from the user.

i am supposed to write a program which read the following data from txt file, search number by entering name and search name by enter number.the first part is workin like searching number by namber,but the second part is not working.can u give clues
BB 555-1111
AA 555-1234
CC 555-2222
DD 555-3333
GG 555-4321
EE 555-4444
FF 555-5555
zz 000-0000

so far my progress is as follow:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <cstring>
using std::cin;
using std::cout;
using std::endl;
using std::ostream;
using std::istream;
using std::ofstream;
using std::fstream;
using std::ios;
using std::ifstream;
using std::setiosflags;
using std::resetiosflags;
using std::setw;
using std::setprecision;
int  read  (const char filename[], char names[][20], int numbers[]);
void write (const char filename[], const char description[],
               const char names[][20], const int numbers[], int numberOfListings);
void read  (istream& infile, char name[], int& number);
void write (ostream& outfile, const char name[], int number);
int linearSearch (const char searchName[], const char names[][20], int numberOfListings);
int linearSearch (int searchNumber, const int numbers[], int numberOfListings);
int main()
{
   char names [100][20];
   int  numbers[100],searchNumber;
   int numberOfListings = read ("sp13Input.txt", names, numbers);
   write ("con:", "Telephone Listings", names, numbers, numberOfListings);
   char searchName [20];
   cout << "Enter name for searching: ";
   cin >> searchName;
   int location = linearSearch (searchName, names, numberOfListings);
   cout << "linear search results for " << searchName << endl;
   if (location >= 0)
   {
      cout << "array location [" << location << "] contains ";
      write (cout, names[location], numbers[location]);
      cout << endl;
   }  // end if
   else // location < 0
      cout << "name not found by binary search\n";
      cout << "Enter number for searching (nnn-nnnn): ";
      cin>>searchNumber;
   // read & convert to a single integer "searchNumber"
   location = linearSearch (searchNumber, numbers, numberOfListings);
 
   cout << "linear search results for " << searchNumber << endl;
 
   if (location >= 0)
   {
      cout << "array location [" << location << "] contains ";
      write (cout, names[location], numbers[location]);
      cout << endl;
   }  // end if
   else // location < 0
      cout << "number not found by linear search\n";
  
}
int read (const char filename[], char names[][20], int numbers[])
{
   ifstream infile (filename);
   if(!filename) // file not found
   {
      cout << "input file \"sp13Input.txt\" was not found!\n";
      exit (1); // error message
      // halt program (with exit)
   }  // end if
    int numberOfListings=0;// set numberOfListings to 0
   
   read (infile, names[numberOfListings], numbers[numberOfListings]); // see below
   while(strcmp(names[numberOfListings],"zzz")!=0) // names[numberOfListings] !=  "zzz"
   {
      numberOfListings++;
      //infile>>names[numberOfListings]>>numbers[numberOfListings];
      read (infile, names[numberOfListings], numbers[numberOfListings]);
   }  // end while
   infile.close ();
   return numberOfListings;
}  // end function
void read (istream& infile, char name[], int& number)
{
   
   infile >> name;
   int digits3;
   int digits4;
   char hyphen;
   infile  >> digits3 >> hyphen >> digits4;
   number=digits3*10000+digits4;
   //number =digits3*10000+digits4;
        // combine 1st 3 digits (digits3) & last 4 (digits4) as one integer
   return;
}  // end function read
void write (const char filename[], const char description[],
            const char names[][20], const int numbers[], int numberOfListings)
{
   
   ofstream outfile (filename, ios::out);
   outfile << description << endl;
   for(int i=0;i<=numberOfListings-1;i++) // i = 0 to numberOfListings-1
   {
      write (outfile, names[i], numbers[i]); // one entry; see below
     
       outfile << endl;
   }  // end for
   outfile << endl;
   outfile.close ();
   return;
}  // end

void write (ostream& outfile, const char name[], int number)
{
   //int digits3,digits4;
   int prefix =(number/10000); // 1st 3 digits of "number"
   int suffix =(number%10000);// last 4 digits of "number"
   outfile << setiosflags (ios::left) << setw(21) << name << resetiosflags (ios::left)
           << prefix << '-' << suffix;
    
   return;
}  // end function write
int linearSearch (const char searchName[], const char names[][20], int numberOfListings)
{
   // set location to 0, the first name to examine
   bool moreToSearch = true;
   int location=0;
    while (moreToSearch)
   {
      if(strcmp(searchName , names[location])>0) // searchName > names[location]
         location++;
      else if(strcmp(searchName , names[location])==0) // searchName == names[location]
         moreToSearch = false;
      else if(strcmp (searchName , names[location])<0) // searchName < names[location]
      {
         moreToSearch = false;
         location = -1;
      }  // end else
   }  // end while
   return location;
}  // end function linearSearch

int linearSearch (int searchNumber, const int numbers[], int numberOfListings)
{
   // set location to 0, the first name to examine
   bool moreToSearch = true;
   int location=0;
   int prefix =(numbers[location]/10000); // 1st 3 digits of "number"
   int suffix =(numbers[location]%10000); // last 4 digits of "number"
   int prefix1 =(searchNumber/10000); // 1st 3 digits of "searchnumber"
   int suffix1 =(searchNumber%10000); // last 4 digits of "searchnumber"
    while (moreToSearch)
   {
      if(prefix>prefix1) 
         location++;
      else if((prefix==prefix1)&&(suffix==suffix1)) 
         moreToSearch = false;
      else if((prefix<prefix1)||((prefix==prefix1)&&(suffix==suffix1)))
      {
         moreToSearch = false;
         location = -1;
      }  // end else
   int prefix =(numbers[location]/10000); // 1st 3 digits of "number"
   int suffix =(numbers[location]%10000); // last 4 digits of "number"
   }  // end while
   return location;
}  // end function linearSearch
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.