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;
}