1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
//Democracy.cpp
//Voting program
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
using std::string;
void vote();
void results(string candidate_1,string candidate_2,string candidate_3);
int winner(int array_results[],int size);
int
candidate_counter_1=0,
candidate_counter_2=0,
candidate_counter_3=0; //global variables cuz these mother f.k.rs.. are everywhere!
int main(){
bool isVoting=true;
char option;
string
candidate_1="Pikachu",
candidate_2="Son Goku",
candidate_3="Homer Simpson"; //local variables to simulate democracy..
while(isVoting){
cout<<"PRESIDENTIAL ELECTIONS\n\n";
cout<<"Candidates:\n";
cout<<"1.-"<<candidate_1
<<"\n2.-"<<candidate_2
<<"\n3.-"<<candidate_3<<endl;
cout<<"Enter <v> vote, <r> results, <q> quit\n";
cin>>option;
while(option!='v'&&option!='V'&&option!='r'&&option!='R'&&option!='Q'&&option!='q'){
cout<<"\nEnter <v> vote, <r> results, <q> quit\n";
cin>>option;
}//end inner while
if(option=='v'||option=='V'){
vote();
}else if(option=='r'||option=='R'){
results(candidate_1,candidate_2,candidate_3);
}else if(option=='q'||option=='Q'){
isVoting=false;
}//end if..else..else
}//end while
return 0; //indicates success
}//end main
void vote(){
int election;
cout<<"\nPlease enter your election: ";
cin>>election;
while(election<1||election>3){
cout<<"\nPlease enter your election (1,2,3): ";
cin>>election;
}//end while
switch(election){
case 1:
candidate_counter_1++;
break;
case 2:
candidate_counter_2++;
break;
case 3:
candidate_counter_3++;
break;
default:
break;
}//end switch
}//end function vote
void results(string candidate_1,string candidate_2,string candidate_3){
cout<<'\n'<<candidate_1<<' '<<candidate_counter_1<<" votes\n"
<<candidate_2<<' '<<candidate_counter_2<<" votes\n"
<<candidate_3<<' '<<candidate_counter_3<<" votes"
<<endl;
cout<<"And the winner is :";
int stored_results[3]={candidate_counter_1,candidate_counter_2,candidate_counter_3};
if(winner(stored_results,3)==1){
cout<<candidate_1<<'\n'<<endl;
}else if(winner(stored_results,3)==2){
cout<<candidate_2<<'\n'<<endl;
}else if(winner(stored_results,3)==3){
cout<<candidate_3<<'\n'<<endl;
}else if(winner(stored_results,3)==4){
int highest=0;
for(int i=0;i<3;i++)
if(stored_results[i]>highest) highest=stored_results[i];
cout<<"\nTie:\n";
for(int i=0;i<3;i++){
if(stored_results[i]==highest){
if((i+1)==1){
cout<<candidate_1<<endl;
}else if((i+1)==2){
cout<<candidate_2<<endl;
}else{
cout<<candidate_3<<endl;
}//end if..else..else
}//end if
}//end for
}else{
cout<<"\nTie:\n";
cout<<candidate_1<<' '<<candidate_2<<' '<<candidate_3<<endl;
}//end if..else..else
}//end function results
int winner(int array[],int size){
int highest=0;
int theWinnerIs;
int sameScore=0;
for(int i=0;i<size;i++)
if(array[i]>highest) highest=array[i]; //store the highest scored
for(int i=0;i<size;i++){
if(array[i]==highest){
theWinnerIs=i+1;
}//end if
}//end for
for(int i=0;i<size;i++)
if(highest==array[i])sameScore++;
if(sameScore>1){
if(sameScore==2)
theWinnerIs=4;
else
theWinnerIs=5;
}//end if
return theWinnerIs;
}//end function winner
|