This assignment requires a C++ program be written to perform a vote counting and tallying procedure to gauge favorite holiday destinations of individuals. It should present the user with the question and a “ballot” of 5 holiday destinations and an exit/quit option. Once the program is started, it should allow the user to vote multiple times (same as in multiple users using the program to vote).
For example, the voting options might look like: *********** FAVORITE HOLIDAY DESTINATION OPINION POLL ***********
1. London
2. Paris
3. Rome
4. Sydney
5. New Zealand
6. Quit voting Please choose your favorite holiday destination from the list above by number
Note that the sixth menu item is the option to quit voting. If this option is selected, voting results (as discussed in the next paragraph) need to be printed before the program terminates. Print the total number of votes, individual votes for each item, and percentages in an easy to read table. .
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Voter
{
string name;
int voice;
};
int main()
{
vector<Voter> vote;
string choise;
string name;
cout << "In this program, a vote is made for your favorite vacation spots." << "\n";
cout<<"In the voting procedure, the user enters his name and the number"<< "\n";
cout<<"of his favorite vacation spot." << "\n";
cout << "Each participant has the right to change his" << "\n";
cout << "decision before the end of the voting" << "\n";
cout << endl;
string places[5] = { "Mountain","River","Sea","Field","Forest" };
int voice;
cout << "Favorite seat list and voting ballot number" << "\n";
for (int i = 0; i < 5; ++i)
{
cout << i+1 << " - " << places[i] << "\n";
}
do
{
cout << "Enter the name of the voter: ";
cin >> name;
cout << "Enter the number of the selected voting place: ";
cin >> voice;
bool not_found = true;
for (int i =0;i<vote.size();++i)
{
if (name == vote[i].name)
{
vote[i].voice=voice;
not_found = false;
break;
}
}
if (not_found)
{
vote.push_back(Voter{ name, voice });
}
cout << "Continue (yes/no ): ";
cin >> choise;
} while (choise != "no");
int voices[6]{ 0 };
for (int i = 0; i < vote.size(); ++i)
{
if (vote[i].voice > 0 && vote[i].voice < 6)
{
voices[vote[i].voice] += 1;
}
else
{
voices[0] += 1;
}
}
cout << "Voting results:" << "\n";
cout << "Total people voted: " << vote.size() << "\n";
cout<<"Invalid votes:"<<voices[0]<< "\n";
cout<< "Votes statistics by number:" << "\n";
for (int i = 0; i < 5; i++)
{
cout << places[i] << " - " << voices[i + 1] << "\n";
}
return 0;
}
Comments
Leave a comment