Matching two different variables in a structure

So in this code, first the program asks male and female names and their ages. Then the program asks the wives of the males.(wives being the females as previously entered) After that program matches the spouses together and displays them with their age.

My problem is that I can't match the males and females. I'm pretty sure there must be a piece of code before the last for loop.(35th line) I tried several things in there but they didn't work. The output is always the last thing i entered, for both ages and names.

for instance:

john-23, mary-24
john-23, mary-24
john-23, mary-24

And I know this is an assignment but I tried before posting here, I had only one week to finish it and I couldn't, and this wasn't the only assignment i had to do.

Usually in questions like these, the answer is most likely a thing that I didn't know its existence.

Here is my code:
 
 code is below
Last edited on
closed account (SECMoG1T)
You are overwriting your variables in your loops , use arrays or vectors to store your records
I did what you said and program doesn't write the same output again. now my output is what i wrote in order.

But my main problem is still the same. Somehow i have to match ages with people, and i have to match the husbands and wives.
I know my problem is between the line 30 and 36. But i still can't figure out what to write there.
This is the updated code(i will delete the other one so the page won't look so full)
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
#include <iostream>
#include <string>
using namespace std;

int main() {

	struct couples {
		string name;
		int age;
		couples *spouse;
	};

	couples male[3], female[3], m_age[3], f_age[3];
	couples *pmale, *pfemale;

	for (int a=0; a<3; a++)
	{pmale = &male[a];
	cout << "Enter male name: " << endl;
	cin >> male[a].name;
	cout << "Enter male age: " << endl;
	cin >> m_age[a].age;}

	for (int a=0; a<3; a++)
	{pfemale = &female[3];
	cout << "Enter female name: " << endl;
	cin >> female[a].name;
	cout << "Enter female age: " << endl;
	cin >> f_age[a].age;}

	for (int b=0; b<3; b++)
	{cout << "Enter the wives of the males: " << endl;
	cin >> pfemale->name;}

	

	for (int a=0; a<3; a++)
	{cout << male[a].name << "-" << m_age[a].age << ", " << female[a].name << "-" << f_age[a].age << endl;}
	system ("pause");
	return 0;
}
In the loop at lines 30-32, after entering the name of each wife, you have to find the wife's record in the female's array and assign the spouse pointers. In pseudocode, you want to do this:
1
2
3
4
5
6
7
8
9
10
for (int b=0; b<3; b++) {
	string spouse;
	cout << "Enter the wife of " << males[b] << ':' << endl;
	cin >> spouse;
	find spouse in the female array;
	if (you found a spouse) {
		set males[b].spouse to the female found
		set female's spouse pointer to &males[b];
	}
} 


When you print them out, you're printing the 1st male with the 1st female, the second male with the second female etc. Instead you should print the first male and his spouse, then the second male and his spouse etc:

1
2
3
4
5
6
7
8
9
for (int a=0; a<3; ++a) {
	cout << male[a].name << "-" << m_age[a].age;
	if (male[a].spouse) {
		// use the spouse pointer to output the spouse's name & age;
	} else {
		// output "(unmarried)"
	}
	cout << endl;
}

For this to work, the spouse pointer must be initialized to nullptr. The best way to do this is to add a constructor to struct couples:
1
2
3
4
struct couples {
	couples() : age(0), spouse(nullptr) {};
	...
};


closed account (SECMoG1T)
Even a simpler approach you could just stick to two arrays alone or maybe even simpler use a single array holding pairs of couples.

Here is an example.

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <string>

struct couples
{
    couples()=default;
    std::string name;
    int age;
};

void collect_info(couples males[],couples females[], unsigned limit)
{
  std::cout<<" Enter the names and ages of your "<<limit<<" participating males and females ,"
           <<"\n Use these format -> Name then Age\n\n";

    std::string curr_name;
    int curr_age;
    std::cout<<"\n\n Male participants \n\n";
  
  for(std::size_t i=0;i<limit;i++)
  {
    std::cout<<" Male "<<i+1<<" :";
    std::cin>>curr_name>>curr_age;

    males[i].name=curr_name;
    males[i].age=curr_age;

    std::cout<<std::endl;
  }

   std::cout<<" \n\n Female participants \n\n";
  for(std::size_t j=0;j<limit;j++)
  {
    std::cout<<" Female "<<j+1<<" :";
    std::cin>>curr_name>>curr_age;

    females[j].name=curr_name;
    females[j].age=curr_age;

    std::cout<<std::endl;
  }

}

void print_matches(couples males[],couples females[],unsigned limit)
{
    std::cout<<"\n\nThe following are the probable matches that we have found \n\n";
    for(std::size_t  i=0;i<limit;i++)
    {
        std::cout<<" Match "<<i+1<<" : "<<males[i].name<<"  "<<males[i].age<<std::endl;
        std::cout<<"           "<<females[i].name<<"  "<<females[i].age<<std::endl<<std::endl;
    }
}

int main()
{
    const unsigned number=3;
    couples male[number],female[number];

    collect_info(male,female,number);
    print_matches(male,female,number);

    std::cout<<"\nYou can always drop by and match your friends, GOODBYE ;)\n";


}
Last edited on
Hey andy, your code seems like a whole different language to me.
I can make it work from here but i have some problems.

What is const for? What is the purpose of couples()=default, and most importantly, if I delete them would the program work? Because our instructor didn't taught us such terms and I know him enough to not to write those things.
closed account (SECMoG1T)
No it's just c++, I just simplified your program to avoid the pointers that seemed to cause you problems

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
const /// these qualifier is used to prevent illegal changes to variables or objects , for example
const int x=4; ///x is a constant
x=5; ///these is an error - assignment to a read only variable

/// the reason I used it is to provide a constant array size , constant array sizes are required by static 
/// arrays so that the compiler can be able to evaluate the size during compile time. 
///  non const array sizes are considered an illegal. 

int size1=10;
int array [size1]; error , your array will have a variable size because later you might decide to change 
                             /// the variable 'size1 '. What will happens if such occurs¿

int arr2 [10]://ok 10 is literal constant


///for the default keyword
couples ()=default;

///These requests the compiler to synthesize a default constructor for my class,  if am convinced that 
///the synthesized  default constructor will work correctly e.g. if your class consists only of member 
///variables of built-in types a synthesized constructor will perfectly work fine . 
/// In our case here I required the default constructor because
/// it was needed on line 58 to create two arrays of default initialized " couples" objects, if your delete the
///default keyword it won't cause much trouble however you'll be required to explicitly define the default 
///constructor. 


NB: " More importantly I just wanted to help you grab a simpler idea on how you could approach your task
       however i'll advice you to write your  own version of the same you could use mine as a reference to 
       countercheck your errors, make sure you understand all the concepts, that will  help you boost your 
       programming skills." - Thanks 
Last edited on
Well for now, I have to use pointers. But after I give the assignment I will do what you said.
Last edited on
Andy's code lets you enter the male and female participants, but it doesn't let you enter the spouses. It seems to me that this is a key part of the whole exercise.
Topic archived. No new replies allowed.