-
Structure problems...
I'm going down the list, designing programs as I go, to make sure I understand how to do things. When this program is supposed to display the people's names and ages, it just skips their name, and put's their age as 0. Anybody know what's wrong???
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;
family brother;
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;
family brother2;
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;
family brother3;
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;
family sister;
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;
family sister2;
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;
family sister3;
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;
}
-
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;
}
-
Well, first of all, I hate STL IO. I much prefer old fashioned, reliable C functions, such as fgets(). I will tell you that there are advantages to using the STL, but that this sort of wacky behaviour is not atypical either! Anyway, mainly I post about the structure of the program. You hardcoded the structures into the program - why not make it more generic?
Code:
struct family_info {
char name[50];
char status[50];
int age;
}, Family;
const int MAX = 200;
int main() {
int type;
int count;
int next = 0;
char buffer[100];
Family fam[ MAX ];
do {
cout<<"Do you have any other siblings? (y/n) ";
cin.getline(buffer, 50);
if( strcmpi(buffer, "y") != 0 ) {
break;
}
cout<<"What's is their relation?"<<endl;
cout<<"1) Brother"<<endl;
cout<<"2) Sister"<<endl;
cin.getline (buffer, 50);
type = atoi(buffer);
if(type == 1) {
strcpy(fam[next].status, "Brother");
}
else
if(type == 2) {
strcpy(fam[next].status, "Sister");
}
else {
strcpy(fam[next].status, "N/A");
}
cout<<"What's their name?"<<endl;
cin.getline (fam[next].name, 50);
cout<<"How old is he/she?"<<endl;
cin.getline (buffer, 50);
fam[next].age = atoi(buffer);
}while( ++next < MAX );
count = next;
next = 0;
while( next < count ) {
//...print data..
++next;
}
return 0;
}