Dynamic Array Output Problem
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:
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]);
}
}
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;
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>>
.)
精彩评论