Created
July 22, 2015 12:47
-
-
Save krysseltillada/bcf89598fe617ddaefb3 to your computer and use it in GitHub Desktop.
voting program using std::vector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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