| | |
Need help implementing a gradebook using dynamic memory
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2009
Posts: 11
Reputation:
Solved Threads: 0
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:
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:
C++ Syntax (Toggle Plain Text)
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;
}•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
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.
C++ Syntax (Toggle Plain Text)
for (int i = 0; i < size; i++ ) { infile >> data[i].First >> data[i].Last >> data[i].Score1 >> /* same for the rest */ }
•
•
Join Date: Sep 2009
Posts: 11
Reputation:
Solved Threads: 0
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?
C++ Syntax (Toggle Plain Text)
#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 >> 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; } infile.close(); outfile.open("output.txt"); for (int i = 0; i < size; i++ ) { outfile << 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 << endl; } outfile.close(); delete [] data; return 0; }
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
•
•
•
•
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?
- 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.
- Use a vector or something else that does the expanding and contracting for you.
- 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.
- 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.
- 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.
•
•
Join Date: Sep 2009
Posts: 11
Reputation:
Solved Threads: 0
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;
}•
•
Join Date: Sep 2009
Posts: 11
Reputation:
Solved Threads: 0
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.
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.
Give this a try.
I know this is constantly swapping between temp and data but its semi compact and gives the output I think you are looking for.
C++ Syntax (Toggle Plain Text)
ifstream infile; ofstream outfile; int size = 1; infile.open("input.txt"); struct Student * data; struct Student * temp; data = new struct Student[size]; int i = 0; while( !infile.eof() ) { 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; if( !infile.eof() ) { size++, i++; temp = new struct Student[size]; for( int c = 0; c < size; c++ ) { temp[c] = data[c]; } data = temp; } } 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.
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
•
•
•
•
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.
![]() |
Similar Threads
- help with dynamic memory (C++)
- Dynamic memory for an array of objects (C++)
- Static and Dynamic Memory Allocations...Advan's & Disadvan's??? (Computer Science)
- regarding dynamic memory allocation (C)
- Dynamic memory allocation homework (C++)
Other Threads in the C++ Forum
- Previous Thread: can someone tell me why my copy constructor won't work?
- Next Thread: please reply me soon
Views: 493 | Replies: 17
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct studio temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






