There is a structure: name, age, gender, street, house, apartment. It is necessary to calculate how many men live in each house. How to calculate the men themselves is understandable, but how to make exactly the sample around the house?

struct anket{ string firstname; string lastname; string midlename; string street_name; string house_number; int apartment_number; string gender; int yo; }; int main(){ int n,m=0,w=0; cout<<"Write please n: "; cin>>n; anket im[n]; for(int i = 0;i<n;i++){ cout<<"Write firstname: "; cin>>im[i].firstname; cout<<"Write lastname: "; cin>>im[i].lastname; cout<<"Write midlename: "; cin>>im[i].midlename; cout<<"Write name of street "; cin>>im[i].street_name; cout<<"Write number of house: "; cin>>im[i].house_number; cout<<"Write number of apartment: "; cin>>im[i].apartment_number; cout<<"Write gender(m/w): "; cin>>im[i].gender; cout<<"Write years old(yo): "; cin>>im[i].yo; } for(int i = 0;i<n;i++){ if( im[i].gender == "m"){ m++; } else{ w++; } } cout<<"Mans: "<<m<<endl; cout<<"Womans: "<<w<<endl; } 


This is essentially what I did, but I didn’t understand how to check the housework, because I don’t know what house number they will enter.

  • Probably, besides the structure itself, you have their array. And to make accounting at home is not more difficult than the floor, just in the cycle we go through the array, we check the floor and the house, and that it is needed. - AivanF.
  • This requires a group of beautiful. The men themselves will go ... - AR Hovsepyan

2 answers 2

The algorithm for finding matches by sex and house is as follows:

 // искомое значение – число верных совпадений int matches = 0; // параметры фильтрации - пол и дом string gender = "m"; string house = ""; cin >> house; // цикл по всем элементам for (int i = 0; i < n; i++) { // сверяем нужные параметры if (im[i].gender == gender && im[i].house_number == house) matches++; } // готово, переменная matches хранит число нужных совпадений 

PS I advise you to save the questionnaire to a file, so as not to enter it manually each time, but quickly test the algorithm. Also, you can put in the main loop, which will constantly ask what action to take (and exit one of the actions), so as not to restart the program every time.

  • but it seems to me that you should check whether you have not been confused with the checking algorithm? - AR Hovsepyan
  • now everything is OK - AR Hovsepyan
  • Thank you so much)) - IODAYSI ProJect

If there is a sequence, and it is necessary to compare the elements according to some criterion, it is better to immediately write a comparator, and then understand how to solve.

You can solve one standard algoitm, but you can (but it’s better to use a ready-made fast and safe algorithm) and write a counting cycle. In any case, the predicate (comparator) will serve the code.

  • Thank you, I will know how to optimize. Thanks again)) - IODAYSI ProJect
  • I was inconsiderate with the condition of the problem, therefore I will delete the solution version - AR Hovsepyan