Thread: how do i display this struct i have??

  1. #1
    n00b-for-rent
    Guest

    Question how do i display this struct i have??

    i want to be able to display wut i input in the struct. heres the code:

    Code:
    struct agendaview 
    {
    	char name[15];
    	char tel [10];
    	char adress[50];
    };
    //this is the struct code
    
    void newperson ()
    {
    	agendaview *newperson ;
    	newperson = new agendaview ;
    	if (newperson != NULL )
    	{
    		cout<<"Name:";
    		cin.get (newperson->name[15]);
    		cin.ignore (80,'\n');
    		cout<<"TEL:";
    		cin.get (newperson->tel[10]);
    		cin.ignore (80,'\n');
    		cout<<"Adress:";
    		cin.get (newperson->adress[50]);
    		cin.ignore (80,'\n');
    	}
    
    }
    //this is how i enter the data.
    now i need to know how do i display the data i input.
    something like this:

    name tel adress
    ------------------------------------------------------------------------------
    (wutever) (wutever) (wutever)

    thx in advance.

  2. #2
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Why dont you use cout << agendaview.name and so on

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    change this:

    cin.get (newperson->name[15]);

    to this:

    cin.getline(newperson->name, 15);

    and so on.

    to display just go

    cout << newperson->name << ' ' << newperson->tel etc.

  4. #4
    n00b-for-rent
    Guest

    almost there

    cout <<agendaview.name did not work.
    so i used the other advice which works except for one thing. i need to initialize newperson to something. i really dont know wut to initialize it to.
    Code:
    void viewall ()
    {
    	agendaview *newperson;
    	// i need to initialize newperson to something here
                    cout <<newperson->name<<'  ';
    	cout <<newperson->tel<< '  ';
    	cout <<newperson->adress<<endl;
    }

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You are writing C++ in C!!

    ...and I forgot to use code tags!

    Here:

    Code:
    
    class agendaview { public: 
    char name[15];	
    char tel [10];	
    char adress[50];
    
    
    agendaview( char *Name, char *Tel, char *Address)
    {
    strncpy(name, Name, 15);
    strncpy(tel, Tel, 10);
    strncpy(adress, Address, 15);
    }
    
    void create(){	
    cout<<"Name:";
    cin.get (name, 15);		
    cin.ignore (80,'\n');		
    cout<<"TEL:";		
    cin.get (tel, 10);
    cin.ignore (80,'\n');		
    cout<<"Adress:";		
    cin.get (adress, 50);		
    cin.ignore (80,'\n');	
    }
    
    void print() {
    cout<<"Name:";
    cout<<name;
    cout<<"TEL:";		
    cout<<tel;
    cout<<"Adress:";		
    cout<<adress;
    }
    
    };
    
    
    
    int main()
    {
    
    agendaview Ralph("Ralph", "2146162352", "5720 maple Ave");
    
    agendaview New;
    
    New.create();
    
    Ralph.print();
    
    New.print();
    
    
    return 0;
    }
    Last edited by Sebastiani; 05-02-2002 at 03:46 PM.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    you could use linked lists for your agenda, i think that would be ezier to make a function to view it then using just structs.

    PHP Code:
    typedef _agenda struct
    {
       
    char name[15];    
       
    char tel [10];    
       
    char address[50];

       
    _agenda  *next;
    agenda;

    agenda *root;
    root->next NULL
    or whatever for the linked list (im not that good at linked lists right now)

    PHP Code:
    void view_agenda()
    {
       
    agenda *temp;
       *
    temp = new agenda;

       
    temp root// considering root is the first node in your linked list
       
       
    while(temp->next != NULL)
       {
          
    cout << temp->name << ' ';
          
    cout << temp->tel << ' ';
          
    cout << temp->address << endl;

          if(
    temp->next != NULL)
             
    temp temp->next;
       }

    then to add a new person do something like:
    PHP Code:
    void AddPerson(char name[15], char tel[10], char address[50])
    {
       
    agenda *temp;
       
    agenda *newperson;

       
    temp = new agenda;
       
    newperson = new agenda;

       while(
    temp->next != NULL)
       {
          if(
    temp->next != NULL)
             
    temp temp->next
       
    }

       
    newperson temp// Set newperson to the last position in the linked list

       
    delete temp// delete temp from the memory

       // Add all the details for the new person
       
    newperson->name name;
       
    newperson->tel tel;
       
    newperson->address address;
       
    newperson->next NULL;

    well, something like that, as i said im not that great with linked lists
    Last edited by Okiesmokie; 05-03-2002 at 05:04 AM.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    This is a C style struct

    typedef struct agendaview

    This is a C++ style struct

    struct agendaview

    Use the latter in C++ code.
    __________________________________
    agendaview *newperson;
    // i need to initialize newperson to something here

    you did a pretty decent job of initializing a pointer with data in newperson(), so I know you know how to do it at leasty one way. Maybe if you give a description of you what you are trying to do in the overall program we can answer you question better. If you are trying to display nodes in a list, the OkieSmokie has some pretty good ideas, not perfect mind you, but pretty good nevertheless.
    ________________________________________

    try not to use the same name for a variable and a function. It can get confusing: see newperson() and agendaview * newperson;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Help please im stuck
    By ItsMeHere in forum C Programming
    Replies: 7
    Last Post: 06-15-2006, 04:07 AM
  3. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  4. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM