### 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?

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````#include #include #include 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include #include 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 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
 ``1234567891011121314151617`` ``````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.