In need of some tips

Hello. I previously had a question regarding my program and am now stuck again in another section of it.

 
edit :D


So I need to get the name of the highest voter. I can get the highest votes, but how can I transfer that to display the string (name) rather than the int (votes)?

I'd like an explanation on how it works rather than the answer :/ Been working on this problem all day and I just can't get my head around it.

I was thinking maybe int winner would have to be a string, but I tried it and I received syntax errors. Thanks.
Last edited on
If numOfVotes[pos] contains the highest number of votes, then
candidatesLastName[pos] contains the name of the candidate with the highest number of votes.
I'm sorry JLBorges but I just can't figure it out =/ Been reading both your statement and the code over and over and over and I just can't get it... *sigh*

Can you elaborate please?
Say, the numOfVotes array contains { 23, 67, 82, 5, 56 } ;
And the candidatesLastName array contains { "Daffy", "Porky", "Sylvester", "Bugs", "Speedy" } ;

The highest number of votes, 82, is at numOfVotes[2]
Ergo, the candidate at candidatesLastName[2] - "Sylvester" - got the highest number of votes.
This section here:
1
2
3
4
5
6
for (loopControl = 0; loopControl < ARRAY_SIZE; loopControl++)
	{
		if (numOfVotes[loopControl] > highestVote)
			highestVote = numOfVotes[loopControl];
		winner = //need help here
	}//end for loop 


Already finds the highest number of votes. candidatesLastName[0] received numOfVotes[0] number of votes, which is percantageOfVotes[0] percent of votes.

By that same logic, if numOfVotes[loopControl] is the highest number of votes, the winner should be candidatesLastName[LoopControl]
Yeah I understand Sylvester would win in that situation, but my problem is I guess the syntax?

So let us use that example where Sylvester is the winner. How do I pass candidatesLastName[2] so that on line 58 it outputs the name?

After this section executes:
1
2
3
4
5
6
//find largest number in array
	for (loopControl = 0; loopControl < ARRAY_SIZE; loopControl++){
		if (numOfVotes[loopControl] > highestVote)
			highestVote = numOfVotes[loopControl];
		
	}


How do I pass that Sylvester won onto line 58 where I am supposed to output the name of candidate and not the number of votes?

I declared winner as int, but that would output a number rather than a string. So I changed that to a string so that winner takes the value of candidatesLastName[2] , or Sylvester. But that is where I am stuck.

Thanks a lot for helping me understand better and lifting some stress off me.
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
#include <iostream>
#include <string>

int main()
{
    const int ARRAY_SIZE = 5;

    std::string candidatesLastName[ARRAY_SIZE] = { "Daffy", "Porky", "Sylvester", "Bugs", "Speedy" } ;
    int numOfVotes[ARRAY_SIZE] = { 23, 67, 82, 5, 56 } ;

    int winner_position = 0 ;
    int highestVote = numOfVotes[0] ;

    for( int i = 1 ; i < ARRAY_SIZE ; ++i )
    {
        if( numOfVotes[i] > highestVote )
        {
            highestVote = numOfVotes[i] ;
            winner_position = i ;
        }
    }

    std::cout << "and the winner is: '" << candidatesLastName[winner_position]
              << "' with "  << highestVote << " votes.\n" ;
}

http://coliru.stacked-crooked.com/a/d653221ca687fd68

Or:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

int main()
{
    const int ARRAY_SIZE = 5;

    std::string candidatesLastName[ARRAY_SIZE] = { "Daffy", "Porky", "Sylvester", "Bugs", "Speedy" } ;
    int numOfVotes[ARRAY_SIZE] = { 23, 67, 82, 5, 56 } ;

    std::string winner_name = candidatesLastName[0] ;
    int highestVote = numOfVotes[0] ;

    for( int i = 1 ; i < ARRAY_SIZE ; ++i )
    {
        if( numOfVotes[i] > highestVote )
        {
            highestVote = numOfVotes[i] ;
            winner_name = candidatesLastName[i] ;
        }
    }

    std::cout << "and the winner is: '" << winner_name << "' with "  << highestVote << " votes.\n" ;
}

http://coliru.stacked-crooked.com/a/22dd2694ea68c3f3
Oh my.. dude you are FREAKING AWESOME!! It all makes sense now that I have seen your example. Line 19 was the one I couldn't think of! Damn dude THANK YOU SO MUCH! :'D

Kind of bummed out I couldn't figure it out though.. I was getting super frustrated cause I had been trying since the morning. Regardless, thank you so much man I can't say it enough. :D

I had never seen that website before. Neat page.

What book would you recommend me for C++ JLBorges? I want to IMPROVE!! I currently have one only and I'd like to get my hands on more.
Last edited on
One, or more of these:

Programming: Principles and Practice Using C++ - Stroustrup
http://www.amazon.com/dp/0321992784

C++ Primer (5th Edition) - Lippman, Lajoie, Moo
http://www.amazon.com/Primer-5th-Edition-Stanley-Lippman/dp/0321714113

Accelerated C++: Practical Programming by Example - Koenig and Moo
http://www.amazon.com/Accelerated-C-Practical-Programming-Example/dp/020170353X
Topic archived. No new replies allowed.