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.