A Vote Tallying Program

closed account (yCf9216C)
So I'm working on a program which finds the highest number of numbers in a vector. It may be lengthy, but that's just because its easier to put different functionality in different loops and places, I've learned. Trying to make one loop do three different things is verrrryy difficult.

I don't know why when I run this, it gets to the loop which finds the ties or highest number of those, and skips the entire loop!

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
#include<vector>
#include<time.h>
#include<iostream>
using namespace std;

void stats (vector<int> MyVector ) { //CORRECT SYNTAX. No Dual Parantheses, in passing or recieving. 
	vector <int> MyTallyVector (10);
	for (unsigned int counter = 0; counter < MyVector.size(); counter += 1 ) { 
		int VectorContainer =  MyVector.at(counter); 
	switch (VectorContainer) {
	case 2:
		MyTallyVector.at(0) += 1;
		cout << "Added one to 2 Tally\n";
		break;
	case 3:
		MyTallyVector.at(1) += 1;
		cout << "Added one to 3 Tally\n";
		break;
	case 4:
		MyTallyVector.at(2) += 1;
		cout << "Added one to 4 Tally\n";
		break;
	case 5:
		MyTallyVector.at(3) += 1;
		cout << "Added one to 5 Tally\n";
		break;
	case 6:
		MyTallyVector.at(4) += 1;
		cout << "Added one to 6 Tally\n";
		break;
	case 7:
		MyTallyVector.at(5) += 1;
		cout << "Added one to 7 Tally\n";
		break;
	case 8:
		MyTallyVector.at(6) += 1;
		cout << "Added one to 8 Tally\n";
		break;
	case 9:
		MyTallyVector.at(7) += 1;
		cout << "Added one to 9 Tally\n";
		break;
		case 10:
		MyTallyVector.at(8) += 1; 
		cout << "Added one to 10 Tally\n";
		break;
	case 11:
		MyTallyVector.at(9) += 1;
		cout << "Added one to 11 Tally\n";
		break;
			case 12:
		MyTallyVector.at(10) += 1;
		cout << "Added one to 12 Tally\n";
		break;


			default:
//rather useless holdover from implementing this switch case.
			cout << "The VectorContainer did not match any range value.\n";
			break;
	}
	}

	//This loop will simply print the tallies.
		for (unsigned int c = 0; c < MyTallyVector.size(); c++ ) {
		cout << "There were: " << MyTallyVector.at(c) << " " <<  c + 2 <<  "'s \n"; 
		

		// I want this to tally the votes by replacing each element which is high, with the next highest one. If there is a tie, then the tied tallies will be stored.
	static bool TieBool = false;
	static int MaxSum = 0;
	int StoreThisOne = 0;
	int StoreThisTwo = 0;
	for (unsigned int count = 0; count < MyTallyVector.size(); count += 1) {

		if (MyTallyVector.at(0) == 0 ) { }
		
		else if (MyTallyVector.at(count) > MaxSum) { 
			MaxSum = MyTallyVector.at(count);
			TieBool = false;
			StoreThisOne = count + 2;
		}
		
		else if (MyTallyVector.at(count) == MaxSum ) { 
			TieBool = true;
			StoreThisTwo = count + 2; 
		} 
	}
	if (TieBool == true) {
		 cout << "Two ties were found: " << StoreThisOne << " " << StoreThisTwo << "\n";
	}
	else { cout << "The highest number of votes was: " << StoreThisOne << " \n";
	}
	}
int main () {
	int pause = 0;
	vector <int> MyVector (6);
	//These should really be alphabetical characters, but its easier to just use numbers right now.
	MyVector.at(0) = 3;
	MyVector.at(1) = 3;
	MyVector.at(2) = 5;
	MyVector.at(3) = 9;
	MyVector.at(4) = 9;
	MyVector.at(5) = 9;

	stats (MyVector );
	cin >> pause;
	return 0;
}
Last edited on
In your main, none of the values you set in MyVector is the value 2. So when you reach line 76, that line is always true. Presumably you meant

if (MyTallyVector.at(count) == 0 ) { }

but there's no reason you need to handle that as a special case.

Last edited on
closed account (yCf9216C)
Huh. I should have noticed that! I intended that to be redundant, but I guess it didn't work because of that! Thanks!
Topic archived. No new replies allowed.