loading
open in
main.cpp
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
/*
 Problem: Write a program that allows the user to enter the last names of five candidates in
 a local election and the number of votes received by each candidate. The
 program should then output each candidate’s name, the number of votes
 received, and the percentage of the total votes received by the candidate. Your
 program should also output the winner of the election.
 */

//////////////////////////////////////////


#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <fstream>
using namespace std;



//Function prototype
void electionValues(string nombre[], int votos[]);
void winner(string name[], int votes[], int counts[]);
int total(string nam[], int nums[]);

int gTotal;

//Main Function
int main() {
    
    //Description Header
    cout << "================================================" << endl;
    cout << "Description: Candidate Project" << endl;
    cout << "School: Long Beach City College" << endl;
    cout << "Author: Christian Araya" << endl;
    cout << "Date: April 29th 2018" << endl;
    cout << "Program: CArayaProject7" << endl;
    cout << "================================================\n\n\n" << endl;
    
    
    
    //Declarations & Initializations
    int tally[5] = {0};
    string names[5] = {""};
    
    // Gen Format
    cout << fixed << showpoint << setprecision(2);
    
    
    //Output
    cout << setw(15) << left << "Candidate"
         << setw(15) << right << "Votes Received"
         << setw(25) << right << "% of Total Votes\n\n";
    
    //Process
    //CALL FUNCTION that will calculate total and assign it to some global variable so that the following function electionValues can have the correct value of the total
    total(names, tally);
    electionValues(names, tally);
    cout << endl << endl << "The Winner of the Election is: ";
    winner(names, tally, tally);
    cout << endl << endl << endl;
    return 0;
}



//Function Definitions
void electionValues(string nombre[], int votos[])
{
    setprecision(2);
    int i = 0;
    int sum = 0;
    ifstream inFile; //Declare input variable
    inFile.open("input.txt"); // open file
    while (!inFile.eof())
    {
        inFile >> nombre[i] >> votos[i];
        cout << setw(10) << left << nombre[i]
             << setw(15) << right << votos[i]
             << setw(22) << right << (static_cast<double>(votos[i])) / gTotal * 100 << endl;
        ++i;
    }
    for (int j = 0; j <= 4; j++)
    {
        sum = sum + votos[j];
    }
    cout << setw(10) << left << "Total"
         << setw(15) << right << sum;
    
    inFile.close(); // close file
}

void winner(string name[], int votes[], int counts[])
{
    int e = 0;
    int champTally;
    ifstream input;
    input.open("input.txt");
    while (!input.eof())
    {
        input >> name[e] >> votes[e];
    }
    int max = 0;
    string champName;
    for (int k = 0; k < 4; k++)
    {
        if (votes[max] < votes[k])
            max = k;
    }
    champTally = votes[max];
    champName = name[max];
    cout << champName;
    
    input.close(); // close file
}

int total(string nam[], int nums[])
{
    int i = 0;
    int suma = 0;
    ifstream in; //Declare input variable
    in.open("input.txt"); // open file
    while (!in.eof())
    {
        in >> nam[i] >> nums[i];
        ++i;
    }
    for (int j = 0; j <= 4; j++)
    {
        suma = suma + nums[j];
    }
    gTotal = suma;
    return suma;

    in.close(); // close file

}
gcc version 4.6.3