Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Candidate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "Candidate.h"
#include <iostream>
#include <string>
using namespace std;

Candidate::Candidate(string cand, int votes)
{
candName = cand;
numVotes = votes;
}

//sets candidate name
void Candidate::setCandName(string cand)
{
candName = cand;
}

//gets candidate name
string Candidate::getCandName()
{
return candName;
}

//sets number of votes
void Candidate::setNumVotes(int votes)
{
numVotes = votes;
}

//gets number of votes
int Candidate::getNumVotes()
{
return numVotes;
}
25 changes: 25 additions & 0 deletions Candidate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef CANDIDATE_H
#define CANDIDATE_H
#include <string>

class Candidate
{
private:
std::string candName;
int numVotes;

public:
//default constructor
Candidate()
{
candName = " ";
numVotes = 0;
}

Candidate(std::string, int);
std::string getCandName();
int getNumVotes();
void setCandName(std::string cand);
void setNumVotes(int votes);
};
#endif
70 changes: 70 additions & 0 deletions client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// CLIENT FUNCTIONS

#include "Candidate.h"
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void addVector(vector<Candidate>&);
void printVector(vector<Candidate> &);

int linearSearch(vector <Candidate> &newCandidate, auto key);//prototype

int linearSearch(vector <Candidate> &newCandidate, auto key)
{
for (int i=0; i < newCandidate.size(); i++)
{
if (newCandidate[i] == key)
{
return i;
}
}
return -1;
}

int main()
{
auto k;

vector <Candidate> newCandidate; // vector declaration

linearSearch(&newCandidate, k);
addVector(newCandidate);
printVector(newCandidate);

system("pause");

return 0;
}

// add objects to vector
void addVector(vector <Candidate> &newCandidate)
{
std::string cand;
int votes;

for (int i = 0; i < 4; i++)
{
cout << "Enter Candidate's name: ";
std::getline(cin>>ws,cand);

cout << "Enter number of votes: ";
cin >> votes;

newCandidate.push_back(Candidate(cand, votes));

cout << endl;
}
}

//print contents of vector
void printVector(vector <Candidate> &newCandidate)
{
for (size_t i = 0; i < newCandidate.size(); i++)
{
cout << "Candidate Name: " << newCandidate[i].getCandName() << endl;
cout << "Number of Votes: " << newCandidate[i].getNumVotes() << endl;
}
}