Thread: Structure problems...

  1. #1
    MillaTime
    Guest

    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;
    }

  2. #2
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102
    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;
        }
    brotherbrother2brother3sistersister2sister3;

    int main()
    {
        
    char buffer[50];
        
    char age[50];
        
    int siblings=0;
        
    cout<<"Do you have any brothers? (y/n) ";
        
    cin.getline(buffer50); 
        if (!
    strcmpi(buffer"y"))
        {
        
    siblings += 1;
        
    cout<<"What's one of their names?"<<endl;
        
    cin.getline (brother.name50);
        
    cout<<"How old is he?"<<endl;
        
    cin.getline (age50); brother.age atoi(age);
        
    cout<<"Do you have another brother? (y/n): ";
        
    cin.getline(buffer50);
        }
        
        if (!
    strcmpi(buffer"y"))
        {
        
    siblings += 1;
        
    cout<<"What's his name?"<<endl;
        
    cin.getline (brother2.name50);
        
    cout<<"How old is he?"<<endl;
        
    cin.getline (buffer50); brother2.age atoi(buffer);
        
    cout<<"Do you have another brother? (y/n): ";
        
    cin.getline(buffer50);
        }

        if (!
    strcmpi(buffer"y"))
        {
        
    siblings += 1;
        
    cout<<"What's his name?"<<endl;
        
    cin.getline (brother3.name50);
        
    cout<<"How old is he?"<<endl;
        
    cin.getline (buffer50); brother3.age atoi(buffer);
        }

        
    cout<<"Do you have any sisters? (y/n): ";
        
    cin.getline(buffer50);
        
        if (!
    strcmpi(buffer"y"))
        {
        
    siblings += 1;
        
    cout<<"What's her name?"<<endl;
        
    cin.getline (sister.name50);
        
    cout<<"How old is she?"<<endl;
        
    cin.getline (buffer50); sister.age atoi(buffer);
        
    cout<<"Do you have another sister? (y/n): ";
        
    cin.getline(buffer50);
        }

        if (!
    strcmpi(buffer"y"))
        {
        
    siblings += 1;
        
    cout<<"What's her name?"<<endl;
        
    cin.getline (sister2.name50);
        
    cout<<"How old is she?"<<endl;
        
    cin.getline (buffer50); sister2.age atoi(buffer);
        
    cout<<"Do you have another sister? (y/n): ";
        
    cin.getline(buffer50);
        }

        if (!
    strcmpi(buffer"y"))
        {
        
    siblings += 1;
        
    cout<<"What's her name?"<<endl;
        
    cin.getline (sister3.name50);
        
    cout<<"How old is she?"<<endl;
        
    cin.getline (buffer50); 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(buffer50);
        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;

    WebSnozz-
    Cats have no butt cheeks.
    If one farted, then it would make a flute noise.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help...accessing character arrays in a structure
    By mathewmc in forum C Programming
    Replies: 7
    Last Post: 10-31-2006, 11:20 AM
  2. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 06:17 AM
  3. structure ...solution plz????
    By hegdeshashi in forum C Programming
    Replies: 4
    Last Post: 07-24-2006, 09:57 AM
  4. structure problems in windows program.
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 04-19-2004, 06:18 PM
  5. Realloc problems with sturcture array inside structure
    By daveyand in forum C Programming
    Replies: 2
    Last Post: 03-29-2004, 06:48 AM