Here's my main code:
// Update vote totals given a new vote
bool vote(string name, int candidatecount, candidate candidates1[MAX])
{
// TODO
for (int a = 0; a < candidatecount; a++)
{
if (candidatecount == a)
{
printf("a\n");
return false;
}
if (name == candidates1[a].names)
{
printf("b\n");
candidates[a].votes = candidates[a].votes + 1;
}
printf("%s, %s\n", candidates1[a].names, name);
}
printf("1\n");
return(name);
}
This was my command line:
./plurality R D
The output (with my responses) was this:
Number of voters: 3
Vote: R
R, R
D, R
1
Vote: D
R, D
D, D
1
Vote: R
R, R
D, R
1
vote candidate 0 go around 0
b 0 e (null)
vote candidate 0 go around 1
b 0 e (null)
(null) won with 0 votes.
It makes it seem like this:
if (name == candidates1[a].names)
should work, but it doesn't, because the printf I have after it doesn't work. Does anyone know why?
Review the Week 3 video, starting around the 40 minute mark. The prof says
It turns out that in C, you can't use equals equals to compare two strings.
He introduces strcmp
at that time, and will give further explanation about why in the next lesson. The simple explanation is that strings are pointers and ==
will compare the addresses of the operands, not the values.