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;
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++;
#include <iostream>
#include <cstring>
usingnamespace 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 = newchar(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++;
elseif (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.