From Integers to Strings Array from File

How can I change this code from integers to strings?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  How to alter code reading string of arrays from file?    
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;

    // Function prototype
    int binarySearch(const int [], int, int);
    const int MAX_SIZE = 1000;
    int main(int argc, char* argv[])

    {
       int values[MAX_SIZE];
       int results,
           count,
           searchKey;
       string fileName;
       ifstream fin;
       if (argc > 1)
            fileName = argv[1];
       else
       {
          cout << "Name of File: ";
          cin >> fileName;
       }
       fin.open(fileName);
       if (!fin)
       {
          perror(fileName.data());
          exit(EXIT_FAILURE);
       }
       count = 0;
       while (count < MAX_SIZE && fin >> values[count])
          count++;
       cout << "Processing " << count << " numbers" << endl;
    
       cout << "Enter Search Key, ctrl-Z to end: ";
       while ( cin >> searchKey )
       {
          results = binarySearch(values, count, searchKey);
          // If searchList returned -1, then searchKey was not found.
          if (results == -1)
              cout << searchKey << " not found" << endl;
          else
             // Otherwise results contains the subscript of
             // the searchKey in the array.
             cout << searchKey << " found at position " << results << endl;
          cout << "Search Key: ";
       }
       return EXIT_SUCCESS;
    }
    
    // The binarySearch function performs a binary search on an     *
    // integer array. array, which has a maximum of size elements,  *
    // is searched for the number stored in value. If the number is *
    // found, its array subscript is returned. Otherwise, -1 is     *
    // returned indicating the value was not in the array.          *
    
    
    int binarySearch(const int array[], int size, int value)
    {
       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 (array[middle] == value)       If value is found at mid
          {
             found = true;
             position = middle;
          }
          else if (array[middle] > value)  If value is in lower half
             last = middle - 1;
          else
             first = middle + 1;           If value is in upper half
       }
       return position;
    }
You could use a typedef / using statement to make the same code work for either int or string types (or other type such as double).

At the start of the code, add a statement something like this:
 
using datatype = int;

"datatype" is just a name which you choose.
see http://www.cplusplus.com/doc/tutorial/other_data_types/

Then go through the code and identify all the places where int is the type of data being handled, and replace "int" by "datatype".
 
int binarySearch(const datatype[], int, datatype);
 
    datatype values[MAX_SIZE];
 
    datatype searchKey;


... and so on.
After you've done this, you should be able to compile and run the program exactly as before.

Now go back to the start, and replace this line:
 
using datatype = int;
with this:
 
using datatype = std::string; 


Now recompile and the program should handle strings instead of integers.

But be careful - there are a lot of integer variables in the program, many of them need to remain as integers, don't just change everything.
Is this correct? it will be reading a list of 400 names.

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

// Function prototype
int binarySearch(const string [], int, int);
const int MAX_SIZE = 400;

Here is how the output should appear:
Name of File: names.txt
Processing 400 strings
Enter Search Key, ctrl-Z to end: Steven
Steven found at position 372
Search Key: Stanley
Stanley not found
Search Key: Natalie
Natalie found at position 314
Search Key: Nancy
Nancy not found
Search Key: Zoey
Zoey found at position 399
Search Key: Aaliyah
Aaliyah found at position 0
Search Key: Zoroaster
Zoroaster not found
Search Key: ^Z

using datatype = std::string;

int main(int argc, datatype argv[])
{
string values[MAX_SIZE];
int results,
count;
string searchKey;
string fileName;
ifstream fin;
if (argc > 1)
fileName = argv[1];
else
{
cout << "Name of File: ";
cin >> fileName;
}
fin.open(fileName);
if (!fin)
{
perror(fileName.data());
exit(EXIT_FAILURE);
}
count = 0;
while (count < MAX_SIZE && fin >> values[count])
count++;
cout << "Processing 400 strings " << endl;

cout << "Enter Search Key, ctrl-Z to end: ";
while ( cin >> searchKey )
{
results = binarySearch(values, count, searchKey);
// If searchList returned -1, then searchKey was not found.
if (results == -1)
cout << searchKey << " not found" << endl;
else
// Otherwise results contains the subscript of
// the searchKey in the array.
cout << searchKey << " found at position " << results << endl;
cout << "Search Key: ";
}
return EXIT_SUCCESS;
}
//***************************************************************
// The binarySearch function performs a binary search on an *
// integer array. array, which has a maximum of size elements, *
// is searched for the number stored in value. If the number is *
// found, its array subscript is returned. Otherwise, -1 is *
// returned indicating the value was not in the array. *
//***************************************************************

int binarySearch(const data type array[], int size, int value)
{
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 (array[middle] == value) // If value is found at mid
{
found = true;
position = middle;
}
else if (array[middle] > value) // If value is in lower half
last = middle - 1;
else
first = middle + 1; // If value is in upper half
}
return position;
}

Is this correct?

Probably not.

It looks like you got in a muddle, some things were changed which should not have been, and vice versa.
Topic archived. No new replies allowed.