Hello again,

I will go straight to the point. I have two files one that is called student.dat and the other is called grades.dat .
Student.dat has the following items in it :

20092112 Anthony Haykal
20084564 John Doe
20075640 James Bond
20045678 Cynthia Smith
20092134 Jennifer Hajj

grades.dat has these :
20045678 75 80 85
20092134 64 51 84
20075640 57 65 90
20092112 80 90 85
20084564 85 46 68

The question asks me to read the data from “grades.dat” file and print on the screen the Student’s ID and Students’s Name (first name, last name) and the total grade.
You will find the firstname and the lastname in the “student.dat” file. (Note that
the files “student.dat” and “grades.dat” are NOT in the same order.)

I just have a problem with the order. I mean how do i make like id number 20092112 to print the name and final name in this case Anthony Haykal
while showing the grades also. If i read them in sequential order i cant like point to this specific id. I arrived here and got stuck :s

#include <iostream>
#include <fstream>
using namespace std;
int main()
{//start main
        int id;
        int id2;
        
        
        char FirstName[30];
        char LastName[30];
        int AssignmentGrade;
        int MidTermGrade;
        int FinalGrade;
        int student[5];


        ifstream infile("grades.dat" , ios::in);
        ifstream infile2("student.dat", ios::in);
        if (!infile||!infile2)
        {
        cout<<"Could not open the file(s)";
        return 1;
        }

        

        while (infile>>id>>AssignmentGrade>>MidTermGrade>>FinalGrade)
                                {//first while
        


while(infile2>>id2>>FirstName>>LastName)
        
{//second while


if (id == id2)
{
cout<<FirstName<<endl;
}
else            if (id != id2)
                        cout<<"Not same id"<<endl;

                



}//second while




        }//first while

        cout<<endl<<endl;




infile.close();
infile2.close();



}//end main

Could you aid me with this issue please!
Thank you

If the number of students is not too big, you can load them all into memory. Then it is easy to work with non-sequential data.

#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

struct Student {
    string Id;
    string Name;
    vector<int> Grades;

    Student() {}
    Student(string id, string name): Id(id), Name(name) {}
};

int main()
{
    istringstream iss_students(
        "20092112 Anthony Haykal\n"
        "20084564 John Doe\n"
        "20075640 James Bond\n"
        "20045678 Cynthia Smith\n"
        "20092134 Jennifer Hajj");
    istringstream iss_grades(
        "20045678 75 80 85\n"
        "20092134 64 51 84\n"
        "20075640 57 65 90\n"
        "20092112 80 90 85\n"
        "20084564 85 46 68");
    map<string, Student> students;
    string id, name;

    while (iss_students >> id >> ws) {
        getline(iss_students, name);
        students[id] = Student(id, name);
    }

    while (iss_grades >> id >> ws) {
        int grade;

        // Too clever? Should have used stringstream to parse? Yuppers
        while (iss_grades >> noskipws >> grade) {
            char next = iss_grades.peek();

            students[id].Grades.push_back(grade);
            iss_grades >> ws;

            if (next == '\n')
                break;
        }
    }

    map<string, Student>::const_iterator it = students.begin();
    map<string, Student>::const_iterator end = students.end();

    while (it != end) {
        cout << it->first << " -- " << it->second.Name << '\n';

        cout << '\t';
        for (vector<int>::size_type i = 0; i < it->second.Grades.size(); i++)
            cout << it->second.Grades[i] << ' ';
        cout << '\n';

        ++it;
    }
}

i am sorry but we still didnt take vectors i did not understand what u wrote :s thanks though!

Well, try to understand it and you will probably learn something. :) Ed used the STL because it is faster for writing sample code, but you can use anything you want; the principle does not change.

Save the students into a list or array of some kind and search the list while reading grades.dat to populate the grades for the right student.

you are right I will try to check it out more thanks for the reply again!

i solved it finally but im faced with another problem:

#include <iostream>
#include <fstream>
using namespace std;

void StudentId(int);
void StudentBelowAverage(int, int , int);
int main(int)
{//start main
        int id;

        int AssignmentGrade;
        int MidTermGrade;
        int FinalGrade;
     


        ifstream infile("grades.dat" , ios::in);
        ifstream infile2("student.dat", ios::in);
        if (!infile||!infile2)
        {
        cout<<"Could not open the file(s)";
        return 1;
        }

        

        while (infile>>id>>AssignmentGrade>>MidTermGrade>>FinalGrade)
                             {//first while
        
								 StudentId(id);

								 }//first while


		



infile.close();
infile2.close();



}//end main

void StudentId(int a)
{
	    int id;
        int id2;  
        char FirstName[30];
        char LastName[30];
		int AssignmentGrade;
        int MidTermGrade;
        int FinalGrade;
		double totalgrade = 0 ;
		double average=0;
		float total=0;
		float count=0;

  

ifstream infile2("student.dat", ios::in);
ifstream infile("grades.dat", ios::in);

while(infile2>>id2>>FirstName>>LastName)
{//first while
if (a==id2)
{//if
//cout<<id2<<" "<< FirstName<<" "<< LastName<<endl;
while (infile>>id>>AssignmentGrade>>MidTermGrade>>FinalGrade)

{//second while
if(a==id)
{


	cout<<id<<" "<<FirstName<<" "<<LastName<<" "<<AssignmentGrade<<" "<<MidTermGrade<<" "<<FinalGrade<<" ";
	
	 totalgrade = ((AssignmentGrade*25)/100)+((MidTermGrade*35)/100)+((FinalGrade*40)/100);
	 cout<<endl<<"Total Grade:"<<totalgrade<<endl<<endl;
	 
	 

}//if

total+= MidTermGrade;
count+=1;

}//second while

	
}//if

}//first while

average = total/count;
cout<<"Average is: "<<average<<endl;

StudentBelowAverage( average,  MidTermGrade,  id);



}

void StudentBelowAverage(int a , int m , int i)
{
if (m<a)

{
ofstream outfile("low.dat" , ios::out | ios::binary);
cout<<"ID: "<<m<<" "<<i<<endl;

}

}

i need to write to file low.dat the id and midtermgrade of the students that have their midterm to be less than average. All i am getting is the same id and same midterm grade. I cant seem to fix it. could anyone help plz!

This question has already been answered. Start a new discussion instead.