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.
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.
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)
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:
#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()
{
constunsigned 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";
}
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.
const /// these qualifier is used to prevent illegal changes to variables or objects , for example
constint 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
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.