Unable to find data in an array (from file)

Hello, first time poster. I'm still learning this forum type.

My code is designed to pull data from a file, "dataset.txt" and it does. It's able to read it, ingest it into my arrays and display it on command.

The problem comes in when I attempt to search for items within the array that I have pulled in from dataset.txt. My code doesn't seem to be able to read that it's there and displays no matching results.

All of my data is "dummy data" from a website I used to generate dummy data.


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
#include <cstdlib>
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <stdio.h>
#include <vector>

using namespace std;

int main(int argc, char** argv) {
    
    int ArraySize = 100; // sets array to 100 items
    
    // initializes all possible arrays in the contact manager
    string LastName[ArraySize] = "no data";
    string FirstName[ArraySize] = "no data";
    string Phone[ArraySize] = "no data";
    string Email[ArraySize] = "no data";
    string NullOne = "";
    
    // Designed to pull data from the file "dataset.txt"
    
    int i = 1;
    
    std::ifstream myfile;
    myfile.open( "dataset.txt", ios::in );

    if(!(myfile.is_open())) {
        cout << "Error Opening File" << endl;
    }
    else {
        cout << "File can be read.  Will pull data." << endl;
    }
    
    while(myfile.good())
    {
        getline(myfile, NullOne);
        getline(myfile, LastName[i]);
        getline(myfile, FirstName[i]);
        getline(myfile, Phone[i]);
        getline(myfile, Email[i]);
        
        i++;
    }
    
    cout << "Data pull complete!" << endl;
    
    myfile.close();
    
    // Designed to search all data for a specified name string
    
    string NameSearch = "null";
    int Matches = 0; // indicates that there is a match at all
    
    cout << "Search for name" << endl;
    cout << "Please input name : " << endl;
    cout << LastName[4] << endl; // gives me a search parameter to use
    cin >> NameSearch;
    
    
    // search for the name in the Name array
    
    for(int i=0; i<ArraySize; i++) {
        cout << LastName[i] << endl;
       if (LastName[i] == NameSearch) {
           cout << "Name  : " << LastName[i] << endl;
           cout << "Phone : " << Phone[i] << endl << endl;
           Matches++;
       }
   }
    
    if (Matches == 0) {
        cout << "No matches found" << endl << endl;
    }
    else {
        cout << Matches << " matches found" << endl << endl;
    }

    return 0;
} 


What it outputs is:

File can be read.  Will pull data.
Data pull complete!
Search for name
Please input name : 
Chaney
Chaney
no data
Walton
Young
English
Chaney
Carpenter
Castaneda
Potter
Blackwell
Carter
Dyer
Yates
Bentley
Pitts
Dawson
Christensen
Goodwin
Boone
Dunn
Booth
Holman
no data (it displays this for about 40 more lines, I cut it out)
No matches found


The match that it SHOULD find is:

Chaney
Penelope
(518) 996-0514
eget@iaculis.edu


I know that this is a dirty method and I have a plan to clean it up but I can't get it to work.
Last edited on
wild guess, the line-breaks in your file are on the form "\r\n"
so it doesn't have "Chaney" but "Chaney\r"


By the way, string LastName[ArraySize] = "no data"; doesn't compile.
no data (it displays this for about 40 more lines, I cut it out)

You should tighten up the code so it only searches array elements which have been read from the file.

No matches found

As suggested, there may be whitespace included as part of the names, hence it won't give an exact match. What you might do (better to check the input file and clean it up if necessary), is to use string::find, and expect to find the search value either at the beginning of the string, or possibly anywhere within it.

1
2
3
    if (LastName[i] == NameSearch)         // exact match only
    if (LastName[i].find(NameSearch) == 0)   // find at start of name
    if (LastName[i].find(NameSearch) != string::npos)   // find anywhere in name 

Also the compare is case-sensitive, capitalisation must match exactly.

Here I use a count variable to track number of items read from the file. (It would be better to use a vector - and make the contact a struct).

Note in ISO standard C++ the array size must be a constant.

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
83
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
    
    const int ArraySize = 100; // sets array to maximum 100 items
    
    // initializes all possible arrays in the contact manager
    string LastName[ArraySize];
    string FirstName[ArraySize];
    string Phone[ArraySize];
    string Email[ArraySize];
    string NullOne;
        
    // Designed to pull data from the file "dataset.txt"
        
    ifstream myfile("dataset.txt");

    if (!myfile)
    {
        cout << "Error Opening File" << endl;
        return 1;
    }
    
    cout << "File can be read.  Will pull data." << endl;
    
    int count = 0;
        
    while ( count < ArraySize                  && 
            getline(myfile, NullOne)           &&
            getline(myfile, LastName[count])   &&
            getline(myfile, FirstName[count])  &&
            getline(myfile, Phone[count])      &&
            getline(myfile, Email[count]) )
    {    
        count++;
    }
        
    cout << "Number of records read = " << count << endl;
        
    
    // Designed to search all data for a specified name string
    
    string NameSearch;
    int Matches = 0; // indicates that there is a match at all
    
    cout << "Search for name" << endl;
    cout << "Please input name : " << endl;
    cout << LastName[4] << endl; // gives me a search parameter to use
    cin >> NameSearch;
    
    
    // search for the name in the Name array
    
    for (int i=0; i<count; i++)
    {
        cout << LastName[i] << endl;
//        if (LastName[i] == NameSearch)         // exact match only
//        if (LastName[i].find(NameSearch) == 0)   // find at start of name
        if (LastName[i].find(NameSearch) != string::npos)   // find anywhere in name
        {
           cout << "Name  : " << LastName[i] << endl;
           cout << "Phone : " << Phone[i] << endl << endl;
           Matches++;
        }
   }
    
    if (Matches == 0) 
    {
        cout << "No matches found" << endl << endl;
    }
    else 
    {
        cout << Matches << " matches found" << endl << endl;
    }

    return 0;
} 




Last edited on
Topic archived. No new replies allowed.