#Need help with the Election Day exercise in C++

12 messages ยท Page 1 of 1 (latest)

compact vector
#

This is my code:

#include <string>
#include <vector>

namespace election {

// The election result struct is already created for you:

struct ElectionResult {
    // Name of the candidate
    std::string name{};
    // Number of votes the candidate has
    int votes{};
};

// TODO: Task 1
// vote_count takes a reference to an `ElectionResult` as an argument and will
// return the number of votes in the `ElectionResult.
 int vote_count(ElectionResult& electionResult){
    return electionResult.votes;
 }


// TODO: Task 2
// increment_vote_count takes a reference to an `ElectionResult` as an argument
// and a number of votes (int), and will increment the `ElectionResult` by that
// number of votes.
void increment_vote_count(ElectionResult& electionResult, int moreVotes){
    for (int i = 0; i < moreVotes; i++){
        electionResult.votes ++;
    }    
}

// TODO: Task 3
// determine_result receives the reference to a final_count and returns a
// reference to the `ElectionResult` of the new president. It also changes the
// name of the winner by prefixing it with "President". The final count is given
// in the form of a `reference` to `std::vector<ElectionResult>`, a vector with
// `ElectionResults` of all the participating candidates.
    ElectionResult& determine_result(std::vector<ElectionResult>& finalCount){
        if(finalCount[0].votes > finalCount[1].votes){
            finalCount[0].name = "President " + finalCount[0].name;
            return finalCount[0];
        }else{
            finalCount[1].name = "President " + finalCount[1].name;
            return finalCount[1];
        }
    }

}  // namespace election

This is the error I am getting:
make[2]: *** [CMakeFiles/test_election-day.dir/build.make:70: CMakeFiles/test_election-day] Error 3
make[1]: *** [CMakeFiles/Makefile2:111: CMakeFiles/test_election-day.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

polar dust
#

Your post is hard to read, can you use the three backticks to frame the code? It highlights your functions as well.

thin oakBOT
#

To make code easier for everyone to read (and to make you look like a pro), **please use a codeblock when sharing code.**For example, you can type the following:

```
for number in range(10):
total += number;
```
Discord will render that as so:

for number in range(10):
    total += number;

You can even get syntax highlighting by adding the language to the codeblock opening:
```python
for number in range(10):
total += number;
```
Discord will render that as so:

for number in range(10):
    total += number;
polar dust
#

In task 3: It seems like you only compare the first two candidates of the results. What happens, if there is an empty list, a list with only one candidate, or one with 1234?

#

Thanks for formatting. If you add cpp after the backticks you even get code highlighting ๐Ÿ™‚

compact vector
#

Ok. I will try to add that and submit it again. Thanks a lot

polar dust
#

My pleasure, keep going ๐Ÿ™‚

#

When you are done, you can try for a code review to get more feedback

compact vector
#

Now I don't have a problem with submitting but with the actual task. I got stuck at the last test. My code looks like this:

// TODO: Task 3
// determine_result receives the reference to a final_count and returns a
// reference to the `ElectionResult` of the new president. It also changes the
// name of the winner by prefixing it with "President". The final count is given
// in the form of a `reference` to `std::vector<ElectionResult>`, a vector with
// `ElectionResults` of all the participating candidates.
    ElectionResult& determine_result(std::vector<ElectionResult>& finalCount){
        int size = finalCount.size();
        ElectionResult& winner = finalCount[0];

        for(int i = 0; i < size; i++){
            if(winner.votes < finalCount[i].votes){
                winner = finalCount[i];
            }
        }

        winner.name = "President " + winner.name;
        return winner;
    }
```cpp
I don't know how to not change the original
polar dust
#

There are several options. Instead of keeping a reference to the winner, you could also save the index of the person with the highest votes and work with that

compact vector
#

I can do that like this:

int size = finalCount.size();
        int index = 0;

        for(int i = 1; i < size; i++){
            if(finalCount[index].votes < finalCount[i].votes){
                index = i;
            }
        }

but then I am not quite sure what to do next. I still don't quite get how change the name of the winner by prefixing it with "President" but also have the name stay the same

polar dust
#

The name can change but the other candidates must not