help! voting program.

a) Table Q2 is a list of favourite football players in the recent World cup 2014.
Write a program to collect the vote for these players and display the names of the players with top 5 highest votes.(where press any alphabet to end the choice and display the total vote.)
1 Lionel Messi (Argentina)
2 Arjen Robben(Netherland)
3 Thomas Müller (Germany)
4 James Rodríguez (Columbia)
5 Neymar da Silva Santos Júnior (Brazil)
6 Cristiano Ronaldo (Portugal)
7 Jan Vertonghen (Belgium)
8 Robin van Persie (Netherlands)
9 Miralem Pjanic (Bosnia-Herzegovina)
10 Karim Mostafa Benzema (France)


so this is my program:
#include <iostream>
#include <string>
using namespace std;
int u;
int i ;
string player[10]={"Lionel Messi","Arjen Robben","Thomas Muller","James Rodriguez","Neymar da Silva Santos Junior","Christiano Ronaldo","Jan Vertonghen","Robin van Persie","Miralem Pjanic","Karim Mostafa Benzema"};

int main()
{
for(i=0;i<10;i++)
cout<<"\t"<<i+1<<"\t\t"<<player[i]<<"\n";
do{

cout<<"Your Choice : ";
cin>>u;
if (!cin.fail())
{


while (u<1 || u>10)
{
cout<<"Invalid choice. Enter any number on the list to continue or Enter any alphabet to quit.";
cin>>u;

}}
else
break;
}while(u>=1&&u<=10);





system ("pause");
return 0;
}


i could only manage to solve until entering the choices after that im stuck. help!

Remember String arrays and normal arrays start at 0, not 1. Your menu runs from 1 - 10 and u would run past the last element of your array, you would need to subtract one from u to read the correct element.

You have two cin statements, you only really need one.

Assuming you are required to use arrays, create a second array of size 10 called votes, so at index 0 of player would be the player name, and index 0 of votes would hold the total votes for that player.

Bubble sort the two arrays in descending score order then show the first 5.

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

#include <iostream>
#include <string>
using namespace std;

int main()
{
	int option;
	string player[10] = {	
		"Lionel Messi", "Arjen Robben", "Thomas Muller", "James Rodriguez", 
		"Neymar da Silva Santos Junior", "Christiano Ronaldo", "Jan Vertonghen", 
		"Robin van Persie", "Miralem Pjanic", "Karim Mostafa Benzema" 
	};
	int votes[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

	// show options
	for (int i = 0; i < 10; i++)
		cout << "\t" << i + 1 << "\t" << player[i] << endl;

	do
	{
		// read vote, make sure we are in the menu range
		do {
			cout << endl << "\tEnter Choice (or type 11 to finish): ";
			cin >> option;
			// bit of code to stop the annoying loop if user enters
			// a letter and not the expected number range.
			cin.clear();  // clear any error flags
			cin.ignore(1, '\n');  // discard any newline left in the stream.
		} while (option < 0 || option > 11);

		// make sure we are not exiting.
		if (option < 11) {
			// store vote in array and loop for another.
			// the option value would be used to access
			// and modify the correct player.

			option--;  // arrays start at 0, not 1

		}
		
	} while (option != 11);

	// bubble sort in decending order, remember you also need to 
	// include the players in the sort so that element index's
	// remain lined up.

	// loop and show the first 5 names (the first 5 highest)

	return 0;
}
Topic archived. No new replies allowed.