May 9, 2012 at 4:24am UTC
I am taking a c++ course and we have an assignment out of the book, he gave us an answer sheet but the program does not work correctly. when it runs we get an error that says
expression: string subscript out of range
I was hoping someone could take a look at our code and give me a little help on finding the problem.
the problem has a text file with an id number and 4 letters next to it, the program is supposed to read from this file and the letters are different votes that can happen
ex.
1000 BEFI
1001 BDGH
1002 BEGH
then they take the votes for each part and put them into a percentage.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
class Voter
{
public:
Voter(int id, string votes);
int get_id() const;
char get_raw_vote (int idx);
int get_vote_count() const;
string get_mayor_vote() const;
string get_prop_17_vote() const;
string get_measure_1_vote() const;
string get_measure_2_vote() const;
private:
int id;
string votes;
} ;
void write_percentage (ostream& out, int count, int total);
int main()
{
vector<Voter> voters;
ifstream list;
int id, total_voters, search_id;
bool found;
string vote_str;
int counts[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0};
list.open("List.txt");
if (list.fail())
{
cout << "Error" << endl;
exit(1);
}
while (!list.eof())
{
list >> id;
list >> vote_str;
Voter v (id, vote_str);
voters.push_back(v);
}
total_voters = voters.size();
for (int i = 0; i < total_voters; i++)
{
Voter v = voters[i];
for (int j = 0; j < 9; j++)
{
counts[v.get_raw_vote(j) - 'A']++;
}
}
cout << "1. Mayor" << endl;
cout << " A. Pincher, Penny: ";
write_percentage( cout, counts[0], total_voters);
cout << endl;
cout << " B. Dover, Skip : ";
write_percentage(cout, counts[1], total_voters);
cout << endl;
cout << " C. Perman, Sue : ";
write_percentage(cout, counts[2], total_voters);
cout << endl;
cout << "2. Proposition17" << endl;
cout << " D. Yes:";
write_percentage(cout, counts[3], total_voters);
cout << endl;
cout << " E. No:";
write_percentage(cout, counts[4], total_voters);
cout << endl;
cout << "3.Measure 1 " << endl;
cout << " F. Yes:";
write_percentage(cout, counts[5], total_voters);
cout << endl;
cout << " G. No:";
write_percentage(cout, counts[6], total_voters);
cout << endl;
cout << "4. Measure 2" << endl;
cout << " H. Yes:";
write_percentage(cout, counts[7], total_voters);
cout << endl;
cout << " I. No:";
write_percentage(cout, counts[8], total_voters);
cout << endl;
cout << "Enter the voter id";
cin >> search_id;
found = false;
for (int i = 0; (i < total_voters) && (! found); i++)
{
if (voters[i].get_id() == search_id)
{
found = true;
cout << "Voter" << search_id << " 's votes: " << endl;
cout << " 1. Mayor"
<< voters[i].get_mayor_vote() << endl;
cout << " 2. Proposition 17: "
<< voters[i].get_prop_17_vote() << endl;
cout << "3. Measure 1: "
<< voters[i].get_measure_1_vote() << endl;
cout << " 4. Measure 2: "
<< voters[i].get_measure_2_vote() << endl;
}
}
return 0;
}
void write_percentage(ostream& out, int count, int total)
{
out.setf(ios::fixed);
out.setf(ios::showpoint);
out.precision(2);
out << (static_cast<double>(count * 100) /total) << "%";
}
Voter::Voter(int voter_id, string voter_votes)
{
id = voter_id;
votes = voter_votes;
}
int Voter::get_id() const
{
return id;
}
char Voter::get_raw_vote(int idx)
{
return votes[idx];
}
int Voter::get_vote_count() const
{
return votes.length();
}
string Voter::get_mayor_vote() const
{
string mayor;
char raw;
raw = votes[0];
if (raw == 'A')
{
mayor = "Pincher, Penny";
}
else if (raw == 'B')
{
mayor = "Dover, Skip";
}
else if ( raw == 'C')
{
mayor = "Perman, Sue";
}
else
{
mayor = " (Invalid vote)";
}
return mayor;
}
string Voter::get_prop_17_vote() const
{
if (votes[1] == 'D')
{
return "Yes";
}
else {
return "No";
}
}
string Voter:: get_measure_1_vote() const
{
if (votes[2] == 'F')
{
return "Yes";
}
else {
return "No";
}
}
string Voter::get_measure_2_vote() const
{
if (votes[3] == 'H')
{
return "Yes";
}
else {
return "No";
}
}
Thank you for any help you can provide.
May 9, 2012 at 5:47am UTC
Try using a debugger to figure out when the error is occurring.