# Thread: Quick question... Structs & Arrays.

1. ## Quick question... Structs & Arrays.

Hi everyone. I am working on making a program that deals with household surveys. I am still learning C++ and my first time dealing with more than one array. Here is the problem:
Write a program to read these survey results. Each record contains data for one household, including a 4-digit integer id number, the annual income, and number of household members. Max of 25 households. Write a program to read the results into 3 arrays.
It sounds to me like I need to use a struct.. but I'm not sure. Here is how I think it should be written...

Code:
```#define MAX_SIZE 25
#include
struct household{
int id[size];
int income[size];
int members[size];
};
void displayStruct(household stuhousehold, int);
int main(){
int size = 0;

struct household value;

household info;
displayStruct(household stuhousehold, size);

return 0;
}

void readStruct(household& oneHouse, int& size){
int i = 0;
cout << "Enter the four-digit integer identification number: ";
cin >> household id;
cout << "Enter the annual household income: ";
cin >> household income;
cout << "Enter the number of household members: ";
cin >> household members;

while(members > 0 && i < MAX_SIZE)                       // I was trying to count # of households with i.

}

void displayStruct(household stuhousehold, int n){
cout << setw(28) << "Identification Number" << setw(28) << "Annual Income" << setw(28) << "Household Members" << endl;

for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
for(int k=0; k<n; k++)
cout << setw(28) << stuhousehold.id[i] << setw(28) << stuhousehold.income[j] << setw(28) << stuhousehold.members[k] << endl;
}```
I think I messed up. I'm kind of confused so if anyone knows of a good website or something to help explain arrays and structs, let me know. Thank you.

2. This is not really a struct problem because of the requirement of reading into 3 arrays. Just move
Code:
```	int id[size]; <--- 1st array
int income[size]; <--- 2nd array
int members[size]; <--- 3rd array```
into your main() function to make your life easier.

if you want to use struct,
Code:
```struct household{
int id;
int income;
int members;
};

int main()
{
household array[size];  <------- an array of 25 element of type household.
}```
then your read struct function would be come
Code:
```void readStruct(household & struct_array, int& size){
int i = 0;
// use a while-loop, or do-while loop.
do{

cout << "Enter the four-digit integer identification number: ";
cin >> struct_array[i].id; // reading into the member variable "id" of the i-th element of the array of type household.
.... etc...
++i ; <--- need to increment i
}	while(i < MAX_SIZE)                       // I was trying to count # of households with i.

}```

3. Thank you. I'll try that.

4. Okay. Here is what I have.
Code:
```#include<iostream>
#include<iomanip>
#define MAX_SIZE 25

void getArray1(int[], int&, int&);
void getArray2(int[], int&);
void getArray3(int[], int&);
void displayTable(int[], int[], int[], int);

using namespace std;
int main(){
int list1[MAX_SIZE];
int list2[MAX_SIZE];
int list3[MAX_SIZE];
int size1, size2, size3;
int value1;

do{
getArray1(list1, size1, value1);			// 1 is 4-digit ID number
getArray2(list2, size2);					// 2 is Annual income
getArray3(list3, size3);					// 3 is Number of household members
}
while(value1 >= 0 && size1 < MAX_SIZE);

displayTable(list1, list2, list3, size1);

return 0;
}

void getArray1(int id[], int& size1, int& value){
int i = 0;

cout << "Enter the four-digit integer identification number (-1 to stop): ";
cin >> value;

id[i] = value;
i++;
size1 = i;
}
void getArray2(int income[], int& size2){
int j = 0;
int value;

cout << "Enter the annual household income: ";
cin >> value;

income[j] = value;
j++;
size2 = j;
}
void getArray3(int members[], int& size3){
int k = 0;
int value;

cout << "Enter the number of household members: ";
cin >> value;

members[k] = value;
k++;
size3 = k;
}

void displayTable(int id[], int income[], int members[], int size){
cout << "Number of households: " << size << endl;
cout << endl;
cout << setw(20) << "ID Number" << setw(20) << "Annual Income" << setw(20) << "Household Members" << endl;
for(int i=0; i < size; i++)
cout << setw(20) << id[i] << setw(20) << income[i] << setw(20) << members[i] << endl;
}```
When I run this, it doesnt display more than the last set of data (id, income, members) with -1 as the id..

5. You got your iteration incorrectly. You are iterating in main() instead of in the the function getArray. So for every iteration, you keep calling income[0], id[0] and members[0], thus your answer is just one output of -1 in the end. Well, one way to remedy that is to use static variable.
Code:
`static int k = 0 ;`
But why do that when you can just use one function that take in all 3 arrays, and iterate over them inside the function?

Code:
```void getArray(int id[],int income[], int members[])
{
// HERE IS WHERE YOU USE THE USE THE LOOP, not in main()
// don't need to pass in the size argument since you already define MAX_SIZE
//whatever-loop you want to do here: for, do-while, while. your choice.
//If you use do-while or while remember to declare int i = 0 outside of the loop and increment it inside the loop.
for( int i = 0 ; i < MAX_SIZE ; ++i)
{
cout << "Enter the four-digit integer identification number";
cin >> id[i];
cout << "Enter the annual household income: ";
cin >> income[i];
///etc.....
}```
}

6. Thanks! I seen that was my problem and I've been wondering how to fix it. I'm working on it now.

7. It worked! (using one function to take all 3 arrays). Thanks so much. Have a good day.

Popular pages Recent additions