im making a program that loads a collection of strings from a file. then the user need to search the keyword using a binarysearch, but im using strings in my program, how to make the program using array of characters? here's my code, thx before :)

#include <iostream>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

void sort(string array[], int len);
void binarySearch(string array[], int len, string searchItem);
void print(string array[], int len);

int main() {
	int rows=0;
	char filename[30];
	string names[100];
	ifstream fin;
	string searchName;
	
	cout << "Enter a filename: ";
	cin >> filename;
	
	fin.open(filename);
	
	if (!fin) {
		cout << "Error opening file.";
		return 0;
	}
	
	while (getline(fin,names[rows])) {
		rows++;
	}
	
	sort(names,rows);
	print(names,rows);
	
	cout << endl;
	cout << "Enter a word to search it on the list: ";
	cout << endl;
	cin >> searchName;
	binarySearch(names,rows,searchName);

	fin.close();
	return 0;
}

void sort(string array[], int len) {
	int i, j;
	string check;
	
	for(i=1; i<len; i++) {
		check = array[i];
		for(j=i; j>=1 && (check < array[j-1]); j--) {
			array[j] = array[j-1];
			array[j-1] = check;
		}
	}
}





void print(string array[], int len) {
	for(int i=0; i<len; i++) {
		cout << array[i] << endl;
	}
}

void binarySearch(string array[], int len, string searchItem)
{
	int first = 0;
	int last = len - 1;
	int mid;

	bool found = false;

	while 
		(first <= last && !found)
	{
		mid = (first + last) /2;

		if (array[mid] == searchItem)
			found = true;
		else if (array[mid] > searchItem)
			last = mid -1;
		else
			first = mid + 1;
	}
	if (found)
		cout << "you keyword is found : " << searchItem << " in data number : " << mid ;
		
	else
		cout << "Key word not found";
}

It would be a lot easier to use std::vector<std::string> instead of that string array, then call std::find instead of your binary search algorithm

Post a few lines from the data file you are using.

commented: thx :) +0

It would be a lot easier to use std::vector<std::string> instead of that string array, then call std::find instead of your binary search algorithm

Post a few lines from the data file you are using.

i need to make it with array of character, but i cannot picture it. i already read in the books.
here is my .txt file. thx man :)

with
the
he
she
for
to
from
there
here
of
on
in
above
beneath
it
this
that
a
an
and

replace string names[100] with char names[100][40]; replace string searchname with char searchname[40]; void binarySearch(char array[100][], int len, char* searchItem) use strcmp() instea of std::string's overloaded < and > operators

replace string names[100] with char names[100][40]; replace string searchname with char searchname[40]; void binarySearch(char array[100][], int len, char* searchItem) use strcmp() instea of std::string's overloaded < and > operators

where do i put the strcmp, by changing the getline function?

Just follow the code you posted. The algorithm is already there -- just replace < and > ith strcmp()< 0 and strcmp() > 0. If you are not familar with strcmp() then study up on it -- see this for example.

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.