help plz
dear people
recently i have been given with this assignment where i have to read from a txt file and then print out the following.
the full class list.
the class list sorted by gpa
the class list sorted by name initial the user enters
the class average gpa.
i am stuck on the second part of it.
i know that we have to store in in a array but when i store it each array consists of a single charecter.please can ne1 guide me as to how i need to store ithe file in a array.
that is so that i can sort it out according to the GPA
here is the txt file
Lastname(initial) ID# Age GPA
-----------------------------
F 2221 24 3.2
B 1236 19 1.0
H 3476 22 4.0
H 1122 23 2.2
R 5412 21 3.9
B 0071 26 0.9
here is what i have done till now
#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>
const int ARRAYSIZE = 1000;
const int ROW = 6;
const int COLUMN = 10;
void telluser();
int askuser();
void displayclasslist();
void sortandprint();
int main()
{
int ans;
telluser();
ans=askuser();
if (ans==1)
{
displayclasslist();
}
if (ans==2)
{
sortandprint();
}
system("PAUSE");
return 0;
}
void telluser()
{
cout<<"This programe handles the student records of tough university and "
<<"displays data according to user specification\n\n\n\n";
}
int askuser()
{
int ans;
cout<<"What do you wish to do? (please enter the coresponding number)\n\n";
cout<<"\t1: Print the entire class list.\n";
cout<<"\t2: Print class list sorted by GPA.(Lowest to Highest)\n";
cout<<"\t3: Print class list of students which match a given"
<<"lastname initial.\n";
cout<<"\t4: Print the mean and standard deviation of the GPA values.\n";
cout<<"\t5: Exit program.\n\n";
cout<<"Enter Your Choice:\t";
cin>>ans;
cout<<endl<<endl;
return ans;
}
void displayclasslist()
{
ifstream inFile;
inFile.open("F:\\StudentRec.txt",ios::in);
char ch[ARRAYSIZE];
cout<<"LastName(initials)\tID#\t\t\tAGE\t\t\tGPA\n";
cout<<"-----------------------------------------";
cout<<"--------------------------------------\n";
inFile.getline(ch,ARRAYSIZE,'\n');
inFile.getline(ch,ARRAYSIZE);
while (!inFile.eof())
{
inFile.getline(ch,ARRAYSIZE,' ');
cout<<ch<<"\t\t\t";
}
cout << endl;
}
void sortandprint()
{
ifstream inFile;
inFile.open("F:\\StudentRec.txt",ios::in);
cout<<"LastName(initials)\tID#\t\t\tAGE\t\t\tGPA\n";
cout<<"-----------------------------------------";
cout<<"--------------------------------------\n";
char ch[50];
char file[ROW][COLUMN];
inFile.getline(ch,ARRAYSIZE,'\n');
inFile.getline(ch,ARRAYSIZE);
while (!inFile.eof())
{
for(int i=0;i<ROW;i++)
{
for(int j=0;j<COLUMN;j++)
{
inFile>>file[i][j];
}
}
}
}
kesh1000
Junior Poster in Training
70 posts since May 2009
Reputation Points: 10
Solved Threads: 3
Please use code tags to make the code readable.
You probably don't want to read the data a single character at a time - consider using cin.getline() to get a line at a time. Also, if you store things in a std::vector, you can then use the std::sort() function to sort the GPAs.
Dave
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
First some remarks on the OP's code:
Use new-style headers if possible, so:
#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>
would become
#include <iostream>
#include <<strong>c</strong>stdlib>
#include <fstream>
Avoid the use of system("pause"); (look in my signature, for a replacement and the reason on why to avoid it)
:)
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
If you read all that data into memory at the beginning of the program you can then write a simple function to display the information.
create a structure that contains each of the fields in the file
read the file into an array of structures
use std::sort() to sort the structures by name
display the data
use std::sort() to sort the structures by gpa
display the data
perform other calculations to get class average etc.
Sorting the array of structures is only one line of code! No need to write all that complicate sort algorithms yourself, unless your instructor told you to. The entire program can be done in about 100 lines of c++ code, so if you are writing more than that you are writing too much code.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
plz note that we havent reached vectors yet......we have done till arrays and were asked to research on infile to do our assignment
thats why im not familiar with getting a file into a array.please can you show me a eg or put the file i have given in a array as words and not as single charecters . another thing is can you tell me how can i store the GPA as floating number so that i can compare my array and sort it out accordingly.please dont mind the style in which i have posted as this is my first post .thanking in advance
kesh1000
Junior Poster in Training
70 posts since May 2009
Reputation Points: 10
Solved Threads: 3
hi.....thank you for your post.the thing is im a newb to streams.....we have only done in class up to single arrays and we were asked to do research further to do our assignment.if any of you could provide me with a eg code with a txt file similar to mine. i would realy appreciate it.thanks
kesh1000
Junior Poster in Training
70 posts since May 2009
Reputation Points: 10
Solved Threads: 3
you will need four arrays, one for of the columns in the file.
const int ARRAYSIZE = 1000;
char names[ARRAYSIZE][1];
int id[ARRAYSIZE] = {0};
int age[ARRAYSIZE] = {0};
float gpa[ARRAYSIZE] = {0};
int RowsRead = 0;
ifstream in("file.txt");
while( RowsRead < ARRAYSIZE &&
in >> names[i] >> id[i] >> age[i] >> gpa[i] )
{
++i;
}
// now all the rows in the file are in memory. When you sort the rows you have to swap the rows in all four arrays so that they are kept in sync with each other.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
kesh1000
Junior Poster in Training
70 posts since May 2009
Reputation Points: 10
Solved Threads: 3
sori i thot it was l.....its 1......can u plz tell me wats i as in does i need to be in a for loop o smthng.......in short its undeclared i dont realy know what it is.
kesh1000
Junior Poster in Training
70 posts since May 2009
Reputation Points: 10
Solved Threads: 3
Sorry about that, you are right, variable i is a mistype
while( RowsRead < ARRAYSIZE &&
in >> names[RowsRead] >> id[RowsRead]
>> age[RowsRead] >> gpa[RowsRead] )
{
++RowsRead;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343