Thread: structure question

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    4

    structure question

    Hey, i am making a program that is basscially a database for information of my friends, it is simple and just to test my knowledge. However, I was wondering if there is a way to let the user input where the ptr-> is going to point to? For example

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct friends {
           int grade;
           int age;
           int phone;
           };
    
    int main(){
        int input;
        int frinput;
        int again;
        string name;
                              
                  
        friends *ptr;
        
        friends sam;
        sam.grade = 9;
        sam.age = 15;
        sam.phone = 9857685;
        
        friends carlos;
        carlos.grade = 9;
        carlos.age = 15;
        carlos.phone = 9756642;
        
        friends alex;
        alex.grade = 9;
        alex.age = 15;
        alex.phone = 9928346 ;
        
        
        
        top:
        cout<<"Welcome to sam's friends database:\n";
        cout<<" Enter the name of the person who's info you want to see, or ""list"" to see who is in the dtabase: ";
        cin>>name;
        if (name=="list"){
                        cout<<"In the friend's database you can access:\n sam \n alex \n carlos \n\nPress Enter to Continue";
                        cin.get();
                        cin.ignore();
                        goto top;
                        }
        else{
                        
        ptr = &  ;//this is the part I have trouble with
        cout<<"\n\nYou have accesed "<<name<<"'s info:";
                   
        cout<<"\n Grade: "<< ptr->grade<<"\n Age: "<<ptr->age<<"\n Phone: "<<ptr->phone;
        cout<<"\n\n Back to main menu? \n 1. Yes\n 2. No\n";
        cin>>again;
        if (again == 1){          
        goto top;}
        else{
        return 0;}
               }  
               
    }
    at the ptr = &(this is where i want the string to be put). Whenever I try and do this it doesnt work, is there any way?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    As you probably know, this doesn't work:

    ptr = &name;

    because name is a string and you declared ptr as type friends*. Even if it did work, the string variable 'name' doesn't have any members like 'class' and 'age'--after all it's just a string. One way to get a pointer to the right structure is to set up a series of if statements to check what name was entered, and then assign the address of the corresponding structure to ptr.

    You also need to include the <string> header file for 'name'.
    Last edited by 7stud; 04-27-2005 at 08:18 PM.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    4
    Yeah, I have done the multiple if thing before, but I am trying to use the string to cut down on how long the program is. This way I only have to set up one if statement and use the string name to fill in the blanks. Is there any way to do this, or something similar? Also, what do you mean by "You also need to include the <string> header file for 'name'."?
    Last edited by sam_8917; 04-27-2005 at 08:25 PM.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Yeah, I have done the multiple if thing before, but I am trying to use the string to cut down on how long the program is. This way I only have to set up one if statement and use the string name to fill in the blanks.
    A different approach to a series of if-statements would be to put a pointer to each friends structure you created for alex, carlos, and sam in an array, e.g.

    [pointer to alex, pointer to carlos, pointer to sam]

    Then, you could have the user input an integer menu choice, e.g.

    1. alex
    2. carlos
    3. sam

    The pointer to the structure will be

    pointerArray[input - 1];


    Also, what do you mean by "You also need to include the <string> header file for 'name'."?
    The reason you have this line in your program:

    #include <iostream>

    is because the meaing of "cout<<" and "cin>>" is defined there. name is declared as type string. The meaning of the string type is defined in the <string> header file, so you need to do this:

    #include <iostream>
    #include <string>

    If you require certain functions for your program, you will have to include the other header files where those functions are defined. When you look up a function definition in a C++ book or an online reference, the function definition will state what header file it is defined in, and then you just include that header file at the top of your program.
    Last edited by 7stud; 04-27-2005 at 11:23 PM.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    4
    Ahh, thanks, I am not very good with arrays so this will be good practice. Thankyou for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  2. Simple C# structure question
    By sketch in forum C# Programming
    Replies: 4
    Last Post: 09-14-2007, 04:29 PM
  3. data structure question
    By miami_victor in forum C++ Programming
    Replies: 13
    Last Post: 12-31-2004, 12:56 AM
  4. Array and Structure Question
    By loopshot in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2004, 05:10 PM
  5. structure question
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-03-2001, 09:04 PM