With your struct you declare global variables brotherX and sisterX
But then in each if statement you declared each one again. These second declarations go out of scope, meaning they don't exist anymore, whenever the if block ends.
PHP Code:
if (1)
{
family brother;
cin>>brother.name;
}
after closing brace brother no longer exist therefore any data collected is lost. The global scope variable declared at the top above main still exist though so the compiler uses that in the output functions at the bottom, but these have no data still, since in each if block they used the brother that was declared there, it is in a closer scope so takes precedence.
You could either do like
PHP Code:
if (1)
{
family brother;
cin>>::brother.name;//the :: tells it to use the global scope brother instead of the one you just declared
}
or as I've modified your program below just remove the declaration inside each if since you aren't even using those
only declare variables inside if statement that are only going to be used in that if statement.
PHP Code:
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
struct family {
char name[50];
int age;
}brother, brother2, brother3, sister, sister2, sister3;
int main()
{
char buffer[50];
char age[50];
int siblings=0;
cout<<"Do you have any brothers? (y/n) ";
cin.getline(buffer, 50);
if (!strcmpi(buffer, "y"))
{
siblings += 1;
cout<<"What's one of their names?"<<endl;
cin.getline (brother.name, 50);
cout<<"How old is he?"<<endl;
cin.getline (age, 50); brother.age = atoi(age);
cout<<"Do you have another brother? (y/n): ";
cin.getline(buffer, 50);
}
if (!strcmpi(buffer, "y"))
{
siblings += 1;
cout<<"What's his name?"<<endl;
cin.getline (brother2.name, 50);
cout<<"How old is he?"<<endl;
cin.getline (buffer, 50); brother2.age = atoi(buffer);
cout<<"Do you have another brother? (y/n): ";
cin.getline(buffer, 50);
}
if (!strcmpi(buffer, "y"))
{
siblings += 1;
cout<<"What's his name?"<<endl;
cin.getline (brother3.name, 50);
cout<<"How old is he?"<<endl;
cin.getline (buffer, 50); brother3.age = atoi(buffer);
}
cout<<"Do you have any sisters? (y/n): ";
cin.getline(buffer, 50);
if (!strcmpi(buffer, "y"))
{
siblings += 1;
cout<<"What's her name?"<<endl;
cin.getline (sister.name, 50);
cout<<"How old is she?"<<endl;
cin.getline (buffer, 50); sister.age = atoi(buffer);
cout<<"Do you have another sister? (y/n): ";
cin.getline(buffer, 50);
}
if (!strcmpi(buffer, "y"))
{
siblings += 1;
cout<<"What's her name?"<<endl;
cin.getline (sister2.name, 50);
cout<<"How old is she?"<<endl;
cin.getline (buffer, 50); sister2.age = atoi(buffer);
cout<<"Do you have another sister? (y/n): ";
cin.getline(buffer, 50);
}
if (!strcmpi(buffer, "y"))
{
siblings += 1;
cout<<"What's her name?"<<endl;
cin.getline (sister3.name, 50);
cout<<"How old is she?"<<endl;
cin.getline (buffer, 50); sister3.age = atoi(buffer);
}
cout<<"okay..."<<endl;
cout<<brother.name<<" is "<<brother.age<<" years old."<<endl;
cout<<brother2.name<<" is "<<brother2.age<<" years old."<<endl;
cout<<brother3.name<<" is "<<brother3.age<<" years old."<<endl;
cout<<sister.name<<" is "<<sister.age<<" years old."<<endl;
cout<<sister2.name<<" is "<<sister2.age<<" years old."<<endl;
cout<<sister3.name<<" is "<<sister3.age<<" years old."<<endl;
cout<<"Is my information correct? (y/n) ";
cin.getline(buffer, 50);
if (!strcmpi(buffer, "y"))
{
cout<<"Okay, i've got a stalker lined up for your family, he "<<
"should be arriving shortly to destroy your lives!";
}
else if (!strcmpi(buffer, "n"))
int main();
return 0;
}