2

I am working on a homework assignment where I had to convert all of the static arrays in a program into dynamic arrays using pointers. I am pretty sure I am understanding the concept, I have made the changes and my program runs. The problem is with my output results. I suspect I am inputting the data incorrectly from the file I am using. Here is a pic of my issue as well as relevent code:

EDITS FROM 9/21 AFTER THIS POINT:

Output & Data file: enter image description here

main

#include "Ch9_Ex7.h"

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

    allCandidates = new string[1];
    votes = new int[1];


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

// read number of candidates

    readVotes (infile, votes, allCandidates, numCandidates);

    //delete [] votes;
    //delete [] allCandidates;

Input Function:

#include "Ch9_Ex7.h"

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

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

    //delete [] votes;
    //delete [] allCandidates;

    allCandidates = new string[numCandidates];
    votes = new int[numCandidates];

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

}
2
  • Your idea is good. char* means a string not an array of strings. Keep digging, it's a nice homework. Commented Sep 21, 2011 at 0:59
  • 1
    +1 Just for knowing how to ask a question. :) Commented Sep 21, 2011 at 1:43

2 Answers 2

3

You are creating an array of one char and one int with this code:

allCandidates = new char[1];
votes = new int[1];

I believe you meant:

allCandidates = new char[numCandidates];
votes = new int[numCandidates];

which creates dynamic arrays of size numCandidates.

Also, as you are inputting names of candidates you probably wanted to use std::string like so:

string *allCandidates;
allCandidates = new string[numCandidates];

(Thanks to Ben Voigt for pointing that out) And since you're inputting their full name you'll need to input it differently. Perhaps use getline():

getline(cin, allCandidates[i]);

In response to your edit:

You will have to pass your pointers as references like so:

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

and free them in main()

delete[] votes;
delete[] allCandidates;
Sign up to request clarification or add additional context in comments.

10 Comments

new string[numCandidates], more likely.
I declare those statements before numCandidates is input from the text file. numCandidates is input in a function that both array pointers are past to so using [1] is just temporary until they get to that input function.
@mwmnj You'll have to move the new statement till after you get the input then. Either that or you have to deallocate it and reallocate it when you get the input but that would be kind of pointless.
create your arrays after you get the numcandidates in readVotes function infile >> numCandidates; string *allCandidates; allCandidates = new std::string[numCandidates];
The reason I can't do that is because the arrays are passed to this input function: "readVotes (infile, votes, allCandidates, numCandidates);" If I dont allocate before calling the function I get an error. I am working on deallocating and reallocating inside the input function now to see if that will work
|
1

First off, this is absolutely terrible design, so please don't do this outside the scope of this exercise.

Now, on to the question. If you want to create a dynamic object (or array) somewhere and pass the pointer to it back, you should take the pointer by reference. You also have to read the names into a string, not into a single character.

void readVotes (std::ifstream & infile, int * & votes, std::string * & allCandidates, int & numCandidates)
{
  // read numCandidates

  votes = new int[numCandidates];
  allCandidates = new std::string[numCandidates];

  // populate
}

The caller has to remember to clean up:

int main()
{
  int n;
  int * votes;
  std::string * names;

  readVotes(std::cin, votes, names, n);

  // ...

  delete[] votes;
  delete[] names;
}

(In a real-world situation, I would have the function return a std::vector<std::pair<int, std::string>>.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.