Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++, design a program that pulls information from a .txt file and displays th

ID: 3692026 • Letter: I

Question

In C++, design a program that pulls information from a .txt file and displays the result. The program is an election in which 10 amendments and 2 candidates are being voted on. The .txt file looks like: 1YNNYYNYNYYThe Captain 2NNNNYYNYNNThe New Guy and so on for 100 voters. where the integer variable is a voter ID # and the char Y/N are the responses to the 10 amendments and the final piece (The Captain/The New Guy) is the candidate voted. There may be duplicate ID numbers, but only the first one of the duplicates to appear will be counted. All 10 amendments pass with a 2/3 majority. The results must be displayed at the end for the candidate and the 10 amendments. In C++, design a program that pulls information from a .txt file and displays the result. The program is an election in which 10 amendments and 2 candidates are being voted on. The .txt file looks like: 1YNNYYNYNYYThe Captain 2NNNNYYNYNNThe New Guy and so on for 100 voters. where the integer variable is a voter ID # and the char Y/N are the responses to the 10 amendments and the final piece (The Captain/The New Guy) is the candidate voted. There may be duplicate ID numbers, but only the first one of the duplicates to appear will be counted. All 10 amendments pass with a 2/3 majority. The results must be displayed at the end for the candidate and the 10 amendments. 1YNNYYNYNYYThe Captain 2NNNNYYNYNNThe New Guy and so on for 100 voters. where the integer variable is a voter ID # and the char Y/N are the responses to the 10 amendments and the final piece (The Captain/The New Guy) is the candidate voted. There may be duplicate ID numbers, but only the first one of the duplicates to appear will be counted. All 10 amendments pass with a 2/3 majority. The results must be displayed at the end for the candidate and the 10 amendments.

Explanation / Answer

//You have to generate a sample input file election.txt

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
ifstream fp;
fp.open ("election.txt");
int i,j,k;
int c1,c2;
int voters[101],amend[10];
char ch;
string cand;
for(i=0; i<101; i++)voters[i]=0;
c1=0,c2=0;
string str;
while(fp>>j)
{
if(voters[j]==1)
{
getline(fp,str);
continue;
}
else
{
voters[j]=1;
}
for(k=0; k<10; k++)
{
fp>>ch;
if(ch=='Y')amend[k]++;
}
getline(fp,cand);
if(cand.compare("The Captain")==0)c1++;
else c2++;
//cout<<cand<<c1<<c2<<endl;
}
for(k=0; k<10; k++)
{
if(amend[k]>66)
{
cout<<"Amendment "<<k+1<<" passed"<<endl;
}
else
{
cout<<"Amendment "<<k+1<<" did not pass"<<endl;
}
}
if(c1>c2)
{
cout<<"The Captain won the election"<<endl;
}
else
{
cout<<"The New Guy won the election"<<endl;
}
return 0;
}