-
loading into structs
Hi guys...
I am working with a struct for the first time and having trouble just loading my data (from an external file into it).
Can someone take apeek at my Load function and tell me what the outline to load should look like?
I have an ex file that has
first names
last names
4 exam grades
Here's ehat I have so far:
Code:
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
struct Student{
char lastname[20];
char firstname[20];
double exams[4];
double average;
char letgrade;
bool passed;};
const int MAXSTUDENTS = 25;
Student ClassList[MAXSTUDENTS];
//function Prototypes
void Load(Student[], int&);
void setupFiles();
ifstream infile("C:\\My Documents\\Cee_\\HW\\HW_Final\\IFF.txt");
ofstream outfile("C:\\My Documents\\Cee_\\HW\\HW_Final\\OFF.txt");
int main()
{
int ns = 0;
//calls
setupFiles();
Load(ClassList, ns);
infile.close();
outfile.close();
return 0;
}
//set up files fx===========================================
void setupFiles() {
if(!infile){
cerr << "Cannot open input file" << endl;
} //if
if(!outfile){
cerr << "Cannot open output file" << endl;
} //if
}
//Function Load
void Load(Student CL[], int& ns)
{
int y =0;
while (infile >> CL[ns].lastname){
(CL[ns].firstname);
while (y = 0; y < 4; y ++){
(CL[ns].exams[y]);
ns++;
}//while1
}//while2
}
-
You have several options here. First off, I would advise you to not declare your streams globally. Yes, it is convenient, but is bad style, etc.
Next, I would probably re-do your loopng structures. Why not just do:
Code:
while(inflile)
{
// read in values using extraction
}
You can extract all the data for each student each iteration through the loop. Of course, increment the loop each time so you progress though your array of students.
Hope this helps
-
Hi thanks I am going to actually separate the global defs into an .h file and then the functions and main into client .cpp files.
Pretty much where i am confused is as to HOW to structure the loop to get the info IN. The format loop I am using is giving me syntax errors and since I have not done this yet (loading the struc) i am kinda at a loss as to how to do it.
I cannot find any examples and I am taking a class that is
more or less a 'teach yourself' so i have no example from there either.
can you be more specific as to how to read in values using extraction?
i wish i knew what you meant but i don't.
thanks,
Mouse
:confused:
-
Try this on for size:
Code:
int i = 0;
while(infile)
{
infile >> ClassList[i].lastname >> ClassList[i].firstname >> ClassList[i].exams;
i++;
}
That oughta be what you're looking for, assuming that you want to read an entire record each time through the loop. If not, the code will still work, as infile.good() will be false, and the loop will exit. You might want to do some error checking, etc, after the loop. Let me know if this isn't what you need.
-
Code:
while (infile >> CL[ns].lastname)
{
(CL[ns].firstname);
while (y = 0; y < 4; y ++)
{
(CL[ns].exams[y]);
ns++;//////////////////////////////////here's the problem, I think. With this line here each student is only going to get one exam grade in the exams array, with the firtst student getting a report in exams[0] the second student in exams[1], the third in exams[2], the fourth in exams[3], the fifth in exams[0] again, etc. I'll leave it up to you to figure out where to put this line.
This is an example of why I think the coding style displayed here is easier to debug than the one you used, although the one you used is a common style.
}//while1
}//while2
-
trying
I tried something like what you have here...but it deosn't seem like my stuff is laoding...
I thought since there are only 4 exams grades to use a separate loop to load those
but when i try to cout to the screen to see what i have loaded
nothing is showing at all:
plus i am getting an io exception to debugg!
here's the mod version:
Code:
//Function Load
void Load(Student CL[], int& ns)
{
int i = 0;
int y = 0;
while(infile)
{
infile >> CL[i].lastname >> CL[i].firstname;
i++;
for (y = 0; y < 4; y++){
(CL[ns].exams[y]);
}//for
}//while
cout << CL[ns].lastname << endl;
cout << CL[ns].firstname << endl;
cout << CL[ns].exams[y] << endl;
}
thanks so much for the help...
it gets pretty frustrating!
Mouse
-
Hi Elad ---thanks for your input...
i am getting a wierd error code at this line:
Code:
while (y = 0; y < 4; y ++)
it is saying:C:\Program Files\Microsoft Visual Studio\MyProjects\HW_F\HW_F.CPP(91) : error C2143: syntax error : missing ')' before ';'
i don't get it...also it iserroring me at the y loop saying that
Code:
while (int y = 0; y < 4; y ++)
{
(CL[ns].exams[y]);
:\Program Files\Microsoft Visual Studio\MyProjects\HW_F\HW_F.CPP(91) : error C2065: 'y' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\HW_F\HW_F.CPP(91) : warning C4552: '<' : operator has no effect; expected operator with side-effect
i am really confused...I do get the ns++ hint you gave but it's the rest of the loop that seems like it should work to me but doesn't??!!
:eek: :eek: :eek:
-
the double while loop was fine as far as I'm concerned. I would drop the i counter and just go with the ns counter as you had before.
I ask you to think about what ns is representing? It's the number of the given student whose exam grades you will be reading in, correct? In that case, when do you want to change the student number---after a single exam grade is read in, or after all 4 exam grades have been read in? Just move the increment of ns to the apprpriate spot and I think all your troubles are solved, although I don't have a compiler to prove it at the moment.