Arrays and Structures

This is a discussion on Arrays and Structures within the C++ Programming forums, part of the General Programming Boards category; I'm trying to make a function that will read in the data from a file and store them in an ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    11

    Arrays and Structures

    I'm trying to make a function that will read in the data from a file and store them in an array of structures. I'm not really sure how to do I've tried various things short of just making finding out what the size of the file is as in how big to make the struct.
    Code:
    struct Node { 
    	
    	string name;
    	long ID; 
    	int SCORE;
    	
    };
    
    Node readStuct()
    {
        Node *records;
        int nSize, i;
         
                            myfile2.seekg (0, ios::end);
    			nSize = myfile2.tellg();
    			myfile2.seekg (0, ios::beg);
    			records = new int [nSize];
    
                while(!myfile2.eof())
                {
                    getline(myfile2, records[i].name, '\n');
                    cout<<records[i].name<<endl;
                    i++;
                } 
    			
               
        return records;
    
    }
    Last edited by Sherina; 11-21-2009 at 09:43 PM.

  2. #2
    dra
    dra is offline
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    Well first off, you need to indent your code properly.

    Also, you are declaring records an array of ints, not Node

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    11
    Code:
    struct Node { 
    	
    	string name;
    	long ID; 
    	int SCORE;
    	
    };
    
    Node readStuct()
    {
        Node *records;
        int nSize, i;
         
                            myfile2.seekg (0, ios::end);
    			nSize = myfile2.tellg();
    			myfile2.seekg (0, ios::beg);
    			records = new Node [nSize];
    
                while(!myfile2.eof())
                {
                    getline(myfile2, records[i].name, '\n');
                    cout<<records[i].name<<endl;
                    i++;
                } 
    			
               
        return *records;
    
    }
    Now I get a bunch of 0's as the output.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures and dynamically allocated arrays
    By Bandizzle in forum C Programming
    Replies: 7
    Last Post: 10-04-2009, 03:05 PM
  2. Replies: 3
    Last Post: 05-09-2009, 12:06 PM
  3. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 10:04 AM
  4. errors with arrays and structures
    By ssjnamek in forum C++ Programming
    Replies: 4
    Last Post: 03-03-2002, 11:48 PM
  5. Newbie Help (Arrays, Structures, Functions,Pointers)
    By tegwin in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 06:29 PM

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