0

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.

For anyone curious, all of my data is "dummy data" from a website I used to generate dummy data.

#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.

anothermh
  • 6,482
  • 3
  • 20
  • 39
  • 2
    You should use `std::find` – Jake Freeman Dec 15 '17 at 02:41
  • 1
    There are multiple fundamental bugs in the shown code. `string LastName[ArraySize] ` -- variable-length arrays is not standard C++. `while(myfile.good())` -- same fundamental bug as [while (!myfile.eof()) is always a bug](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). `int i = 1;` -- the first loop starts populating the arrays starting at index 1, but then `for(int i=0; ...` starts indexing array starting at array index 0, instead of 1. You need to use a debugger to step through your code, and look what's in all the arrays, at runtime. – Sam Varshavchik Dec 15 '17 at 02:52
  • 1
    I wish g++ or whatever compiler would turn **off** the setting to allow Variable Length Arrays, and only have the programmer enable the setting (thus only programmers who know about it and know exactly that it isn't standard C++ will make use of it). There are too many posters, mostly beginners in C++, being led down the wrong track by using things like `string LastName[ArraySize]`, believing they're writing valid C++ code. As to what to use instead -- use `std::vector`. – PaulMcKenzie Dec 15 '17 at 03:23
  • Edit a couple of lines from your input file into the question. – rustyx Jan 07 '19 at 08:30

1 Answers1

0

It looks very much like you are picking up a carriage-return \r character at the end of the strings that you are reading from the file.

std::getline will read up to but excluding the end of line character \n (see for example the second entry at cppreference), but text files under Windows will typically end lines with the two character sequence \r\n.

You can confirm if this is the issue by checking if the last character of the strings you read in is in fact equal to \r. If so, it can be fixed by trimming off that last character with, for example, the string pop_back method.

halfflat
  • 1,530
  • 7
  • 11