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.
#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 = newint[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?
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.
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.
#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 = newint[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
staticvoid __CLRCALL_OR_CDECL assign(_Elem& _Left, const _Elem& _Right) _NOEXCEPT
{ // assign an element
_Left = _Right;
}
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.
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?
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;
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.