Thread: Help with structs

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    6

    Help with structs

    Hey people I am having an issue with structs and hopeing someone could help me out...

    The code i have writen is as follows....

    Code:::

    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>

    struct name
    {
    char firstname[15];
    char middleinitial[3];
    char lastname[15];
    };
    name fullname;
    struct listing
    {
    name fullname;
    char title[31];
    char phone[15];
    char officenum[11];
    };
    listing entry;
    int main()
    {
    // Define local variables
    name firstname[15];
    name middleinitial[3];
    name lastname[15];
    listing title [31];
    listing phone[15];
    listing officenum;
    char end_condition;

    // Read in from file

    ifstream in;
    in.open ("direct.dat");
    in.get (entry.fullname.firstname, 15, ';');
    in.get ();
    in.get (entry.fullname.middleinitial, 3, ';');
    in.get ();
    in.get (entry.fullname.lastname, 15, ';');
    in.get ();
    in.get (entry.title, 31, ';');
    in.get ();
    in.get (entry.phone, 15, ';');
    in.get ()
    in.get (entry.officenum, 11, '\n');
    in.get ();

    :::Code

    The problem i am haveing right now is with the last 3 in.get where im trying to put data into the title, phone, and officenum. for some reason i cant even compile with those as they are.

    I dont know if im calling to the struct variables properly... im jsut confused because i have the top 3 working right... and i thought i had the form the same.

    also, one other quick question about structs... can i take a struct (that is created via user input) and export the entire struct to a file?

    any help will be most welcome!!!!!

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Okay, those aren't code tags, but at least you tried. What does the input data look like?

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    6
    the input data looks like this:

    firstname; m; lastname; job title; (pho) num-bers; officenumber

    Im also gona have to input in this same manner.
    Last edited by Kendrose; 12-09-2002 at 11:41 PM.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Your code is a bit messy as it is now. Could get rid of the semi-colon thing in the input? Anyways, the problem is you should be delimitting each entry with a space rather than a semi-colon. If you must leave the semi-colons there, then still delimit with spaces, but change the semi-colon into a null terminator.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    6
    I cant use a space, because i have to allow for the title to be multiple words seperated by a space.

    the issue i have is acessing the data variables in the struct "listing" i cannot access title, phone number, or office number

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  2. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 02:46 AM
  3. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 01:46 PM
  4. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 05:09 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM