Hi everyone,
I'm trying to write a program that inputs candidate names and vote tallies from the user and outputs the name, tally, and percentage for each. It compiles, but when it gets past the for loops, I get an error message saying the "Debug assertion failed! Expression: string subscript out of range."
I'm very new to programming, so I don't know what this means or how to fix it.
I also have to press enter twice after entering the first name before the second prompt appears. And it's ignoring all of my endl statements. Thanks for any help you can give me.
Here's my code:
Thanks for your help! My program works as it should now and my code looks better too.
I left out one thing, though. I also need it to output the winner of the election. I know how to find the largest element in the votes array, but how do I output the name associated with that element?
You could create another variable, lets call int winner. ( Or you could just re-use variable j. Where you see winner, substitute a j ) As your stepping through the for loop, and find a high value, have winner, ( or j ) equal the variable in the loop. Like so...
1 2 3 4 5 6 7 8 9 10
int winner,high=votes[0];
for (i=0;i<5;i++)
{
if( votes[i] > high)
{
high = votes[i];
winner = i;
}
}
cout << "The winner of this election was " << candidate[winner] << " with " << votes[winner] << " votes." << endl;