Structs and Incrementing Ouput

How would I be able to have this display the rest of the households without having to have 13 separate cout lines(one for each household)? In other words, how can I set it up to increment the output?

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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

struct Household
{
	int idNumber;
	int income;
	int members;
};

char again;
int main()
{
	do
	{
		Household house1 = {1041, 12180, 4};
		Household house2 = {1062, 13240, 3};
		Household house3 = {1327, 19800, 2};
		Household house4 = {1483, 22458, 8};
		Household house5 = {1900, 22458, 2};
		Household house6 = {2112, 17000, 7};
		Household house7 = {2345, 18125, 2};
		Household house8 = {3210, 15623, 6};
		Household house9 = {3600, 3200, 5};
		Household house10 = {3601, 6500, 2};
		Household house11 = {4724, 11970, 2};
		Household house12 = {6217, 8900, 2};
		Household house13 = {9280, 6200, 1};

		cout << "----------Household Survey----------" << endl << endl;
		cout << "ID Number	Annual Income	Household Members" << endl;
		cout << house1.idNumber << "		" << house1.income << "		" << house1.members << endl;



		cout << "\nDo you want to run this program again? Y/N: ";
		cin >> again;
		cout << endl;



	} while (again == 'y' || again == 'Y');

	return 0;
}
Create an array of Household or use a vector to store them, that way you can just use an iterator to access all of them.

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
#include <iostream>
#include <vector>
using namespace std;

struct Household
{
   int idNumber;
   int income;
   int members;
   
   Household(int n, int m, int o)
   :idNumber(n), income(m), members(o) {}
};

int main()
{
   vector <Household> community;
   char again;
   do
   {
      community.push_back(Household(1041, 12180, 4));
      community.push_back(Household(1062, 13240, 3));
      community.push_back(Household(1327, 19800, 2));
      community.push_back(Household(1483, 22458, 8));
      community.push_back(Household(1900, 22458, 2));
      community.push_back(Household(2112, 17000, 7));
      community.push_back(Household(2345, 18125, 2));
      community.push_back(Household(3210, 15623, 6));
      community.push_back(Household(3601, 6500, 2));
      community.push_back(Household(4724, 11970, 2));
      community.push_back(Household(6217, 8900, 2));
      community.push_back(Household(9280, 6200, 1));
      
      cout << "----------Household Survey----------" << endl << endl;
      cout << "ID Number	Annual Income	Household Members" << endl;
      
      for (auto it: community)
	 cout << it.idNumber << "		" << it.income << "		" << it.members << endl;
      
      cout << "\nDo you want to run this program again? Y/N: ";
      cin >> again;
      cout << endl;
      
      
   } while ((again|0x20) == 'y');
   
   return 0;
}


First time using the auto keyword in a for-loop and I've got to admit it is brilliant. I see c++ is leaning a lot more towards java programming style

E: I don't see the reason why you would want to run this program again and again.
Last edited on
or you can use old-style C-arrays if your compiler does not support/you are forbidden to use C++11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
do {
    Household houses[13] = {{1041, 12180, 4}, {1062, 13240, 3}, {1327, 19800, 2},
                            {1483, 22458, 8}, {1900, 22458, 2}, {2112, 17000, 7},
                            {2345, 18125, 2}, {3210, 15623, 6}, {3600, 3200, 5},
                            {3601, 6500, 2},  {4724, 11970, 2}, {6217, 8900, 2},
                            {9280, 6200, 1}};

    std::cout << "----------Household Survey----------\n\n" <<
                 "ID Number\tAnnual Income\tHousehold Members" << std::endl;
    for(int i = 0; i < 13; ++i) {
        std::cout << houses[i].idNumber << "\t\t" << houses[i].income <<
                     "\t\t" << houses[i].members << std::endl;
    }
    std::cout << "\nDo you want to run this program again? Y/N: ";
    std::cin >> again;
    std::cout << endl;
} while (again == 'y' || again == 'Y');

Smac89: you should use emplace_back() instead of push_back() to construct elements in place instead of dealing with unnessesary copy.
Last edited on
Gotcha thank you guys!
Topic archived. No new replies allowed.