I am trying to get a better feel for working with arrays in C++. I am using the object oriented approach in doing this, so I do have a class. I am accepting user input into a string array in one function and trying to print the output in another. I am having some difficulty printing or passing the output. How do you pass the string array as a parameter to another function? I know C++ uses pointers and addresses. When debugging the program I see that only the array address is passed to the printing function. How do I get the actual array values to pass to the output function? My three files are below...
My main...
#include <iostream>
#include "ElectionResults.h"
#include <string>
using namespace std;
void main()
{
string candidates[5];
ElectionResults election;
election.enterElectionData();
election.displayElecionData(candidates);
cin.ignore();
cin.ignore();
}
My CPP file...
#include "ElectionResults.h"
#include <iostream>
using namespace std;
ElectionResults::ElectionResults(void)
{
}
void ElectionResults::enterElectionData(void)
{
for (int i = 0; i < 5; i++)
{
cout << "Candidiate number " << i + 1 << ": ";
cin >> candidates[i];
}
cout << endl;
}
void ElectionResults::displayElecionData(string candidates[5])
{
for (int i = 0; i < 5; i++)
{
cout << "Candidate name " << i + 1 << ": " << candidates[i] << endl;
}
cout << endl;
}
ElectionResults::~ElectionResults(void)
{
}
My header file...
#ifndef ELECTIONRESULTS_H
#define ELECTIONRESULTS_H
#include <string>
using namespace std;
class ElectionResults
{
private:
string candidates[5];
public:
void enterElectionData();
void displayElecionData(string candidates[5]);
ElectionResults(void);
~ElectionResults(void);
};
#endif
Thank You