/*could someone show me how to create a text file named names.dat with names in a directory with an executable file? i want this program to find a name after i enter it. here r the names that i want to put into names.dat.:*/

"Collins, Bill", "Smith, Bart", "Allen, Jim",
"Griffin, Jim", "Stamey, Marty", "Rose, Geri",
"Taylor, Terri", "Johnson, Jill", "Allison, Jeff",
"Looney, Joe", "Wolfe, Bill", "James, Jean",
"Weaver, Jim", "Pore, Bob", "Rutherford, Greg",
"Javens, Renee", "Harrison, Rose", "Setzer, Cathy",
"Pike, Gordon", "Holland, Beth"

//here is the code for the program.:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int linear_search(string const array[], int amount, string const& name) {
  for (int i = 0; i < amount; ++i)
    if (array[i] == name)
      return i + 1;
  return 0;
}

void selection_sort(string array[], int amount) {
  for (int i = 0; i < amount; ++i)
    for (int j = i + 1; j < amount; ++j)
      if (array[i] > array[j]) {
        string tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
      }
}

int binary_search(string array[], int amount, string const& name) {
  int l = 0, r = amount - 1;
  int comparisons = 0;

  selection_sort(array, amount);

  while (l <= r) {
    ++comparisons;
    int m = (l + r) / 2;
    if (name < array[m])
      r = m - 1;
    else if (name > array[m])
      l = m + 1;
    else 
      return comparisons;
  }

  return 0;
}

int main()
{
  const int NUM_NAMES = 20;
  string array[NUM_NAMES];

  ifstream input("names.dat");
  if (!input) {
    cout << "Cannot open input file: names.dat\n";
    return -1;
  }

  int amount = 0;
  while (getline(input, array[amount]))
    ++amount;

  string name;
  cout << "Enter a name for searching: ";
  getline(cin, name);

  int comparisons;
  cout << "Linear search: ";
  if (comparisons = linear_search(array, amount, name))
    cout << "Amount of comparisons: " << comparisons << "\n";
  else
    cout << "Not found\n";

  cout << "Binary search: ";
  if (comparisons = binary_search(array, amount, name))
    cout << "Amount of comparisons: " << comparisons << "\n";
  else
    cout << "Not found\n";
  return 0;
}

//thanks for the help.

You don't need to make your questions into comments. They are not code.

As for making the file, try Notepad.exe

well, it turned a colon & asterisk into a love emoticon. sometimes u learn things by accident. WaltP: could u please explain step-by-step how to do that?

well, it turned a colon & asterisk into a love emoticon. sometimes u learn things by accident. WaltP: could u please explain step-by-step how to do that?

Look in the start menu under accessories.

>>i want this program to find a name after i enter it. here r the names that i want to put into names.dat.

Alternatively, create a section of your program to write whatever information you want to the desired file before you download the file back to your program. Not necessarily an effecient use of your time in the real world, but it puts the desired file in the easiest place to find it and it gives you practice both writing to and reading from files.

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.