Hello!

I am working a programming assignment and the goal is to turn the static arrays into dynamic arrays. The program reads in a file of names, votes and outputs the winner and number of votes. I have the first dynamic array for votes complete, but I am having trouble getting the char array to work. Here is the code with all the functions.

main

#include "Ch9_Ex7.h"

int main()
{
    int numCandidates;
    char **allCandidates;
    int *votes, index, totalVotes;
    ifstream infile;

    infile.open("/Users/user/Documents/ch9ex7dynamic/ch9ex7dynamic/Ch9_Ex7Data.txt");
    if (!infile)
    {
        cerr << "Cannot open input file. Program terminates!" << endl;
        return 1;
    }

// read number of candidates
   
    readVotes (infile, votes, allCandidates, numCandidates);

    cout << fixed << showpoint << setprecision(2);

    infile.close();

    totalVotes = sumVotes(votes, numCandidates);

    cout << left << setw(40) << "Candidate" <<  "Votes Received "
          << "   % of Total Votes" << endl;
    
    for (index = 0; index < numCandidates; index++)
        cout << left << setw(40) << allCandidates[index]
            << right << " " << setw(10) << votes[index] << "   " << setw(15)
            << (static_cast<double>(votes[index]) / static_cast<double>(totalVotes)) * 100 << endl;

    cout << "\nTotal:  " << totalVotes << endl;

    cout << "Winner: " << allCandidates[winnerIndex(votes,numCandidates)] << endl;
    system ("Pause");
    return 0;
}

function to read in the info

#include "Ch9_Ex7.h"

void readVotes (ifstream & infile, int * & votes,
                char ** & allCandidates, int & numCandidates)
{

// read number of candidates
    infile >> numCandidates;
    infile.ignore();  // carriage return

    votes = new int[numCandidates];
    allCandidates = new char*[numCandidates];
    
    for (int index = 0; index < numCandidates; index++)
    {
        infile >> votes[index];
        infile.ignore();  // space
        infile.get(allCandidates[index], MAX_NAME_LEN);
    }

}

get the total votes

int sumVotes(const int list[], int size)
{
    int sum = 0;

    for (int i = 0; i < size; i++)
        sum += list[i];

    return sum;
}

find winner

int winnerIndex(const int list[], int size)
{
    int maxIndex = 0;

    for (int i = 1; i < size; i++)
        if (list[i] > list[maxIndex])
            maxIndex = i;
    return maxIndex;
}

header

#ifndef Ch9_Ex7_h
#define Ch9_Ex7_h

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int const MAX_CANDIDATES = 25;
int const MAX_NAME_LEN = 31; // includes NULL

int extern sumVotes(const int [], int);
int extern winnerIndex(const int [], int);
void extern readVotes (ifstream &, int * &, char **  , int &);

#endif

and here is the text file:

5
1150 Peter White
1205 Nancy Joneston
1500 Thomas Louis Johnson
1304 Abrigail Louise Fitzpatrick
1410 William Ryan O'Connor

any help would be much appreciated!

The problem in read votes is that you are using the 2nd dymetion without initializing it you need to do this

allCandidates[index]=new char[MAX_NAME_LEN];

just before

infile.get(allCandidates[index], MAX_NAME_LEN);
This article has been dead for over six months. Start a new discussion instead.