Array of Structure

Can you help me to get with this output?


Enter No. of candidates :3

Enter Name of candidate 1:obama
Enter Name of candidate 2:bush
Enter Name of candidate 3:clinton

Enter No. of Precinct(s) :3

            Precinct1     Precinct2     Precinct3     Total
1. Obama      100            203           504         807
2. Bush       600            900            10         1510
3. Clinton    567            123            90         780

Bush Won with 1510 votes


Last edited on
reading in stuff:
http://www.cplusplus.com/doc/tutorial/basic_io/

Should get you started.
when reading in the candidates look at the 'cin and strings' section.

Generating your random numbers:
http://www.cplusplus.com/reference/cstdlib/rand/

Will there always be 3 precincts?
Last edited on
I have started can you help e with output?
something like this maybe, but you need to sort out the tabbing in your output and add some error checking on the user input.

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
#include <iostream>
#include <string>
#include <vector>

class CandidateInfo
{
private:
	std::string m_strName;
	std::vector<int> m_votes;

public:
	std::string GetName(){return m_strName;}
	std::vector<int>& GetVotes(){return m_votes;}

	void SetName(std::string name)
	{
		m_strName = name;
	}

	void SetVotes(std::vector<int>& votes)
	{
		m_votes = votes;
	}
};

// do not keep gloabl. perhaps have CandidateInfoCollection class?
void Display(std::vector<CandidateInfo>& lst, int& maxVotes, std::string& maxPres)
{
	std::vector<CandidateInfo>::iterator it;

	int candNum(1);
	maxVotes = -1;
	maxPres.clear();

	for(it = lst.begin();it != lst.end();it++)
	{
		std::cout << candNum << ". " << it->GetName() << "\t";
		
		std::vector<int>& lstVotes = it->GetVotes();

		std::vector<int>::iterator voteIt;
		for(voteIt = lstVotes.begin();voteIt != lstVotes.end();voteIt++)
		{
			if (*voteIt > maxVotes)
			{
				maxVotes = *voteIt;
				maxPres = it->GetName();
			}

			std::cout << "\t\t" << *voteIt;
		}

		std::cout << "\n";

		candNum++;
	}

}

int main()
{
	std::cout << "Number candidates: ";
	int nCandidates;
	std::cin >> nCandidates;

	std::cout << "Number precincts: ";
	int nPrecincts;
	std::cin >> nPrecincts;

	std::vector<std::string> lstPresidents;	
	std::string strPres;

	for(int j = 0; j < nCandidates;++j)
	{
		std::cout << "President Name?";
		std::cin >> strPres;

		lstPresidents.push_back(strPres);	
	} 		


	std::vector<CandidateInfo> lstCandidateInfo;
	std::vector<std::string>::iterator iter;

	for(iter=lstPresidents.begin();iter!=lstPresidents.end();iter++)
	{
		CandidateInfo info;
		info.SetName(*iter);

		std::vector<int> lstVotes;
		for(int i=0; i<nPrecincts; ++i)
		{
			// I've just used a constant '3' here, but this is where you
			// should generate your random number of votes
			lstVotes.push_back(3);
		}

		info.SetVotes(lstVotes);

		// now add to the collection
		lstCandidateInfo.push_back(info);
	}


	std::string pres;
	int votes;
	Display(lstCandidateInfo, votes, pres);

	std::cout << pres << " won with " << votes << " votes.\n";

	return 0;
}


edit: added a horrible way to get max number of votes for the candidate, but you kinda get the idea.
Last edited on
pls dont use vector. i cant understand it wasnt taught yet im sorry if im kinda dumb
pls dont use vector. i cant understand it wasnt taught yet im sorry if im kinda dumb
You're not dumb for not knowing stuff mate, especially if you haven't been taught it.
I havent got time to change it this evening sorry, but try and follow the logic and replace the vectors with arrays.
Here is my new code. Im having a trouble with the total. It just compute the last row

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
#include <iostream>
#include <ctime>
#include <string>
#include <conio.h>
using namespace std;

int numcand;
int numprct;
int row, col;
char candname [5][20];
int table[10][10];
int total[10];


int main ()
{

	system("COLOR 09");
	//******************************************************************
	//******************************************************************
	//*********************GETTING THOSE INPUTS NEEDED******************
	//******************************************************************
	//******************************************************************


	//Accepts Number of Candidates
	cout<<"\nEnter number of Candidates for the Election [1-10]: ";
	cin>>numcand;
	cin.ignore();

    //Test if no. of candidates is <10 && >1
	while ((numcand<1 && numcand!=0) || numcand>10)
	{
		system("cls");
		cout<<"\nPlease just enter form 1-10";
		cout<<"\nEnter number of Candidates for the Election [1-10]: ";
		cin>>numcand;
		system("cls");
	}

	if (numcand == 0)
	{
		cout<<"\nNo Voting Will Happen!"<<endl;
		return 0;
	}
	
	//Accepts the Name of the Candidates
	for (int ctr=1; ctr<=numcand; ctr++)
	{
		cout<<"\nEnter the Name of Candidate Number "<<ctr<<" : ";
		cin.getline(candname[ctr],20);
	}
	
	//Accepts Number of Precincts
	cout<<"\nPlease enter number of precincts : ";
	cin>>numprct;

	system("cls");

	//******************************************************************
	//******************************************************************
	//**********************PROCESS FOR OUTPUT**************************
	//******************************************************************
	//******************************************************************
	
	srand(time(NULL));

	//Display Precinct Nos
	cout<<"\t\t";
	for (ctr=1; ctr<=numprct; ctr ++)
	{
		cout<<"\tP"<<ctr<<"";
	}

	//Display Name of Candidates
	for (ctr=1; ctr<=numcand; ctr++)
	{
		cout<<"\n\n"<<candname[ctr];

		//Display Random Numbers
		cout<<"\t";
		for (row=0; row<1; row++)
		{
			for (col=0; col<numprct; col++)
				{
					table[row][col] = rand()%999;
					cout<<"\t"<<table[row][col];
				}
			cout<<"\n\n";
		}
	}

	for (row=0; row<numcand; row++)
	{
		total[row] = 0;
		for (col=0; col<numprct; col++)
		{
			total[row] += table[row][col];
		}
	}

	for(row=0; row<numcand; row++)
        {
            cout<< "\nTotal Votes for Candidacy of " <<(candname[row+1])<<" : " << total[row];
        }

	getch();
	return 0;	

}
Last edited on
Topic archived. No new replies allowed.