Answer to Question #243375 in C++ for Ritik

Question #243375

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). The program should start with a program title and brief instructions on how to vote. It should then ask the user to select an item (vote) from a group of items that are presented. You should use your own choice of holiday destinations


1
Expert's answer
2021-09-28T05:08:44-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS