structure and file

Hi there, I have to create a structure which includes student last name, his average mark and cipher whose first 2 letters indicates faculty in which student is studying.I have to read the data from a file and find a group of people from 1 faculty and sort them according to they average mark from biggest to smallest. Can anyone help? Maybe you have done familiar task? The hardest thing for me to read the file with structure and find people from 1 faculty.
The structure is like that

[struct studentas{
char lastname[30];
float aver;
char group[10];
};]
and the file example would be like that: Collins 8.5 PFSU
Johns 5.4 EFSU
and so on. PF stands for Physics faculty and the further text doesn't matter.
@WilliamMorris
Maybe you have done familiar task?

Yes, I have done a familiar task just like you.
How to read text from file and how to find students from 1 faculty?
WilliamMorris - if you read enough posts on this board you'll find that help is not usually given unless the OP demonstrates some tangible effort of his/her own. Copying/pasting the original assignment does not count as such
I'm not asking to write a whole program. I'm asking to explain how to do it, a hint I guess, if a person writes a whole program it's not my problem. I have only written which parts I don't understand( read the file with structure and find people from 1 faculty).
Here's one of my recent posts about reading files into structs:

http://www.cplusplus.com/forum/beginner/201566/
Why it wont print anything in new file?
[#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>
using namespace std;

struct student{
string lastname;
float ave;
string grp;
};
void reading(student s,int n);

void printing(student s,int n);

int main()
{
int n;
student s;

reading(s,n);

printing(s,n);
return 0;
}


void printing(student s,int n){
ifstream fd("grupes.txt");

for(int i=0;i<n;i++)
fd>>s.lastname>>s.ave>>s.grp;

fd.close();

}
void printing(student s,int n){
ofstream fr("rez.txt");
for(int i=0;i<n;i++){


fr<<s.lastname<<s.ave<<s.grp<<endl;


}

fr.close();

}


]
Last edited on
Your appear to have defined two functions called
void printing(student s,int n)
and I would suspect the first one (which precedes the ifstream fd line) should probably be
void reading(student s, int n)

It then goes into a loop from i=0 to n-1; however, you don't appear to have set n anywhere. It is not clear whether you intend to pass that from main() or read it from file or do something else.

So the short answer to your question is that it won't print anything in a new file because it didn't read anything in an old file.

That said, you do seem to have taken gunnerfunner's advice and the code is beginning to look structured.
Last edited on
Well, I made the first mistake because i wanted to rewrite the program in english and i havent noticed that and one of my biggest problems is not the understanding but the lack of knowledge.. I haven't done anything with structures and programming for couple years :D
Topic archived. No new replies allowed.