need help with a dynamic array

Hidy ho all,

I'm working on a dynamic array program. very simple, and I think I just about have it. The single issue I seem to be having is asking the user to input a string.

Heres the code:

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
#include "functions.h"

void votes()
{
	int control = 0, tvotes = 0;

	cout << endl << "How many people are participating: ";
	cin >> control;
	cout << endl;

	string *names = new string[control];
	int *votes = new int[control];

	for (int i = 0; i <= control; i++)
	{
		cout << "...........Enter name: ";
		cin >> names[i];
		system("cls");
	}

	for (int j = 0; j <= control; j++)
	{
		cout << "Enter Number of Votes: ";
		cin >> votes[j];
		system("cls");
	}

	delete[]names;
	names = NULL;

	delete[]votes;
	votes = NULL;
}


The error I'm getting when I compile it is: "binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)" when I try to input the name. Whats wrong with what I'm trying to do? Do I need a string variable in between?
Is "string" in your code the real std::string or is it your own?
no, that's supposed to be the string variable. for a grouping of letters, presumably a name.

Edit:
oh I see, I didn't include the string up top.
Last edited on
I'm talking about line 11
Ok, now I need some help with the math. Math has never been my strong suit.

Basically, I'm adding up all the votes, then trying to figure out what percentage of the votes each person got.

I went ahead and added tvotes = tvotes + votes[j]; to my second for loop, so it'll add up all the votes.

now I need to figure out how to get the program to recognize tvotes as 100%, then make each persons vote whatever percentage of 100% they got.

thats my biggest issue.
Portion
--------- * 100%
Total
oh nice, thanks. so that would be something like:

(votes[j]/tvotes)*.1

or would it be:

(votes[j]/tvotes)*100

and line 11, if I'm understanding my coding correctly, thats supposed to be a creating a pointer, then pointing said pointer to the mem location of the temporary array.
Last edited on
You would definitely multiply by 100.0 and not .1 to convert to percent.


LB wrote:
Is "string" in your code the real std::string or is it your own?
LB wrote:
I'm talking about line 11
mattig89ch wrote:
and line 11, if I'm understanding my coding correctly, thats supposed to be a creating a pointer, then pointing said pointer to the mem location of the temporary array.
That's not what I was asking, but it seems like you solved the problem.
Ok, heres what I'm working with right now:

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
#include "functions.h"
#include <string>

void votes()
{
	int control = 0, tvotes = 0;
	double average = 0.00;

	cout << endl << "How many people are participating: ";
	cin >> control;
	cout << endl;

	string *names = new string[control];
	int *votes = new int[control];

	for (int i = 0; i <= control; i++)
	{
		cout << "...........Enter name: ";
		cin >> names[i];

		cout << "Enter Number of Votes: ";
		cin >> votes[i];

		tvotes = tvotes + votes[i];
	}

	system("cls");

	for (int j = 0; j <= control; j++)
	{

		average = (votes[j] / tvotes) * 100;
		cout << "Candidate Name" << "Vote Number" << "Percentage of Votes" << endl;
		cout << names[j] << votes[j] << average << endl;
		average = 0.00;
	}

	delete[]names;
	names = NULL;

	delete[]votes;
	votes = NULL;
}


I'm not quite sure exactly what happened, but I got an interesting error. First, it runs. My main function is calling this function and thats it.

I told it I wanted to enter 2 people. It entered those just fine. But it prompted me for a 3rd entry, after I entered it, it said there was an access error. First time playing with dynamic arrays, so I'm not sure where my issue is.

Also, this window popped up to the right of my files. Its alot of codes that doesn't seem to make a whole lot of sense to me, but it highlighted this line:

1
2
3
4
static void __CLRCALL_OR_CDECL assign(_Elem& _Left, const _Elem& _Right) _NOEXCEPT
		{	// assign an element
		_Left = _Right;
		}


Does anyone know what happened?
after running it with a debugger, it seems that it doesn't exit the for loop. trouble is votes[i] is a pointer, so I can't read whats in there. hovering over it only gives me a memory address.

man, this would be so much easier without the dynamic array. But my teacher said I needed a dynamic array in this homework. So that's what I'm doing.
lines 16 and 29: You want the termination condition to be i<control and j<control If control is 10, 0-10 is actually 11 occurrances.
ok, fixed. same issue though. little error window pops up and says...huh, thats odd. I was going to copy and paste the window, but it just worked now. It was saying an error when I tried last time.

ok then, well, it just ran another 3 times. wierd.

so anywho, now its looking like that math is wrong.

when I entered 1 person had 21, and another had 19 votes, its saying they both had 0 for a percentage. Was LB wrong last night?
If you're dividing an integer by an integer, your result will be integer division. votes[j]/tvotes may be giving you 0.

average = (votes[j] / tvotes) * 100;
oh, average and int? Or tvotes and votes doubles?

Either/or?
Actually, I just tried swapping tvotes to a double, and it seems to work fine now. Spits out a percentage that looks good.

Now my single issue, is to try and find a way to compare and spit out who won this election.

I'm thinking of creating another for loop, and putting an if statement in there.

something like

if votes[k] > int variable

int variable = votes[k]
string variable = names[k]

would something like this work? or is there an easier way to do this?
As long as you don't have two integer values in the division. If I were going to redefine a variable I'd probably choose tvotes instead of the array. Or you can cast the variable to a double in the division and that should work as well.

average = (votes/static_cast<double>(tvotes)) * 100;

Last edited on
As they're initially entering the number of votes in the first for loop, you could keep track of the index number of the largest entered so far.
that works! I even managed to incorporate it into the j for loop.

Woo!

One last question, I now want to pretty it up.

How do I get it to display in columns? I know theres a way, but its been years since I tried, so I wouldn't know where to start looking for that.

edit:
oh, didn't see your reply wild blue. I guess I could track it in there. Might be a better idea in fact.
Last edited on
You can use the escape sequence "\t" to put a tab between output. Or if you go into <iomanip> you can find things like setw to set the width of columns, justify things to the left or right etc.
Topic archived. No new replies allowed.