String array input problem

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:

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream>
#include<cstring>
#include<string>
#include<iomanip>

using namespace std;

int main()
{
	string candidate[5];
	int votes[5];
	int i;
	int j;
	double total = 0.0;
	
	cout << fixed << setprecision(2);

	for (i = 0; i < 5; i++)
	{
		cout << "Enter the name of a candidate: ";
		cin.ignore(1024, '\n');
		getline(cin, candidate[i]);

		cout << "Enter the number of votes received"
		     << "by this candidate: ";
		cin >> votes[i];

		cout << endl << endl;

		total = total + votes[i];
	}

	cout << endl << endl;
	cout << setw(20) << left << "Candidate" << setw(20) << left 
	     << "Votes Received" << setw(20) << left << "% of Total Votes"
	     << endl << endl;

	for (j = 0; j < 5; j++)
	{
		cout << setw(24) << left << candidate[j] << setw(20)
		<< left << votes[j] << (votes[j] / total) * 100.0 << endl;
	}

	cout << endl;

	system("pause");

	return 0;
}


Thanks again!
Last edited on
It works for me :S
Only had to make line 24 and 25 in to one line.
> I also have to press enter twice after entering the first name before the second prompt appears.

Move the ignore() to after the formatted input operation:

1
2
3
4
5
6
7
8
9
10
11
               // ...

		// cin.ignore(1024, '\n'); *** move this
		getline(cin, candidate[i]);

		cout << "Enter the number of votes received"
		     << "by this candidate: ";
		cin >> votes[i];
		cin.ignore(1024, '\n'); // *** here
                
                // ... 


> And it's ignoring all of my endl statements.

That is strange.

In any case, you should not be writing all those unnecessary endls.
http://cppkid.wordpress.com/2008/08/27/why-i-prefer-n-to-stdendl/


Also, in general avoid magic numbers in your code.
http://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constants

And declare variables in the smallest scope in which they are required.

Prefer something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
        enum { N = 5 } ;
	string candidate[N];
	int votes[N];

        // int i ; // ***

        // ...

	for( int i = 0; i < N; ++i ) // i is local to the 'for'
        {
              // ...
        }
        



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?

Thanks again for your help.
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;
Last edited on
I got it. Thank you for your help. :)
Topic archived. No new replies allowed.