Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Created July 22, 2015 12:47
Show Gist options
  • Save krysseltillada/bcf89598fe617ddaefb3 to your computer and use it in GitHub Desktop.
Save krysseltillada/bcf89598fe617ddaefb3 to your computer and use it in GitHub Desktop.
voting program using std::vector
#include <iostream>
#include <vector>
struct person
{
std::string name;
int votes = 0;
};
int main ()
{
person p1;
std::vector <person> v1, sorted_v1;
std::string temp_name;
int temp_votes = 0, counter = 0;
while (std::cin >> temp_name) {
std::cin >> temp_votes;
p1.name = temp_name;
p1.votes = temp_votes;
v1.push_back (p1);
}
for (unsigned i = 0; i != v1.size(); ++i) {
for (unsigned ii = (counter += 1); ii != v1.size(); ++ii) {
if (v1[i].votes < v1[ii].votes) {
p1.votes = v1[i].votes;
p1.name = v1[i].name;
v1[i].votes = v1[ii].votes;
v1[i].name = v1[ii].name;
v1[ii].votes = p1.votes;
v1[ii].name = p1.name;
}
}
}
for (unsigned i = 0; i != v1.size(); ++i)
std::cout << "name: " << v1[i].name << " votes: " << v1[i].votes << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment