Need help implementing a gradebook using dynamic memory

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2009
Posts: 11
Reputation: thisisanfield.1 has a little shameless behaviour in the past 
Solved Threads: 0
thisisanfield.1 thisisanfield.1 is offline Offline
Newbie Poster

Need help implementing a gradebook using dynamic memory

 
0
  #1
Sep 22nd, 2009
This is my first time using pointers and I'm having some trouble. So far I have declared a struct named Student which is meant to hold the data for each student. There is an input file with an unknown amount of entries. Each entry has a first and last name along with 7 scores.

I am trying to read in the data from the text file at this point. Then I will need to sort the data (selection sort) in alphabetical order by the students last names. And lastly output the data to a new text file.

Below I'll provide what I've done so far. Really I'm just trying to get a better insight as to how I should be approaching this problem. I can figure out the sorting and output later.

A sample entry from the input file:
  1. ANTHONY CLARK 83 85 84 85 82 85 78

#include <fstream>
#include <iostream>

using namespace std;

struct Student
{
    char First[30];
    char Last[30];
    float Score1, Score2, Score3, Score4, Score5, Score6, Score7;

};

int main()
{
    ifstream infile;
    ofstream outfile;
    int size=5;
    infile.open("input.txt");
    
    struct Student * data;
    data = new struct Student[size];

    for (int i = 0; i < size; i++ )
    {
        infile.getline(data[i].First,30);

    }


    


    infile.close();
    outfile.open("output.txt");
    outfile.close();
     



    delete [] data;
    

    return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Need help implementing a gradebook using dynamic memory

 
0
  #2
Sep 22nd, 2009
Do not use getline here. The first name can be UP TO 30 characters, but will usually be less than that. Your getline stores other fields in the first name. This input file is separated by spaces and newlines, so it is tailor made for the >> operator instead of getline. I see no need to read in the data using getline.

  1. for (int i = 0; i < size; i++ )
  2. {
  3. infile >> data[i].First >> data[i].Last >> data[i].Score1 >> /* same for the rest */
  4. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 11
Reputation: thisisanfield.1 has a little shameless behaviour in the past 
Solved Threads: 0
thisisanfield.1 thisisanfield.1 is offline Offline
Newbie Poster

Re: Need help implementing a gradebook using dynamic memory

 
0
  #3
Sep 22nd, 2009
Thanks for the help. That is makes things much clearer. My next question is: How does the array continue to expand in order to span the length of the input file? I just did a simple print to the output file and it only displays the first 5 names on the list. I know that is because I set size = 5. What should I have done to make sure the array will continue to allocate memory?

  1. #include <fstream>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. struct Student
  7. {
  8. char First[30];
  9. char Last[30];
  10. float Score1, Score2, Score3, Score4, Score5, Score6, Score7;
  11.  
  12. };
  13.  
  14. int main()
  15. {
  16. ifstream infile;
  17. ofstream outfile;
  18. int size=5;
  19. infile.open("input.txt");
  20.  
  21. struct Student * data;
  22. data = new struct Student[size];
  23.  
  24. for (int i = 0; i < size; i++ )
  25. {
  26. infile >> data[i].First >> data[i].Last >> data[i].Score1 >> data[i].Score2
  27. >> data[i].Score3 >> data[i].Score4 >> data[i].Score5
  28. >> data[i].Score6 >> data[i].Score7;
  29. }
  30.  
  31.  
  32.  
  33.  
  34.  
  35. infile.close();
  36. outfile.open("output.txt");
  37.  
  38. for (int i = 0; i < size; i++ )
  39. {
  40. outfile << data[i].First << data[i].Last << " " << data[i].Score1
  41. << " " << data[i].Score2 << " " << data[i].Score3 << " "
  42. << data[i].Score4 << " " << data[i].Score5 << " "
  43. << data[i].Score6 << " " << data[i].Score7 << endl;
  44. }
  45.  
  46.  
  47. outfile.close();
  48.  
  49.  
  50.  
  51.  
  52. delete [] data;
  53.  
  54.  
  55. return 0;
  56. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 11
Reputation: thisisanfield.1 has a little shameless behaviour in the past 
Solved Threads: 0
thisisanfield.1 thisisanfield.1 is offline Offline
Newbie Poster

Re: Need help implementing a gradebook using dynamic memory

 
-1
  #4
Sep 22nd, 2009
morning bump
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Need help implementing a gradebook using dynamic memory

 
0
  #5
Sep 22nd, 2009
Originally Posted by thisisanfield.1 View Post
Thanks for the help. That is makes things much clearer. My next question is: How does the array continue to expand in order to span the length of the input file? I just did a simple print to the output file and it only displays the first 5 names on the list. I know that is because I set size = 5. What should I have done to make sure the array will continue to allocate memory?
You have a few options.
  1. Pick an original size that you know will be large enough to handle anything you'll encounter (say 1,000,000) and size it to that initially. It's a waste of memory and bad coding practice, but resizing will be unnecessary. That's assuming that such a maximum exists in the first place.
  2. Use a vector or something else that does the expanding and contracting for you.
  3. Go through the file once and count how many records there are and size the array to that, then go through again to read the data.
  4. Do an initial guess of the size, like you do with 5. Start reading the file. When you run out of room, resize the array manually by declaring a new array of the new size, deep copy the elements from the first array to the new array, then release the memory of the original array and reassign any pointers.
  5. Use malloc, calloc, realloc, and free rather than new and delete in order to possibly avoid a deep copy - this is a C solution rather than a C++ solution.

I would imagine you are expected to implement approach 4 above. Here' a link where Narue comments.

http://www.daniweb.com/forums/thread13709.html

There are other examples too. Google "C++ resize dynamic array" or something similar.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 11
Reputation: thisisanfield.1 has a little shameless behaviour in the past 
Solved Threads: 0
thisisanfield.1 thisisanfield.1 is offline Offline
Newbie Poster

Re: Need help implementing a gradebook using dynamic memory

 
0
  #6
Sep 22nd, 2009
Yea your correct. I need to dynamically expand the array as needed. I took a look at the link you gave me (thanks by the way) and am still having trouble implementing it correctly. So far this is what i have:

#include <fstream>
#include <iostream>

using namespace std;

struct Student
{
    char First[30];
    char Last[30];
    float Score1, Score2, Score3, Score4, Score5, Score6, Score7;

};

int main()
{
    ifstream infile;
    ofstream outfile;
    int size = 5;
    infile.open("input.txt");
    
    struct Student * data;
    struct Student * temp;
    data = new struct Student[size];
    

    for (int i = 0; i < size; i++)
    {
        infile >> data[i].First >> data[i].Last >> data[i].Score1 >> data[i].Score2
               >> data[i].Score3 >> data[i].Score4 >> data[i].Score5
               >> data[i].Score6 >> data[i].Score7;

        
        
    }
    
    temp = new struct Student[size * 2];

        for ( int i = 0; i < size; i++ )
        {
            temp[i] = data[i]; 
         }

        size *= 2;
        data = temp;

    for (int i = 0; i < size; i++)
    {
        infile >> temp[i].First >> temp[i].Last >> temp[i].Score1 >> temp[i].Score2
               >> temp[i].Score3 >> temp[i].Score4 >> temp[i].Score5
               >> temp[i].Score6 >> temp[i].Score7;
    }



    infile.close();
    outfile.open("output.txt");

    for (int i = 0; i < size; i++ )
    {
    outfile << data[i].Last << ", " << data[i].First << " " << data[i].Score1
            << " " << data[i].Score2 << " " << data[i].Score3 << " "
            << data[i].Score4 << " " << data[i].Score5 << " "
            << data[i].Score6 << " " << data[i].Score7 << endl;

    }

    delete [] data;
    delete [] temp;
    outfile.close();
     
    return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Need help implementing a gradebook using dynamic memory

 
0
  #7
Sep 22nd, 2009
Define "still having trouble implementing it correctly". What is happening that shouldn't be happening or what isn't happening that should be happening?
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 11
Reputation: thisisanfield.1 has a little shameless behaviour in the past 
Solved Threads: 0
thisisanfield.1 thisisanfield.1 is offline Offline
Newbie Poster

Re: Need help implementing a gradebook using dynamic memory

 
0
  #8
Sep 22nd, 2009
I am unsure as to where I should be placing size *= 2. Its purpose is to double the size of the array each time it is necessary.

At first, I didn't know whether I should be placing the new array (temp) inside the loop which is pulling data from the input file. The link you posted was helpful. I am just having trouble applying it to my program correctly. The program as it is now will now display 10 items, which isn't the end product I am looking for.

What I would like to do is have this new array named temp to continue and allocate memory until the input file has been accounted for entirely. Am I on the right track by setting up the new temp array after the original for loop, and running a series of them on its own? Thanks again for your help.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 251
Reputation: sfuo is on a distinguished road 
Solved Threads: 29
sfuo's Avatar
sfuo sfuo is offline Offline
Posting Whiz in Training

Re: Need help implementing a gradebook using dynamic memory

 
0
  #9
Sep 22nd, 2009
Give this a try.
  1. ifstream infile;
  2. ofstream outfile;
  3. int size = 1;
  4.  
  5. infile.open("input.txt");
  6.  
  7. struct Student * data;
  8. struct Student * temp;
  9. data = new struct Student[size];
  10.  
  11. int i = 0;
  12. while( !infile.eof() )
  13. {
  14. infile >> data[i].First >> data[i].Last >> data[i].Score1 >> data[i].Score2
  15. >> data[i].Score3 >> data[i].Score4 >> data[i].Score5
  16. >> data[i].Score6 >> data[i].Score7;
  17.  
  18. if( !infile.eof() )
  19. {
  20. size++, i++;
  21. temp = new struct Student[size];
  22. for( int c = 0; c < size; c++ )
  23. {
  24. temp[c] = data[c];
  25. }
  26. data = temp;
  27. }
  28. }
  29.  
  30. infile.close();

I know this is constantly swapping between temp and data but its semi compact and gives the output I think you are looking for.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Need help implementing a gradebook using dynamic memory

 
0
  #10
Sep 22nd, 2009
Originally Posted by thisisanfield.1 View Post
I am unsure as to where I should be placing size *= 2. Its purpose is to double the size of the array each time it is necessary.

At first, I didn't know whether I should be placing the new array (temp) inside the loop which is pulling data from the input file. The link you posted was helpful. I am just having trouble applying it to my program correctly. The program as it is now will now display 10 items, which isn't the end product I am looking for.

What I would like to do is have this new array named temp to continue and allocate memory until the input file has been accounted for entirely. Am I on the right track by setting up the new temp array after the original for loop, and running a series of them on its own? Thanks again for your help.

Don't confuse size with capacity. Size is the number of actual elements. Capacity is the number of elements you have room to store. size must always be less than or equal to capacity. It's usually less than capacity.

So you need a capacity variable and a size variable. "Resizing" involves changing the capacity, not the size. Obviously, "resize" can lead to confusion. Feel free to call your variables arrayCapacity and numStudents , so it will be extra clear. I would in fact advise doing that to avoid confusion.

So any display must involve the "size" variable. Any "resizing" involves changing the "capacity" of the array.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 493 | Replies: 17
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC