Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,087 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,563 people online right now. Registration is fast and FREE... Join Now!




STRUCT

 
Reply to this topicStart new topic

STRUCT, Dynamically allocating and calling individual elements?

LittleRyoko
14 Oct, 2007 - 10:27 AM
Post #1

New D.I.C Head
*

Joined: 7 Sep, 2007
Posts: 10


My Contributions
OK, well I thought I had fixed the problem but I dont think I did. I am having trouble correctly allocating space I think. What should I be doing?? I am totally clueless about how to work with this. some help would be appreciated!!!
CODE

const int MAX_NUMBERS = 50;

struct SimpleRecord
{
    char *fName;
    char *lName;
    int id[MAX_NUMBERS];
    float payRate;
    float hours;
};
typedef SimpleRecord * ptrToRecord;


#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>

#include "Record.h"

using namespace std;

char* getFile();
void Sort( int [], int);
int search( int[], int, int);
void getID(int [], int);
void setId( SimpleRecord&, int );
int getId( SimpleRecord );

int main()
{    
    SimpleRecord * bob;
    
    bob = new SimpleRecord[50];

    int count=0;

    fstream infile;
    char* filename;
    filename = getFile();

    infile.open( filename );

    if(!infile)
    {
        cout<<"ERROR:" << endl;
        return 1;
    }
    cout<<"before while loop" << endl;
    char first[25];
    char last[25];
    int idnum;
    float rate, hour;
            
    while(!infile.eof())
    {
        infile >> first >> last >> idnum >> rate >> hour;
            count++;
            cout << first <<last << idnum << rate << hour<<endl;
                    
            /*
            bob[count].fName = first;
            bob[count].lName = last;
            bob[count].id = idnum;
            bob[count].payRate = rate;
            bob[count].hours = hour;

            
            
            cout<< bob[count].fName << bob[count].lName << bob[count].id;
            cout << bob[count].payRate<< bob[count].hours <<endl;
            */
}
    cout<< "After the infile " <<endl;
    cout <<"there are "<<count<< " employees in the file" <<endl;
    
    infile.clear();
    infile.seekg( 0L, ios::beg );
    
    ptrToRecord * myArray;
    myArray = new ptrToRecord[count];

    Sort( myArray[count]->id, count);


    infile.close();
        return 0;
}
char* getFile()
{
    char* file= new char[25];
    cout << "Enter file name : ";
    cin >>setw(24)>> file;
    cout<< "You have enterd " << file << endl;
    return file;
}
void Sort( int sortee[], int howMany)
{
    int startscan, minIndex, minValue;
    for(startscan=0; startscan < (howMany-1); startscan++)
    {
        minIndex = startscan;
        minValue = sortee[startscan];
        for(int index= startscan+1; index<howMany; index++)
        {
            if( sortee[index]< minValue)
            {
                minValue = sortee[index];
                minIndex = index;
            }
        }
        sortee[minIndex] = sortee[startscan];
        sortee[startscan] = minValue;
    }
}

int search ( int index[], int howMany, int seek)
{
    int first, last, middle;
    first =0;
    last = howMany-1;

    bool found = false;
    int position= -1;

    while(!found && first<=last)
    {
        middle=(first + last) /2;
        if(index[middle] == seek)
        {
            found = true;
            position = middle;
        }
        else if (index[middle] > seek )
            last = middle -1;
        else
            first = middle + 1;
    }
        return position;
}
void setId( SimpleRecord &s, int i )
{
    if ( i>0 && i<=9999 )
        s.id = i;
    else
    {
        cout << " Illegal ID \n NO CHANGE ";
    }
}

int getId( SimpleRecord s )
{
    return s.id;
}


What should I do to dynamically allocate space from an incoming file, I dont know how to do that so that I can call each element individually and modify them if need be.....How do I do it?!?! I'm so confused, plz help me! crazy.gif


Attached File(s)
Attached File  employees.txt ( 159bytes ) Number of downloads: 20
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: STRUCT
14 Oct, 2007 - 12:30 PM
Post #2

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,906



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
What I usually do is open the file and count the total number of records in the file. Then I close the file, allocate memory based on the count, reopen the file again and then read in all the data.
User is offlineProfile CardPM
+Quote Post

LittleRyoko
RE: STRUCT
14 Oct, 2007 - 01:28 PM
Post #3

New D.I.C Head
*

Joined: 7 Sep, 2007
Posts: 10


My Contributions
That is what my program does , but when I try to reopen and allocate the space I cannot seem to set it up correctly. Any advice about what I'm doiing wrong and how can I fix it?
CODE
while(!infile.eof())
    {
        infile >> first >> last >> idnum >> rate >> hour;
            count++;
}
    cout<< "After the infile " <<endl;
    cout <<"there are "<<count<< " employees in the file" <<endl;
    
    infile.clear();
    infile.seekg( 0L, ios::beg );
//    bob= new SimpleRecord[count];
    
    ptrToRecord * myArray;
    myArray = new ptrToRecord[count];

    for( int p=0; p<count; p++)
    {
        cout << *myArray[p]->fName << myArray[p]->lName << myArray[p]->id;
        cout<< myArray[p]->payRate << myArray[p]->hours <<endl;


Is the problem in my for loop? should it be set up like this
CODE
while(!infile.eof())
    {
        infile >> first >> last >> idnum >> rate >> hour;
            count++;
}
    cout<< "After the infile " <<endl;
    cout <<"there are "<<count<< " employees in the file" <<endl;
    
    infile.clear();
    infile.seekg( 0L, ios::beg );
SimpleRecord* bob;
bob = new SimpleRecord[count]

for( int i=0; i<count; i++)
{
    cout << bob[i].fName <<bob[i].lName <<bob[i].id;
    cout  <<bob[i].payRate <<bob[i].hours<<endl;
}


I obviously don't not how to set it up, help me please!
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: STRUCT
14 Oct, 2007 - 02:45 PM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,009



Thanked: 5 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
QUOTE(LittleRyoko @ 14 Oct, 2007 - 02:28 PM) *

That is what my program does , but when I try to reopen and allocate the space I cannot seem to set it up correctly. Any advice about what I'm doiing wrong and how can I fix it?
CODE
while(!infile.eof())
    {
        infile >> first >> last >> idnum >> rate >> hour;
            count++;
}
    cout<< "After the infile " <<endl;
    cout <<"there are "<<count<< " employees in the file" <<endl;
    
    infile.clear();
    infile.seekg( 0L, ios::beg );
//    bob= new SimpleRecord[count];
    
    ptrToRecord * myArray;
    myArray = new ptrToRecord[count];

    for( int p=0; p<count; p++)
    {
        cout << *myArray[p]->fName << myArray[p]->lName << myArray[p]->id;
        cout<< myArray[p]->payRate << myArray[p]->hours <<endl;


Is the problem in my for loop? should it be set up like this
CODE
while(!infile.eof())
    {
        infile >> first >> last >> idnum >> rate >> hour;
            count++;
}
    cout<< "After the infile " <<endl;
    cout <<"there are "<<count<< " employees in the file" <<endl;
    
    infile.clear();
    infile.seekg( 0L, ios::beg );
SimpleRecord* bob;
bob = new SimpleRecord[count]

for( int i=0; i<count; i++)
{
    cout << bob[i].fName <<bob[i].lName <<bob[i].id;
    cout  <<bob[i].payRate <<bob[i].hours<<endl;
}


I obviously don't not how to set it up, help me please!

with this line bob = new SimpleRecord[count] which needs a semicolon btw, you only allocated the needed space, but then you try to read the valus from bob? you can't do that, because you haven't assigned values to those variables of that structure. You need to assign the values to bob, before you can print them. I'm refering to the second code.

born2c0de gave you a hint... open the file, count, close, allocate, reopen, then! read from the file and assign to bob.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:16PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month