0

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

Termininja
  • 6,620
  • 12
  • 48
  • 49
user2305700
  • 83
  • 2
  • 10

3 Answers3

2

You can use the bound array argument syntax

void displayElecionData(string (&candidates)[5]);

In this case (as Ben Voigt points out), the array does not decay into a pointer (even though [] is just syntactic sugar) and you can assure in compile time that only arrays of 5 elements can be passed to your function

Nikos Athanasiou
  • 29,616
  • 15
  • 87
  • 153
0

There's no my_array.size() or similar function so you need to store the size somewhere, and send it as an argument in every function taking an array unless you have very well specified arrays. However there is a, in my view, nicer way of doing this. Use an std::vector or std::array.

#include <vector>
#include <string>

void doStuff(std::vector<std::string>& lines) {
    for(int i = 0; i < lines.size(); ++i) {
        //do stuff to lines[i]
    }
}
int main() {
    std::vector<std::string> my_lines;
    doStuff(my_lines);
    return 0;
}

I suggest you learn how to use raw arrays and then use std::vector or std::array instead. It will very likely lead to fewer bugs and problems in the long run.

If anybody else got curious about the difference between arrays and pointers I found this answer to be enlightening: https://stackoverflow.com/a/1049535/197657

Community
  • 1
  • 1
dutt
  • 7,909
  • 11
  • 52
  • 85
  • 1
    *In C++ arrays are, more or less, pointers* - No, they're not. OP, don't listen to this. And `std::string* lines`. Ugh. I would tear that bit out completely. – Ed S. Feb 09 '14 at 22:53
  • Removed the part about equality and the horrible version, I didn't know about bound array syntax that Nikos posted. – dutt Feb 10 '14 at 08:01
0

It seems that your actual problem is not related to passing array to function, rather you are missing something basic.

In enterElectionData function, you are reading input into class member candidates, but in function displayElecionData you are printing the parameter candidates, which is being passed from main function, where it was never given any value.

In displayElecionData function candidates parameter is hiding member candidates, to be more specific you can access class member by using this keyword (e.g. this->candidates). Or just remove the parameter as it is not required.

If this is the problem you are facing, then you need to familiarize yourself to basic concepts of Classes (instance members and their scope etc).

Ammar
  • 233
  • 1
  • 8