Need Help (Election program without array)

This is what ive made so far i cant add the total votes and display the names tho :(
#include <iostream>
#include <list>
#include <string>
using namespace std;

main()

{
string name="";
float x=1, z=1, proc=0, totalVotes=0,total=0,i,a;
int votes, totalv, candi, totall,ltotal=0;
cout<<"How many cadidates for election?: ";
cin>>proc;
totalVotes+=votes;
a++;

for (int x=1; x<=proc; x++)
{
votes=0;
cout<<"\nEnter candidate last name: ";
cin>>name;
string name;
getline(cin,name);
cout<<"\nEnter votes received: ";
cin>>votes;
totalVotes+=votes;


}

cout<<"\nCandidate"<<"\tVotes Received"<<"\t\t% of Total Votes"<<endl;
totalVotes+=votes;
for (int i=1; i<=proc; i++)
{
cout<<name<<"\t\t"<<votes<<"\t\t\t\t"<<votes/totalVotes*100.00<<"%"<<endl;
}
cout<<"Total"<<"\t\t"<<totalVotes<<"\t\t\t\t"<<"100%"<<endl;

int winnerPos = 0,winnerVotes = votes=0;
for (i=1;i<=x;i++)
{
{

winnerPos = totalVotes;
winnerVotes = votes;

cout<<"The winner of the election is :"<<name;
}
}






return 0;
}
Can anyone help me? Please
Last edited on
First of all, please use code tags.
It will be easier to read and we have line numbers:
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
#include <iostream>
#include <list>
#include <string>
using namespace std;

main()

{
	string name = "";
	float x = 1, z = 1, proc = 0, totalVotes = 0, total = 0, i, a;
	int votes, totalv, candi, totall, ltotal = 0;
	cout << "How many cadidates for election?: ";
	cin >> proc;
	totalVotes += votes;
	a++;

	for (int x = 1; x <= proc; x++)
	{
		votes = 0;
		cout << "\nEnter candidate last name: ";
		cin >> name;
		string name;
		getline(cin, name);
		cout << "\nEnter votes received: ";
		cin >> votes;
		totalVotes += votes;


	}

	cout << "\nCandidate" << "\tVotes Received" << "\t\t% of Total Votes" << endl;
	totalVotes += votes;
	for (int i = 1; i <= proc; i++)
	{
		cout << name << "\t\t" << votes << "\t\t\t\t" << votes / totalVotes*100.00 << "%" << endl;
	}
	cout << "Total" << "\t\t" << totalVotes << "\t\t\t\t" << "100%" << endl;

	int winnerPos = 0, winnerVotes = votes = 0;
	for (i = 1; i <= x; i++)
	{
		{

			winnerPos = totalVotes;
			winnerVotes = votes;

			cout << "The winner of the election is :" << name;
		}
	}






	return 0;
}


Then, please tell us what issue you have.
Don't you receive errors and/or warnings from the compiler?

Finally, some hints:
- Line 6 should be int main()
- You should initialize variables:
1
2
	float x = 1, z = 1, proc = 0, totalVotes = 0, total = 0, i = 0, a = 0;
	int votes = 0, totalv = 0, candi = 0, totall = 0, ltotal = 0;

- What are you trying to do in line 14 totalVotes += votes; ?
- You have two string name variables. Which variable do you want to use? I think neither or them, you need a vector of strings instead. So, remove line 9 and add std::vector<std::string> names(proc); before line 17 (you need to #include <vector> .
- Same thing with variable votes, you need std::vector<int> votes(proc); instead
- Generally, I don't use cin, but I learned that after each cin you should clear it, using the following two lines:
1
2
	std::cin.clear();
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

- Replace lines 21 to 23 with cin >> names[x - 1];
And so on....

I rewrited it in this way:
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
#include <iostream>
#include <string>
#include <vector>

int main()
{
	int number_of_candidates = 0;
	std::cout << "How many cadidates for election?: ";
	std::cin >> number_of_candidates;
	std::cin.clear();
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

	std::vector<std::string> names(number_of_candidates);
	std::vector<int> votes(number_of_candidates);
	int totalVotes = 0;
	for (int i = 0; i < number_of_candidates; ++i)
	{
		std::cout << "\nEnter candidate last name: ";
		std::cin >> names[i];
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		std::cout << "\nEnter votes received: ";
		std::cin >> votes[i];
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		totalVotes += votes[i];
	}

	// shows votes
	std::cout << "\nCandidate\tVotes Received\t\t% of Total Votes" << std::endl;
	for (int i = 0; i < number_of_candidates; ++i)
	{
		std::cout << names[i] << "\t\t" << votes[i] << "\t\t\t\t" << static_cast<double>(votes[i]) / static_cast<double>(totalVotes) * 100.0 << "%" << std::endl;
	}
	std::cout << "Total\t\t" << totalVotes << "\t\t\t\t100%" << std::endl;

	// show winner
	int winnerPos = 0;
	for (int i = 1; i < number_of_candidates; ++i)
	{
		if (votes[i] > votes[winnerPos])
		{
			winnerPos = i;
		}
	}
	std::cout << "The winner of the election is: " << names[winnerPos];

	return 0;
}


You could use function from <algorithm> and lambdas, but this is a start.
Last edited on
Hello, i cant use arrays tho. Just loop and if...else
I don't see arrays, perhaps you refer to vectors?
You could use deque, list or whatsoever other container.
Otherwise, I don't think it's feasible.
Let's see:
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
#include <iostream>
#include <string>
#include <sstream>

int main()
{
	int number_of_candidates = 0;
	std::cout << "How many cadidates for election?: ";
	std::cin >> number_of_candidates;
	std::cin.clear();
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

	std::ostringstream output_votes;
	output_votes << "\nCandidate\tVotes Received\t\t% of Total Votes" << std::endl;

	int totalVotes = 0;
	std::string winnerName;
	int winnerVotes = 0;
	for (int i = 0; i < number_of_candidates; ++i)
	{
		std::string name;
		int votes = 0;
		std::cout << "\nEnter candidate last name: ";
		std::cin >> name;
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		std::cout << "\nEnter votes received: ";
		std::cin >> votes;
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		totalVotes += votes;

		output_votes << name << "\t\t" << votes << "\t\t\t\t" << static_cast<double>(votes) / static_cast<double>(totalVotes) * 100.0 << "%" << std::endl;

		if ((i == 0) || (votes > winnerVotes))
		{
			winnerName = name;
			winnerVotes = votes;
		}
	}

	// shows votes
	std::cout << output_votes.str() << "Total\t\t" << totalVotes << "\t\t\t\t100%" << std::endl;

	// show winner
	std::cout << "The winner of the election is: " << winnerName;

	return 0;
}

but in this way "% of Total Votes" works only for the last candidate...
Topic archived. No new replies allowed.