I have to create a program that reads data from a file about census info specifically a last name and the frequency of which last name appeared in census. I have to call a function to read in the data, calls a function to display the data, do a binary search of some names that are inputted by the data file, call a function to sort them by frequency, and then call the previous function to display the last name and frequency.
Okay so I'm completely lost. I've created a few arrays; array 1 contains the names; array 2 contains the frequency; and array 3 contains the names needing to be searched. I need array 3 go through each element and search for a match element in array 1. When it finds the matching element I need it to display that array element number for array 2. (IE array 1 {Anderson, Brown}; array 2{12, 4}; array 3 {Brown} okay take array 3's info which is search for Brown in array 1; Brown is in the second element so dispaly array 2's info in the second element which is 4.) I'm not sure how to do all of that in a binary search.
Also I'm not sure what the document meants by call the previous function to display the name and frequency. Yes I do have a function to sort the arrays; but how do I "call the function to display" rather then make a new function.
I'm also getting lots of fun errors:
pg1.cpp:27: error: too few arguments to function 'int binary(int, std::string, std::string*, int*, std::string*)'
pg1.cpp:69: error: at this point in file
pg1.cpp: In function 'void getInfo(int, std::string*, int*, std::string*)':
pg1.cpp:106: warning: statement has no effect
pg1.cpp: In function 'int binary(int, std::string, std::string*, int*, std::string*)':
pg1.cpp:143: error: 'searchname' was not declared in this scope
pg1.cpp:148: error: no match for 'operator>' in '*(((std::string*)(((long unsigned int)middle) * 8ul)) + searchnames) > searchnames'

Thanks for any help you can give me.


#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

// Function prototypes

void getInfo(int, string[], int[], string []);
void displayInfo(int, string[], int[]);
int binary(int, string, string[], int[], string[]);
int sort(int, string[], int[]);
void displayFrequency(int, string [], int []);

const int SIZE = 25;

int main()
    // Names and Types of inputs

    string names[SIZE];
    int frequency[SIZE];
    string searchnames[5];
    string name;
    int position;

    getInfo(SIZE, names, frequency, searchnames);
    displayInfo(SIZE, names, frequency);

    cout << endl << "Please enter a name to search for:  ";
    cin  >> name;

    position = binary(SIZE, name, names, frequency);
    if (position == -1)
        cout << name << " is an not in the list" << endl;
        cout << name << " is a valid charge account number" << endl;
        cout << name <<

     sort(SIZE, names, frequency);

     cout << "Last Names By Frequency Rank" <<endl;
     displayFrequency(SIZE, names, frequency);

   return 0;

void getInfo(int SIZE, string names[], int frequency[], string searchnames[])
string temp;
int t=0;
    for (int s = 0; s < SIZE; s++)
        cin >> names[s];
        cin >> frequency[s];
    cin >>temp;
    while (temp !="ZZZ")

void displayInfo(int size, string names[], int frequency[])
    cout << endl << "Last Names Alphabetically" << endl;
    cout << endl << "Alete Robison" << endl;
    cout << endl << "________________________" << endl;
    cout << endl << "Name         Frequency" << endl;
    for (int s = 0; s < SIZE; s++)

        cout << setw(14)  << names[s];
        cout << setw(14)  << frequency[s] << endl;
int binary(int SIZE, string name, string names[], int frequency[], string searchnames [])
    int first = 0,        // First array element
        last = SIZE -1,   // Last array element
        middle,           // Mid point of search
        position = -1;    // Position of search value
    bool found = false;   // Flag

    while (!found && first <= last)
        middle = (first + last) / 2;     // Calculate mid point
        if (searchnames[middle]== searchname)        // If value is found at mid
             found = true;
             position = middle;
        else if (searchnames[middle] > searchnames)   // If value is in lower half
             last = middle - 1;
             first = middle + 1;

return position;
int sort(int SIZE, string names[], int frequency[])
   int count, s, temp;
   string temp2;
      for (count =0; count<SIZE; count++)
        for (s=0; (s<SIZE-1); s++)
           if (frequency[s]<frequency[s+1])
            temp =frequency[s];
            frequency[s+1] = temp;
            temp2= names[s];
            names[s]= names[s+1];
return 0;

void displayFrequency(int SIZE, string names[], int frequency[])
     cout << endl << "Name   Frequency" << endl;
    for (int s = 0; s < SIZE; s++)
        cout << "  " << left << setw(10) << names[s]
             << right << setw(8) << frequency[s] << endl;

This a mess, and the main reason is that you have tried to do WAY to much in one go.

So, you really really need to start again. I can assure you that it will help. Open a clean editor window and start again. The problems of your code are many and most are that you didn't have a short piece of code to test and get right before moving on.

So what to do this time that will prevent the mess of the first one:

Start with one component. Let us say you are going to read some data from the census. What about creating a class for that information. e.g.

class censusRecord

     std::string Name;
     int frequency;
     censusRecord(const censusRecord&);
     censusRecord& operator=(const censusRecord&);

     int getFreq() const;
     const std::string& getName() const;

      void write(std::ostream&) const;
      int read(std::istream&); // returns -1 on failure / 0 success

Quick comment on the class (a) it can readily be extended as extra information is required. You can easily add comparison operators if you want to go down the route of stl sort and such like. It lets you get the data so that comparison functors or simple functions to do comparison are easy to write. (b) I have provided read/ write so that you can write operator<< if you wish or just a getline and check the line against the read , if good store the result.

Then you have a class for reading and writing the data from a file stream. You can have another class that contains an array/vector of them. You can use them as they are in a main program.

Finally: Do not be afraid of writing several programs then combining the result. For example use a simple test program with integers to test your binary search algorithm. Then add it to the program that reads the census data.

Be a part of the DaniWeb community

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