Calling a fucntion and Passing a structure + other parameters

Hi guys,
I'm having issues passing structure name (student) when calling function countValues. I would like to pass structure student + resArray so I can assign values to numRes and mumNonRes that belong to student structure. Thanks for your help in advance.



#include <iostream>
#include <cstring>
using namespace std;

struct student
{

int numRes;
int numNonRes;


student() //constructor
{
numRes = 0;
numNonRes = 0;
}
void displayResCount()
{
cout << "Resident students: " << numRes << endl;
cout << "Non-resident students: " << numNonRes << endl;
}



};



void inputArray(char e[], int size);

void countValues(char resArray[], int size);







int main()
{

char* resArray;
int size;
student stu;
cout << "Enter the number of input values: ";
cin >> size;

resArray = new char(size);

inputArray(resArray, size);

countValues(resArray, size); // how do I pass structure student when calling function countvalues.

stu.displayResCount();


return 0;
}


void inputArray(char resArray[], int size)
{

for (int i = 0; i < size; i++)
{
cout << "Enter r or n:# ";
cin >> resArray[i];
}

}

void countValues(char resArray[], int size)
{

student ste; // using struct like this only works inside this function.
for (int i = 0; i < size; i++)
{
if (resArray[i] == 'r' || resArray[i] == 'R')
ste.numRes++;
else if (resArray[i] == 'N' || resArray[i] == 'n')
ste.numNonRes++;

}

}
Last edited on
student ste; // using struct like this only works inside this function.

ste goes out of scope when countValues() exits.

You need to pass ste into the function as an argument by reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <cstring>
using namespace std;

struct student
{   int numRes;
    int numNonRes;

    student() //constructor 
    {   numRes = 0;
        numNonRes = 0;
    }
    
    void displayResCount()
    {   cout << "Resident students: " << numRes << endl;
        cout << "Non-resident students: " << numNonRes << endl;
    }
};



void inputArray(char e[], int size);
void countValues(char resArray[], int size, student & stu); //  Add reference argument

int main()
{   char* resArray;
    int size; 
    student stu; 
    cout << "Enter the number of input values: ";
    cin >> size;
    resArray = new char(size);
    inputArray(resArray, size);
    countValues(resArray, size, stu);   // Add student structure as argument  
    stu.displayResCount(); 
    delete [] resArray;                 //  Avoid memory leak
    return 0;
}

void inputArray(char resArray[], int size)
{   for (int i = 0; i < size; i++)
    {   cout << "Enter r or n:# ";
        cin >> resArray[i];
    }
}

void countValues(char resArray[], int size, student & ste)  //  Add reference argument
{   for (int i = 0; i < size; i++)
    {   if (resArray[i] == 'r' || resArray[i] == 'R')
            ste.numRes++;
        else if (resArray[i] == 'N' || resArray[i] == 'n')
            ste.numNonRes++;
    }
}


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
AbstractionAnon,


thank you so much, i was having so much trouble finding out how to pass the structure to countValues.

Topic archived. No new replies allowed.