Thread: structure names

  1. #1
    luigi
    Guest

    Question structure names

    hi im writing a little program for myself so i can get used c++.
    i made a structure to hold the data then i tried to make a function that would creat a new struct for each new card (void direct)
    as you can imagine the way i tried isnt working.
    any sugestions?

    here are some line i made..
    test is the struct itself
    create() gathers the name and stuff...
    direct() puts the info in the structure.

    struct test // structure of the cards
    {
    char fname[15];
    char lname[15];
    int age[2];
    int tel[12];
    int bdate[8];
    };

    void create() // gather info and put them into cards
    {

    cout <<endl<< "Enter first name :"<<endl;
    cin >> temp[0];

    cout <<endl<< "Enter last name :"<<endl;
    cin>>temp[1];

    cout <<endl<< "Enter age :"<<endl;
    cin>>temp[2];

    cout <<endl<< "Enter birth date (DD/MM/YY) :"<<endl;
    cin>>temp[3];

    cout <<endl<< "Enter phone number :"<<endl;
    cin>>temp[4];

    direct();

    cout <<endl<< "card created!" <<endl<< endl;
    }

    void direct() // put info from create() into struct test
    {
    char name[15];
    name = temp[1];
    test name;
    name.fname = temp[0];
    name.lname = temp[1];
    name.age = temp[2];
    name.bdate = temp[4];
    name.tel = temp[3];
    }


    thx in advance..
    luigi

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    functions can only see functions above them. for example, main can only see direct if direct is above it or prototyped above it.

    add a prototype of direct above create, i.e.

    void direct();

    also, initialize variables/arrays such as temp.

  3. #3
    luigi
    Guest

    Unhappy hmmm..

    guess i wasnt clear..

    ok temp[1] contains a last name right..
    so what i want to do is to name a structure by that name..

    so if temp[1] = floyd

    test temp[1] should equal test floyd

    if i create another adress card and the last of that dude is roger then temp[1] will be roger so test temp[1] should equal test roger...

    and so on..

    how do make that happen?

    luigi
    thx again..

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >how do make that happen?
    The way you want to...you don't. If you really must have something like this then create a wrapper structure or use an STL map. You should prefer the latter, but here is a crufty example of the former:
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    struct test // structure of the cards
    {
      char fname[15];
      char lname[15];
      char age[3];
      char tel[13];
      char bdate[9];
    };
    
    struct wrap
    {
      char name[20];
      test content;
    };
    
    char temp[5][15];
    
    void direct(wrap *list, int index)
    {
      strcpy ( list[index].name, temp[1] );
      strcpy ( list[index].content.fname, temp[0] );
      strcpy ( list[index].content.lname, temp[1] );
      strcpy ( list[index].content.age, temp[2] );
      strcpy ( list[index].content.bdate, temp[3] );
      strcpy ( list[index].content.tel, temp[4] );
    }
    
    void create(wrap *list, int index) // gather info and put them into cards
    {
      cout <<endl<< "Enter first name :"<<endl;
      cin>>temp[0];
    
      cout <<endl<< "Enter last name :"<<endl;
      cin>>temp[1];
    
      cout <<endl<< "Enter age :"<<endl;
      cin>>temp[2];
    
      cout <<endl<< "Enter birth date (DD/MM/YY) :"<<endl;
      cin>>temp[3];
    
      cout <<endl<< "Enter phone number :"<<endl;
      cin>>temp[4];
    
      direct(list, index);
    
      cout <<endl<< "card created!" <<endl<< endl;
    }
    
    // Test driver
    int main()
    {
      wrap stuff[5];
    
      create(stuff, 0);
      create(stuff, 1);
      create(stuff, 2);
      create(stuff, 3);
      create(stuff, 4);
    
      for(int i = 0; i < 5; i++) {
        cout<< stuff[i].name << endl << stuff[i].content.lname <<", "<<
          stuff[i].content.fname << endl << stuff[i].content.age << endl <<
          stuff[i].content.bdate << endl << stuff[i].content.tel << endl <<endl;
      }
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM