Ok what I need to do is utilize a binary search algorithm and determine the index location of a set of keys.
The keys are in the file the user enters along with the numbers that have to be searched through. But im lost on how to do this. Do i need to put each key in its own variable?

#include <iostream>				    // Need for cout,cin
#include <iomanip>                   // Need setf,fixed,showpoint,setprecision
#include <stdio.h>				    // Need for getchar
#include <fstream>					// Needed for files
#include <cstdlib>					// Needed for exit function
#include <string>					// Need for string class


using namespace std;


string getInputvalueFileName(string f_Name); // a function to prompt for the complete file name
string getInputkeyFileName(string f_Name); // a function to prompt for the complete file name



int main ()
{
const int h=100;
	const int k = 10;
	int keys[k];
	int j=0;
	int value[h];
	string f_Name;
	ifstream keyinFile;
	ifstream valueinFile;
	string keyfileName,valuefileName;
	keyfileName = getInputkeyFileName(f_Name); // prompt and obtain the full file name
	valuefileName = getInputvalueFileName(f_Name);
	// try to open the file
	keyinFile.open(keyfileName.c_str(),ios::in);
	valueinFile.open(valuefileName.c_str(),ios::in);

	if(keyinFile.is_open())
	{
		cerr << "Key File open Error" ;
		cout << " Press enter to continue" << endl;
		cin.ignore();
		char ch = getchar();
		exit(1);
	}

	if (valueinFile.is_open())
	{
		cerr << "Value File open error " ;
		cout << " Press enter to continue" << endl;
		cin.ignore();
		char ch = getchar();
		exit (1);
	}

	while (!keyinFile.eof()&& j > 10)

	{
		keyinFile >> keys[j];
	j++;
	cout << "test1" << endl;
	}
	 j=0;
while (!valueinFile.eof() && j > 100)

	{
		valueinFile >> value[j];
	j++;
	cout << "test2" << endl;
	}













		keyinFile.close();
	valueinFile.close();





	// keeps program open untill user enters a key

	cout.setf (ios::showpoint );
	cout.setf( ios::fixed);
	cout << setprecision(2);

	cout << "\n\n Press Enter to continue" << endl;
	cin.ignore();
	char ch = getchar();


	return 0;

}



//************************************************************/
//
// Function name: getInputFileName
//
// Purpose: to prompt for the fully qualified name of the key  file
//              i.e. including the path of the file
//
// Input parameters: none
//
// Output parameters: none
//
// Return Value: a string containing the fully qualified name
//               of a file
//
//************************************************************/

string getInputkeyFileName(string f_Name)
{

	cout << "Please enter the fully qualified name of the " << endl
		<< "input text file for the keys being searched (i.e. including the path): ";
	cin >> f_Name ;
	cout << endl; // skip a line

	return f_Name;
}
string getInputvalueFileName(string f_Name)
{

	cout << "Please enter the fully qualified name of the " << endl
		<< "input text file for the values  being searched (i.e. including the path): ";
	cin >> f_Name ;
	cout << endl; // skip a line

	return f_Name;
}

Ok what I need to do is utilize a binary search algorithm and determine the index location of a set of keys.
The keys are in the file the user enters along with the numbers that have to be searched through. But im lost on how to do this. Do i need to put each key in its own variable?

Do you need to find multiple keys? Can you stuff multiple keys into 1 variable? How?

#include <stdio.h>				    // Need for getchar

Why? Can't you use cin.get or something similar? Why use a C function when writing C++?

#include <cstdlib>					// Needed for exit function

Why? Do you call the exit() function when you can simply say return 0; as is the norm?

yes i need to find multiple keys. So i should read them into differnt variables then instead of an array and the exitis b/c we havnt learned anything else yet and the cin.get i guess i can use

yes i need to find multiple keys. So i should read them into differnt variables then instead of an array

An array would work fine. You said "a variable" first, not "an array". They are different.

and the exitis b/c we havnt learned anything else yet

You haven't learned the return statement yet? What's that at the bottom of your main then? return does the same thing no matter where it is in main()

and the cin.get i guess i can use

Much better...

ok this is what i have so far. amd as for the return statements. Our teacher dosnt want us to keep runnign the program if we cant open the file.

#include <iostream>				    // Need for cout,cin
#include <iomanip>                   // Need setf,fixed,showpoint,setprecision
#include <fstream>					// Needed for files
#include <cstdlib>					// Needed for exit function
#include <string>					// Need for string class


using namespace std;


string getInputvalueFileName(string f_Name); // a function to prompt for the complete file name
string getInputkeyFileName(string f_Name); // a function to prompt for the complete file name



int main ()
{
	const int h=100;
	const int k = 10;
	int key1,key2,key3,key4,key5,key6,key7,key8,key9,key10;
	int j=0;
	int value[h];
	string f_Name;
	ifstream keyinFile;
	ifstream valueinFile;
	string keyfileName,valuefileName;
	keyfileName = getInputkeyFileName(f_Name); // prompt and obtain the full file name
	valuefileName = getInputvalueFileName(f_Name);
	// try to open the file
	keyinFile.open(keyfileName.c_str(),ios::in);
	valueinFile.open(valuefileName.c_str(),ios::in);

	if(!keyinFile.is_open())
	{
		cerr << "Key File open Error" ;
		cout << " Press enter to continue" << endl;
		cin.ignore();
		char ch = getchar();
		exit(1);
	}

	if (!valueinFile.is_open())
	{
		cerr << "Value File open error " ;
		cout << " Press enter to continue" << endl;
		cin.ignore();
		char ch = getchar();
		exit (1);
	}

	while (!keyinFile.eof())

	{
		keyinFile >> key1 >> key2 >> key3 >> key4 >> key5 >> key6 >> key7 >> key8 >> key9 >> key10;
		cout << "test1" << endl;
	}
	
	while (!valueinFile.eof() && j > 100)

	{j=0;
		
		valueinFile >> value[j];
		j++;
		cout << "test2" << endl;
	}













	keyinFile.close();
	valueinFile.close();





	// keeps program open untill user enters a key

	cout.setf (ios::showpoint );
	cout.setf( ios::fixed);
	cout << setprecision(2);

	cout << "\n\n Press Enter to continue" << endl;
	cin.ignore();
	char ch = cin.get();


	return 0;

}



//************************************************************/
//
// Function name: getInputFileName
//
// Purpose: to prompt for the fully qualified name of the key  file
//              i.e. including the path of the file
//
// Input parameters: none
//
// Output parameters: none
//
// Return Value: a string containing the fully qualified name
//               of a file
//
//************************************************************/

string getInputkeyFileName(string f_Name)
{

	cout << "Please enter the fully qualified name of the " << endl
		<< "input text file for the keys being searched (i.e. including the path): ";
	cin >> f_Name ;
	cout << endl; // skip a line

	return f_Name;
}
string getInputvalueFileName(string f_Name)
{

	cout << "Please enter the fully qualified name of the " << endl
		<< "input text file for the values  being searched (i.e. including the path): ";
	cin >> f_Name ;
	cout << endl; // skip a line

	return f_Name;
}

An array would work fine. You said "a variable" first, not "an array". They are different.

You haven't learned the return statement yet? What's that at the bottom of your main then? return does the same thing no matter where it is in main() Much better...

Edited 6 Years Ago by smeghead007: n/a

ok this is what i have so far. amd as for the return statements. Our teacher dosnt want us to keep runnign the program if we cant open the file.

I see you don't believe me at all. As an experiment, change exit(1); to return 1; and see if the program continues if the file doesn't exist.

Edited 6 Years Ago by WaltP: n/a

ok I take your word for it but now how do i search the other arrary mainly how do I return the spot the number is in?

I see you don't believe me at all. As an experiment, change exit(1); to return 1; and see if the program continues if the file doesn't exist.

yes i need to find multiple keys. So i should read them into differnt variables then instead of an array

An array would work fine.

What happened to the array? It would certainly make it easier now that you've added almost enough information for us to understand what you're doing. Almost...

Set up a nested loop. Outside loop can go through each value, inside loop can go through each key.

ok I have the loop and everything but whenever i print it out it jsut gives me location at 0,1,2,3,4,5,6 i think its my logic... any help would be great.

#include <iostream>				    // Need for cout,cin
#include <iomanip>                   // Need setf,fixed,showpoint,setprecision
#include <fstream>					// Needed for files
#include <cstdlib>					// Needed for exit function
#include <string>					// Need for string class


using namespace std;


string getInputvalueFileName(string f_Name); // a function to prompt for the complete file name
string getInputkeyFileName(string f_Name); // a function to prompt for the complete file name



int main ()
{
	const int h=100;
	const int k = 10;
	int key[k];
	int j=0;
	int t=0;
	int value[h];
	string f_Name;
	ifstream keyinFile;
	ifstream valueinFile;
	string keyfileName,valuefileName;
	keyfileName = getInputkeyFileName(f_Name); // prompt and obtain the full file name
	valuefileName = getInputvalueFileName(f_Name);
	// try to open the file
	keyinFile.open(keyfileName.c_str(),ios::in);
	valueinFile.open(valuefileName.c_str(),ios::in);

	if(!keyinFile.is_open())
	{
		cerr << "Key File open Error" ;
		cout << " Press enter to continue" << endl;
		cin.ignore();
		char ch = getchar();
		exit(1);
	}

	if (!valueinFile.is_open())
	{
		cerr << "Value File open error " ;
		cout << " Press enter to continue" << endl;
		cin.ignore();
		char ch = getchar();
		exit (1);
	}

	while (!keyinFile.eof()&& t < 10)
	{
	
		keyinFile >> key[t];
		t++;
			cout << "test1" << endl;
	}

	while (!valueinFile.eof() && j > 100)

	{j=0;

	valueinFile >> value[j];
	j++;
	cout << "test2" << endl;
	}
int n=0;
int count = 0 ;
int v=0;
	for ( key[n] = 0; n < 10; n++)
	{
		while (key[n] != value[v])
		{
		 if ( v < 100 )
		 {
			 v++;
		 count++;
		 }
		else 
			cout << " The key was not found in the file" << endl;
		 break;
		}
cout << " The Key" << "  " << key[n] << " was found at index " << "  " << count << endl;
	}








	keyinFile.close();
	valueinFile.close();





	// keeps program open untill user enters a key

	cout.setf (ios::showpoint );
	cout.setf( ios::fixed);
	cout << setprecision(2);

	cout << "\n\n Press Enter to continue" << endl;
	cin.ignore();
	char ch = cin.get();


	return 0;

}



//************************************************************/
//
// Function name: getInputFileName
//
// Purpose: to prompt for the fully qualified name of the key  file
//              i.e. including the path of the file
//
// Input parameters: none
//
// Output parameters: none
//
// Return Value: a string containing the fully qualified name
//               of a file
//
//************************************************************/

string getInputkeyFileName(string f_Name)
{

	cout << "Please enter the fully qualified name of the " << endl
		<< "input text file for the keys being searched (i.e. including the path): ";
	cin >> f_Name ;
	cout << endl; // skip a line

	return f_Name;
}
string getInputvalueFileName(string f_Name)
{

	cout << "Please enter the fully qualified name of the " << endl
		<< "input text file for the values  being searched (i.e. including the path): ";
	cin >> f_Name ;
	cout << endl; // skip a line

	return f_Name;
}

Helping you is kinda like pulling teeth. You completely ignore suggestions like exit(1) => return 1, you give useless information like

ok I have the loop and everything but whenever i print it out it jsut gives me location at 0,1,2,3,4,5,6 i think its my logic...

and expect us to know where your problem is and what it is. If you would post the relevant code, or at least tell us where the problem is, and post details, we might have some ideas.

This article has been dead for over six months. Start a new discussion instead.