C++ merging duplicated search results from arrays - c++

Okay so I have an array like this:
class name {
public:
string first;
int last;
name(string a, int b){
first = a;
last = b;
}
};
name arr[] { { "John", 1 }, { "Jane", 2 }, { "Dick", 3 }, { "John", 1 }, { "Jane", 2 } };
For now I am only able to print them all out like this:
int main() {
int icount = sizeof(arr) / sizeof(name);
for (int i = 0; i < icount; i++){
cout << arr[i].first << " " << arr[i].last << endl;
}
}
Output will be like this:
John 1
Jane 2
Dick 3
John 1
Jane 2
However I need merge any similar results, meaning if the name is the same I need to add up the numbers behind them. The desired output should be like this:
John 2
Jane 4
Dick 3
Is there any function I can use to merge them or how should I go about doing it?

Here is another variant:
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
class name {
public:
string first;
int last;
name(string a, int b);
};
name::name(string a, int b) : first(a), last(b)
{
}
int main(int argc, char **argv)
{
string names[] =
{
"John", "Jane", "Dick", "John", "Jane"
};
int values[] =
{
1, 2, 3, 1, 2
};
vector<name> people;
for (int i = 0; i < sizeof(names) / sizeof(*names); ++i)
{
people.push_back(name(names[i], values[i]));
}
map<string, int> unique_people;
for (vector<name>::const_iterator it = people.begin(),
end = people.end(); it != end; ++it)
{
if (unique_people.count(it->first) != 0)
{
unique_people[it->first] += it->last;
}
else
{
unique_people.insert(pair<string, int>(it->first, it->last));
}
}
for (map<string, int>::const_reverse_iterator it = unique_people.rbegin(),
end = unique_people.rend(); it != end; ++it)
{
cout << it->first << " " << it->second << endl;
}
return 0;
}

Since you are using C++ you can use Map to finish your job.
Here is the complete program you need.
#include <iostream>
#include <map>
using namespace std;
map<string, int> mer;
class name
{
public:
string first;
int last;
name (string a, int b)
{
first = a;
last = b;
}
};
name arr[] { { "John", 1 }, { "Jane", 2 }, { "Dick", 3 }, { "John", 1 }, { "Jane", 2 } };
void merge()
{
int icount = sizeof(arr) / sizeof(name);
for (int i = 0; i < icount; i++)
{
if (mer.count(arr[i].first) > 0)
{
mer[arr[i].first]++;
}
else
{
mer[arr[i].first] = 1;
}
}
}
int main()
{
int icount = sizeof(arr) / sizeof(name);
merge();
map<string,int>::iterator it;
for(it = mer.begin(); it != mer.end(); it++)
{
cout << it->first << " " << it->second << endl;
}
return 0;
}
This will print the result of what you expected. Of course it will print in reverse order. You can use reverse iterator to get the desired result.

Related

Access violation executing in classes with inheritance/ c++

I have a base class Participant and I need to sort object in array of participant by quantity of their prizes or diplomas. The virtual function get_data return this number. But whileI try to sort, i've got error Access violation executing in row with comparing 2 numbers.
if (p[j].Get_Data() < p[i].Get_Data()) {
This is my full code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Participant {
protected:
string name;
string surname;
vector <string> type;
int age;
public:
Participant() : name(""), surname(""), type(), age(0)
{}
Participant(string name, string surname, vector <string> type, int age) {
this->name = name;
this->surname = surname;
this->type = type;
this->age = age;
};
Participant(const Participant& other) : name(other.name), surname(other.surname), type(other.type), age(other.age)
{
}
Participant& operator=(const Participant& other)
{
name = other.name;
surname = other.surname;
type = other.type;
age = other.age;
return *this;
}
virtual int Get_Data() {
return 0;
}
friend void Sort(Participant* p);
};
class Diploma : public Participant {
protected:
vector<int> places;
vector <string> type_d;
public:
Diploma() : places(), type_d{}
{}
Diploma(string name, string surname, vector <string> type, int age, vector<int> places, vector <string> type_d);
int Get_Data() override {
cout << "diplom prize" << endl;
cout << type_d.size() << endl;
return type_d.size();
}
};
class Prize : public Participant {
protected:
vector <int> places;
vector<string> prize;
public:
Prize(string name, string surname, vector <string> type, int age, vector<int> places, vector<string> prize);
int Get_Data() override{
cout << "Cont prize" << endl;
cout << prize.size() << endl;
return prize.size();
}
};
Prize::Prize(string name, string surname, vector <string> type, int age, vector<int> places, vector<string> prize) {
this->name = name;
this->surname = surname;
this->type = type;
this->age = age;
this->places = places;
this->prize = prize;
}
Diploma::Diploma(string name, string surname, vector <string> type, int age, vector<int> places, vector <string> type_d){
this->name = name;
this->surname = surname;
this->type = type;
this->age = age;
this->places = places;
this->type_d = type_d;
}
void Sort(Participant* p) {
Participant temp;
for (int i = 0; i < 3; i++) {
for (int j = i + 1; j < 3; j++)
{
if (p[j].Get_Data() < p[i].Get_Data()) {
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
}
int main() {
Participant* pa[3];
pa[0] = new Diploma("Alex", "Smith", { "Geo","Math" }, 17, { 1,6 }, { "first"});
pa[1] = new Prize("Helen", "Blink", { "IT","Math" }, 18, { 2,2 }, {"Golden medal", "medal"});
pa[2] = new Prize("Brandon", "Brown", { "IT","Math" }, 18, { 2,2 }, { "Golden medal", "medal","gold"});
Sort(*pa);
for (int i = 0; i < 3; i++) {
pa[i]->Get_Data();
cout << endl;
}
}
The issue here is that when you are passing *pa, only the first element is accessible. If you run it in debug mode you will be able to see that inside sort function, p[1] is not a valid object. Basically, only p[0] is passed to the function. You should pass the reference to the array i.e. **pa to the sort function
void Sort(Participant **p)
{
Participant *temp;
for (int i = 0; i < 3; i++)
{
for (int j = i + 1; j < 3; j++)
{
if (p[j]->Get_Data() < p[i]->Get_Data())
{
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
}
int main()
{
Participant *pa[3];
pa[0] = new Diploma("Alex", "Smith", {"Geo", "Math"}, 17, {1, 6}, {"first"});
pa[1] = new Prize("Helen", "Blink", {"IT", "Math"}, 18, {2, 2}, {"Golden medal", "medal"});
pa[2] = new Prize("Brandon", "Brown", {"IT", "Math"}, 18, {2, 2}, {"Golden medal", "medal", "gold"});
Sort(pa);
for (int i = 0; i < 3; i++)
{
pa[i]->Get_Data();
cout << endl;
}
}

Exception thrown: read access violation. **_Right_data** was 0x4. C++

I tried everything and looked everywhere but I keep getting
Exception thrown: read access violation.
**_Right_data was 0x4.**
What is wrong with the code? I am not very good with C++ and don't like it at all but working on this school project and trying to figure out why I get the exception. Any help is appreciated
#include "Roster.h"
#include "Student.h"
#include "NetworkStudent.h"
#include "SecurityStudent.h"
#include "SoftwareStudent.h"
#include <iostream>
#include <string>
#include<vector>
#include<sstream>
#include<regex>
// Separates Data on basis of ,
template <class Container>
void splitString(const std::string& str, Container& cont)
{
char delim = ',';
std::stringstream ss(str);
std::string token;
while (std::getline(ss, token, delim)) {
cont.push_back(token);
}
}
// Checks and returns if email is valid or not
bool Email(std::string email)
{
const std::regex pattern("(\\w+)(\\.|_)?(\\w*)#(\\w+)(\\.(\\w+))+");
return regex_match(email, pattern);
}
// Main Function
int main()
{
Roster classRoster;
// Data Array
const std::string studentData[] = { "A1,John,Smith,John1989#gm ail.com,20,30,35,40,SECURITY",
"A2,Suzan,Erickson,Erickson_1990#gmailcom,19,50,30,40,NETWORK",
"A3,Jack,Napoli,The_lawyer99yahoo.com,19,20,40,33,SOFTWARE",
"A4,Erin,Black,Erin.black#comcast.net,22,50,58,40,SECURITY",
};
for (unsigned int i = 0;i < 5;i++)
{
std::vector<std::string> words;
std::string s = studentData[i];
// Splits Input
splitString(s, words);
// if Student belongs to Security
if (words.at(8) == "SECURITY")
{
int a[3] = { stoi(words.at(5)),stoi(words.at(6)),stoi(words.at(7)) };
classRoster.classRosterArray[i] = new SecurityStudent(words.at(0), words.at(1), words.at(2), words.at(3), stoi(words.at(4)), a, SECURITY);
}
// IF Student Belongs to Software
else if (words.at(8) == "SOFTWARE")
{
int a[3] = { stoi(words.at(5)),stoi(words.at(6)),stoi(words.at(7)) };
classRoster.classRosterArray[i]=new SoftwareStudent(words.at(0), words.at(1), words.at(2), words.at(3), stoi(words.at(4)), a, SOFTWARE);
}
// If Student Belongs to Network
else if (words.at(8) == "NETWORK")
{
int a[3] = { stoi(words.at(5)),stoi(words.at(6)),stoi(words.at(7)) };
classRoster.classRosterArray[i] = new NetworkStudent(words.at(0), words.at(1), words.at(2), words.at(3), stoi(words.at(4)), a, NETWORK);
}
}
std::cout << "\n---------Print All Student's Data------------\n";
classRoster.printAll();
std::cout << "\n---------------------------------------------\n";
std::cout << "Invalid Emails\n";
classRoster.printInvalidEmails();
std::cout << "\n--------------Days in Course------------------\n";
classRoster.printDaysInCourse("A2");
std::cout << "\n--------------By Degree Program------------------";
classRoster.printByDegreeProgram(3);
std::cout << "\n--------------Removes A3------------------\n";
classRoster.remove("A3");
classRoster.remove("A3");
return 0;
}
// Adds Student to Roster
void Roster::add(std::string studentID, std::string firstName, std::string lastName, std::string emailAddress, int age, int daysInCourse1, int daysInCourse2, int daysInCourse3, degree d)
{
if (d == SOFTWARE)
{
delete classRosterArray[4];
int a[3] = { daysInCourse1,daysInCourse2,daysInCourse3 };
classRosterArray[4] = new SoftwareStudent(studentID, firstName, lastName, emailAddress, age,a, d);
}
else if (d == NETWORK)
{
delete classRosterArray[4];
int a[3] = { daysInCourse1,daysInCourse2,daysInCourse3 };
classRosterArray[4] = new NetworkStudent(studentID, firstName, lastName, emailAddress, age, a, d);
}
else if (d == SECURITY)
{
delete classRosterArray[4];
int a[3] = { daysInCourse1,daysInCourse2,daysInCourse3 };
classRosterArray[4] = new SecurityStudent(studentID, firstName, lastName, emailAddress, age, a, d);
}
}
// Removes Student
void Roster::remove(std::string studentID)
{
for (unsigned i = 0;i < 5;i++)
{
if (classRosterArray[i]->getStudentID() == studentID){
std::cout << "STUDENT REMOVED " << classRosterArray[i]->getStudentID()<<"\n";
classRosterArray[i] = NULL;
}
}
std::cout << "Student ID Does not Exist \n";
}
// Prints All Data Of Array
void Roster::printAll()
{
for (unsigned i = 0;i < 5;i++)
{
std::cout << i + 1 << "\t";
classRosterArray[i]->print();
std::cout << "\n";
}
}
// Prints Average Days in Course for a specific Student
void Roster::printDaysInCourse(std::string studentID)
{
int sum = 0;
for (unsigned i = 0;i < 5;i++)
{
if (classRosterArray[i]->getStudentID() == studentID)
{
int *p = classRosterArray[i]->getStudentDaysofCourses();
for (unsigned int j = 0;j < 3;j++)
sum += p[j];
delete[]p;
break;
}
}
std::cout << sum / 3.0;
}
// Prints Invalid Emails
void Roster::printInvalidEmails()
{
for (unsigned i = 0;i < 5;i++)
{
std::string email = classRosterArray[i]->getStudentEmail();
if (!Email(email))
{
std::cout << classRosterArray[i]->getStudentEmail();
//classRosterArray[i]->print();
std::cout << "\n";
}
}
}
// Prints By Degree Program
void Roster::printByDegreeProgram(int degreeProgram)
{
for (unsigned i = 0;i < 5;i++)
{
if (classRosterArray[i]->getDegreeProgram()==degreeProgram)
classRosterArray[i]->print();
std::cout << "\n";
}
}
// Destructor
Roster::~Roster()
{
for (unsigned i = 0; i < 5; i++)
{
if (classRosterArray[i]!=NULL)
delete classRosterArray[i];
}
}
Figured it out!
This part was causing the error
// Removes Student
void Roster::remove(std::string studentID)
{
for (unsigned i = 0;i < 5;i++)
{
if (classRosterArray[i]->getStudentID() == studentID){
std::cout << "STUDENT REMOVED " << classRosterArray[i]->getStudentID()<<"\n";
classRosterArray[i] = NULL;
}
}
std::cout << "Student ID Does not Exist \n";
}
Changed to
// Removes Student
void Roster::remove(std::string studentID)
{
for (unsigned i = 0; i < 5; i++)
{
if (classRosterArray[i] != NULL && classRosterArray[i]->getStudentID() == studentID) {
std::cout << "STUDENT REMOVED " << classRosterArray[i]->getStudentID() << "\n";
delete classRosterArray[i];
classRosterArray[i] = NULL;
return;
}
}
std::cout << "Student ID Does not Exist \n";
}

Printing from Trie

I have built a trie in C++ designed to hold words of sentences. Each sentence will have a weight which determines the order in which they should be output. I have several recursive functions that call other recursive functions, and the dilemma I am facing is that I want to print my list only once.
Basically my get function calls the printFromNode function which creates the vector of pairs p that I want to sort and print. If someone could point me in the right direction in how to do that it would be much appreciated.
Code:
Trie.cpp:
//#include "Trie.h"
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <sstream>
#include <stack>
using namespace std;
class Node
{
private:
string word = "";
bool endOfSentence = false;
int weight = -1;
public:
vector<Node> children = {};
Node() {
this->setWord("");
}
Node(string s){
this->setWord(s);
}
string getWord(){
return this->word;
}
void setWord(string s) {
this->word = s;
}
void setEOS(){
this->endOfSentence = true;
}
void setWeight(int weight){
this->weight = weight;
}
int getWeight() {
return this->weight;
}
};
class Trie
{
public:
Node root;
void add(vector<string> phrase, int weight, Node* n){
Node* current = n;
int w = weight;
int found = -1;
for (int i = 0; i < current->children.size(); i++) {
if (phrase[0] == current->children[i].getWord()) {
found = i;
}
}
if (found > -1) {
current = &current->children[found];
phrase.erase(phrase.begin());
add(phrase, w, current);
}
else {
addPhrase(phrase, w, current);
}
}
void addPhrase(vector<string> phrase, int weight, Node* n) {
Node* current = n;
for (int i = 0; i < phrase.size(); i++) {
Node temp = *new Node(phrase[i]);
current->children.push_back(temp);
current = &current->children.back();
if (i == phrase.size() - 1) {
current->setEOS();
current->setWeight(weight);
}
}
}
void get(vector<string> search) {
Node* current = &this->root;
get(search, current);
}
void get(vector<string> search, Node* n) {
Node* current = n;
int found = -1;
//test search size
if (search.size() == 0) {
cout << "Please enter a valid search" << endl;
}
for (int i = 0; i < current->children.size(); i++) {
if (search[0] == current->children[i].getWord()) {
found = i;
}
}
if (found > -1 && search.size() == 1) {
current = &current->children[found];
printFromNode(*current);
maxNode(*current);
}
else if (found > -1 && search.size() != 1) {
current = &current->children[found];
search.erase(search.begin());
get(search, current);
}
else {
cout << "Not Found" << endl;
}
}
void printOutput(vector<pair<int,string>> p){
sort(p.begin(), p.end());
cout << p.size() << endl;
for (int i = 0; i < p.size(); i++) {
cout << p[i].second << " " << endl;
}
}
void printFromNode(Node n) {
vector<string> phrase = {};
vector <pair < int, string>> final = {};
printFromNode(n,phrase,final);
}
void printFromNode(Node n, vector<string> &v, vector<pair<int,string>> &p) {
string output;
if (n.getWord() == "") {
return;
}
for (int i = 0; i < n.children.size(); i++) {
if (n.children[i].getWeight() > 0) {
for (int i = 0; i < v.size(); i++)
{
output.append(v[i] + " ");
}
output.append(n.children[i].getWord());
p.push_back(make_pair(n.children[i].getWeight(), output));
}
v.push_back(n.children[i].getWord());
printFromNode(n.children[i], v, p);
v.pop_back();
sort(p.begin(), p.end());
}
return;
}
void maxNode(Node n) {
int max = 0;
int index = 0;
int temp = 0;
for (int i = 0; i < n.children.size(); i++) {
temp = n.children[i].children.size();
if (temp > max) {
max = temp;
index = i;
}
}
cout << n.children[index].getWord() << " " << max << endl;
}
};
Main.cpp:
#include "Trie.cpp"
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char* argv[]) {
// Initialize trie up here
Trie myTrie = *new Trie();
// parse input lines until I find newline
for(string line; getline(cin, line) && line.compare(""); ) {
stringstream ss(line);
string string_weight;
ss >> string_weight;
int weight = stoi(string_weight);
// I am just going to put these words into a vector
// you probably want to put them in your trie
vector<string> phrase = {};
for(string word; ss >> word;) {
phrase.push_back(word);
}
myTrie.add(phrase, weight, &myTrie.root);
vector<string> ans = {};
}
// parse query line
string query;
getline(cin, query);
stringstream ss(query);
vector<string> search = {};
for (string query; ss >> query;) {
search.push_back(query);
}
myTrie.get(search);
return 0;
}
You can remove recursive methods, and doing something like the following:
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <set>
class Node
{
public:
bool endOfSentence = false;
std::set<int> weights;
std::map<std::string, Node> children;
Node() = default;
const Node* get(const std::string& word) const
{
auto it = children.find(word);
if (it == children.end()) {
return nullptr;
}
return &it->second;
}
auto find_by_weight(int weight) const
{
return std::find_if(children.begin(),
children.end(),
[=](const auto& p){ return p.second.weights.count(weight);});
}
};
class Trie
{
Node root;
public:
void add(int weight, const std::vector<std::string>& phrase)
{
Node* node = &root;
for (const auto& word : phrase) {
node->weights.insert(weight);
node = &node->children[word];
}
node->weights.insert(weight);
node->endOfSentence = true;
}
bool contains(const std::vector<std::string>& phrase) const
{
const Node* node = &root;
for (const auto& word : phrase) {
node = node->get(word);
if (node == nullptr) {
return false;
}
}
return node->endOfSentence;
}
void print(int weight) const
{
const Node* node = &root;
const char* sep = "";
while (node) {
const auto it = node->find_by_weight(weight);
if (it == node->children.end()) {
break;
}
std::cout << sep << it->first;
sep = " ";
node = &it->second;
}
std::cout << std::endl;
}
void print_all() const
{
for (int i : root.weights) {
print(i);
}
}
};
And usage/Test:
int main(int argc, char* argv[]) {
const std::vector<std::vector<std::string>> sentences = {
{"My", "name", "is", "John"},
{"My", "house", "is", "small"},
{"Hello", "world"},
{"Hello", "world", "!"}
};
Trie trie;
int i = 0;
for (const auto& sentence : sentences) {
trie.add(i, sentence);
++i;
}
const std::vector<std::vector<std::string>> queries = {
{"My", "name", "is", "John"},
{"My", "house"},
{"Hello", "world"}
};
for (const auto& query : queries) {
std::cout << trie.contains(query) << std::endl;
}
trie.print_all();
}
Demo

Trouble with nested data structure in C++

Using https://github.com/nlohmann/json, I am trying to assign values to a recursive data structure (json_node_t):
#include <iostream>
#include <string>
#include <vector>
#include "json.hpp"
using namespace std;
using json = nlohmann::json;
struct json_node_t {
int id;
std::vector<json_node_t> children;
};
void to_json(json& j, const json_node_t& node) {
j = {{"ID", node.id}};
if (!node.children.empty())
j.push_back({"children", node.children});
}
int main() {
json_node_t node_0;
std::vector<int> values = {1,2,3};
std::vector<json_node_t> parents;
parents.resize(20);
for(int i = 0; i < values.size(); i++) {
if ( i == 0 )
{
node_0.id = values[0];
std::vector<json_node_t> node_children_;
node_0.children = node_children_;
parents[0] = node_0;
} else {
json_node_t node_i;
node_i.id = values[i];
std::vector<json_node_t> node_i_children_;
parents[i] = node_i;
parents[i-1].children.push_back(node_i);
}
}
json j = node_0;
cout << j.dump(2) << endl;
return 0;
}
My purpose is to create a JSON representation like the following:
{
"ID": 1,
"children": [
{
"ID": 2
},
{
"ID": 3,
"children": []
}
]
}
However, the nested children are not getting printed. I only get this output:
{
"ID": 1
}
What is wrong? I cannot connect the child to his parent correct. How can I fix the problem?
You output node_0;, but you never append any children to it. The reason for that, is parents[0] = node_0; copies node_0. So when you parents[0].children.push_back(node_i); you append node_i as a child to the copy of node_0 - the original one remains unchanged. That is why it contains no children in the end.
EDIT:
My code.
#include "json.hpp"
#include <memory>
#include <vector>
#include <iostream>
struct json_node;
using json_node_ptr = std::shared_ptr<json_node>;
struct json_node
{
int id;
std::vector<json_node_ptr> children;
json_node(int _id)
: id{ _id }
{
}
};
void to_json(nlohmann::json& j, const json_node_ptr& node)
{
j = {{"ID", node->id}};
if (!node->children.empty()) {
j.push_back({"children", node->children});
}
}
int main()
{
std::vector<int> values = {1,2,3};
std::vector<json_node_ptr> parents;
for(int i = 0; i < values.size(); i++)
{
if ( i == 0 ) {
auto root = std::make_shared<json_node>(values[0]);
parents.push_back(root);
} else {
parents.push_back(std::make_shared<json_node>(values[i]));
parents[i-1]->children.push_back(parents[i]);
}
}
nlohmann::json j = parents[0];
std::cout << j.dump(2) << std::endl;
return 0;
}
Output:
{
"ID": 1,
"children": [
{
"ID": 2,
"children": [
{
"ID": 3
}
]
}
]
}

search for string in array of structs C++

I am trying to create a function that allows me to type a name and return that employees information found in the struct. /.......................................................................
// info (ss_number,etc..)
struct employees {
int ss_number;
int dob; //date of birth
string f_name;
string l_name;
string state;
};
void print_person(employees e)
{
cout<<e.ss_number<<" "<<e.dob<<" "e.f_name<<" "<<e.l_name<<" "<<e.state;
}
void search(employees array[])
{
string first;
string last;
cout << "Enter name"; //Example Jack Patton
cin >> first >> last;
for(int i = 0; i < 10; i++) {
if(array[i].f_name == first && array[i].l_name == last) {
print_person(array[i]);
}
}
}
void main()
{
employees array[10];
search(array);
}
*t is better to place the request for the name outside the function. The function should do only the search.
It could be defined the following way
const employee * search( const employees array[], size_t n,
const std::pair<std::string, std::string> &name )
{
const employees *first = array;
while ( first != array + n &&
!( first->f_name == name.first && first->l_name == name.second ) )
{
++first;
}
return first;
}
And in main it could be called the following way (take into account that main shall have return type int):
int main()
{
const size_t N = 10;
employees array[N];
// filling the array
std::pair<std::string, std::string> name;
cout << "Enter name"; //Example Jack Patton
cin >> name.first >> name.second;
const employees *emp = search( array, N, name );
if ( emp != array + N )
{
print_person( *emp );
}
//...
}
Or you could use standard algorithm std::find_if with a predicate.
At the same time function print_person should be defined as
void print_person( const employees &e)
{
cout<<e.ss_number<<" "<<e.dob<<" "e.f_name<<" "<<e.l_name<<" "<<e.state;
}
What you have is mostly fine except that you never initialized any values for your array.
You could read them in from a file, but in the meantime write this and it will work (thanks M2tM for the data):
employees array[10] =
{ { 100, 1985, "John", "Edwards", "AB" }
, { 101, 1960, "Mike", "Thomas", "CA" }
/* fill in more rows if you want */
};
Other issues: in print_person you are missing an << before e.f_name ; and void main() should be int main().
This requires C++11 and fulfills your requirements. Notice I switched to vector (since you're using string you are using C++), and am using a built in algorithm with a lambda. C++ has a lot of built in functionality in the standard libraries to make this sort of thing much easier.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Employee
{
int socialSecurtyNumber;
int yearOfBirth;
string firstName;
string lastName;
string state;
};
vector<Employee>::iterator search(vector<Employee> &employees, const string &first, const string &last)
{
return find_if(begin(employees), end(employees), [&](const Employee& employee){
return employee.firstName == first && employee.lastName == last;
});
}
int main()
{
vector<Employee> employees{
{ 100, 1985, "John", "Edwards", "AB" },
{ 101, 1960, "Mike", "Thomas", "CA" },
{ 102, 1992, "George", "Flunky", "FL" },
{ 103, 1983, "Tweet", "Johnson", "AB" },
{ 104, 1968, "Michael", "Jordan", "OH" },
};
string first;
string last;
cout << "Enter name: "; //Example Jack Patton
cin >> first >> last;
auto employee = search(employees, first, last);
if (employee != end(employees)){
cout << "FOUND! SSN: " << employee->socialSecurtyNumber << " YOB: " << employee->yearOfBirth << endl;
}else{
cout << "NOT FOUND!" << endl;
}
}