C++ Peeps, how do I impletement datafiles into my code? (fstream.h, infile..etc)

MikeMAN

Senior member
Oct 26, 1999
743
6
81
#include <iostream.h>


int GetID (int IDNum)
{
cout<<"Enter Student ID (-1 to stop): ";
cin>>IDNum;
return IDNum;
}

void GetData(int & nCreditSum, double & dGradePointSum)
{

double dGradePoint;
int nCredit;

nCreditSum=0;
dGradePointSum=0;

cout<<"Please enter the number of credits (enter 0 to stop): ";
cin>> nCredit;

while(nCredit!=0)
{
cout<<"Please enter the grade earned in this course: ";
cin>>dGradePoint;
dGradePointSum=dGradePointSum+dGradePoint;
nCreditSum=nCreditSum+nCredit;
cout<<"Please enter the number of credits (enter 0 to stop): ";
cin>>nCredit;
}

}

double CalcGPA (int nCreditSum, double dGradePointSum)
{
double GPA=0;

if (nCreditSum==0)
{
GPA=0;
}
else
{
GPA=(dGradePointSum/nCreditSum);
}
return GPA;

}

void ProcessStudent (int IDArr[100], double GPAArr[100], int & NumStu)
{



int IDNum;
int nCreditSum;
double dGradePointSum,dGPA;

cout<<"Enter the Number of Students ";
cin>>NumStu;
for(int x=0;x<NumStu;x++)
{
IDArr[x] = GetID(IDNum);
GetData(nCreditSum,dGradePointSum);
GPAArr[x] = CalcGPA(nCreditSum,dGradePointSum);
}


}

void Output(int IDArr[], double GPAArr[], int NumStu)
{
for(int x=0;x<NumStu;x++)
{
cout<<IDArr[x]<<" "<<GPAArr[x]<<endl;
}
}

void main()
{
int IDArr[100];
double GPAArr[100];
int NumStu;

ProcessStudent(IDArr,GPAArr,NumStu);
Output(IDArr,GPAArr,NumStu);

}

i want the following program to have a

char datafile[30];
cout<< "Enter the name of the datafile" <<endl;
cin >> datafile;

to load a datafile, but im not sure how to declare everything(where to put instream, infile.open(filename) infile.close, etc). its been a while since i've done C++

any help is appreciated!

thanks again,
mike
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
i want the following program to have a

char datafile[30];
cout<< "Enter the name of the datafile" <<endl;
cin >> datafile;

First, I would recommend you use a string instead of char array, I know I tend to pass paths longer than 30 chars to some programs.

Second, What are you trying to ultimately do? Would you like to automate the program by having it read a file with the student data instead of having it all be interactive?
 

MikeMAN

Senior member
Oct 26, 1999
743
6
81
yup, i wrote the program as interactive, just to get a baseline, to get it to work and debugged.

now i just wanna have 1 cin>>filename.....have it ask "type in the datafile", user types in "data5.dat" then it automates....

thanks for ur help!,
mike