Thread: help with error

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    2

    help with error

    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.
    Code:
    #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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
            for (int j = 0; j < 9; j++)
            {
                counts[v.get_raw_vote(j) - 'A']++;
            }
    All your example vote strings are 4 characters, so why are you going up to 9 ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    2
    Quote Originally Posted by Salem View Post
    Code:
            for (int j = 0; j < 9; j++)
            {
                counts[v.get_raw_vote(j) - 'A']++;
            }
    All your example vote strings are 4 characters, so why are you going up to 9 ?

    wow thank you soo much, setting it to 4 fixed the problem and now it runs correctly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 11-28-2011, 11:48 AM
  2. Replies: 4
    Last Post: 07-24-2011, 09:38 PM
  3. Replies: 1
    Last Post: 11-15-2010, 11:14 AM
  4. Replies: 3
    Last Post: 10-02-2007, 09:12 PM
  5. Replies: 1
    Last Post: 01-11-2007, 05:22 PM