pointer

This is a discussion on pointer within the C++ Programming forums, part of the General Programming Boards category; hi My question is: I have a struct which is called Pet. data members name,type,age and a pointer variable nextPet. ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    3

    pointer

    hi


    My question is: I have a struct which is called Pet. data members name,type,age and a pointer variable nextPet.

    I must write a function that dynamically creats a new node for a Pet object. The function should ask to user to enter in the information for all data members of the node, and assign NULL to the nextPet data member. I wrote but it gives a error.
    here is the program...
    #include <iostream>
    struct Pet
    {
    char name;
    char type;
    int age;
    node *nextPet;
    };
    int main( )
    {
    Pet *nextPet;
    nextPet=new Pet;
    cout<<"please write name"<<endl
    cin>>nextPet->name;
    cout<<"please write animal type"<<endl;
    cin>>nextPet->type;
    cout<<"please write age<<endl;
    cin>>nextPet->age;
    root->next=NULL;
    RETURN 0;
    }
    pls help...thnks

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    3
    hi again..
    I made some changes in my program..but still it is not run correctly...

    #include <iostream>
    struct Pet
    {
    char name;
    char type;
    int age;
    Pet *nextPet;
    };
    int main( )
    {
    Pet *nextPet;
    nextPet=new Pet;
    cout<<"please write name"<<endl;
    cin>>nextPet->name;
    cout<<"please write animal type"<<endl;
    cin>>nextPet->type;
    cout<<"please write age"<<endl;
    cin>>nextPet->age;
    nextPet=NULL;
    return 0;
    }

    please help me. thanks

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    22
    What kind of error are you getting???

    You might need to declare your char name and char type as char name[20] and char type[10] or you could use a string class like

    #include <string.h>

    String name;
    String type;

    or if you have it (which is definately better)

    #include <apstring.h>

    apstring name;
    apstring type;
    <^>( * ; * )<^>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 04:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 08:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21