I'll show you your version (which does exactly as mine). But I removed the errors. Then I will explain the errors.
Code:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
void wait_for_enter();
struct studinfo
{
string last;
string first;
float final, mid;
float homework[10];
};
int main()
{
// declare all my variables
string tfirst, tlast;
float tmid, tfin, thome[10], gohome;
int i(0);
ifstream in_file( "students.txt" ); // open a file at this location
if( !in_file.is_open() ) // if the program cannot find the file specified
{
cout << "An error occurred while opening the file." << endl;
wait_for_enter();
return 0;
}
vector <studinfo> studentlist;
while( !in_file.eof() ) // will read through each line until end of file
{
in_file >> tlast;
in_file >> tfirst;
in_file >> tfin;
in_file >> tmid;
in_file >> gohome;
i=0;
while( gohome != -1 )
{
thome[i] = gohome;
in_file >> gohome;
i++;
}
studinfo tempstud;
tempstud.last = tlast;
tempstud.first = tfirst;
tempstud.final = tfin;
tempstud.mid = tmid;
i = 0;
while( i < 10 )
{
tempstud.homework[i] = thome[i];
i++;
}
studentlist.push_back(tempstud);
}
//Print the vector (file).
unsigned size = studentlist.size();
for(unsigned i=0; i != size; ++i)
{
cout<<studentlist[i].last<<" "<<
studentlist[i].first<<" "<<
studentlist[i].final<<" "<<
studentlist[i].mid<<" ";
for(unsigned j=0; j<10; ++j)
{
cout<<studentlist[i].homework[j]<<" ";
}
cout<<endl;
}
wait_for_enter();
return 0;
}
void wait_for_enter() // for OS independency
{
cout << "Press <ENTER> to continue...";
// Reset...
cin.clear();
string line;
getline( cin, line );
}
- You didn't reset "i" to 0 in the next "while" code block.
You should change these while-loops to "for statements" to make the type of loop explicit. That's why these loops are for.
Just watch this code block:
Code:
i = 0;
while( i < 10 )
{
tempstud.homework[i] = thome[i];
i++;
}
Do you see that the loop is guaranteed to end if the code "i++" is reached 9 times. This is a typical hint to use for-loops instead! Try to change them into for loops yourself. Or come back for help. No problem :).
- Also look at my code. I am using less variables and use less memory.
- And I loath the usage of "eof()" in these cases. You are processing
past the true end of file now. Never forget that eof() is defined as a file-pointer PAST the true end in C++! In simple occasions that isn't a problem. But nevertheless it is not correct. See how I do it. Or perhaps I could make a mini-mini-mini tutorial in this thread for you to see what's going on with this.
- Don't call your vector "studentlist". A vector is not a list. Call it "studentvec" or something like that.
- By the way.
In my first code I used...
unsigned SIZE = sizeof(temp.homework) / sizeof(*temp.homework);
...instead of the number "10" for arrays. This way you can change the size of the array in the struct without changing all the "10-s" :).
In your corrected code I didn't do that.
What you should try:
1) Try to do it without this:
Code:
string tfirst, tlast;
float tmid, tfin, thome[10], gohome;
2) Change that while-loop into a for-loop.
3) Change the name of studentlist.
4) The eof()-loop we can do together. So nevermind that for now.
5) See if you're interested in changing
float homework[10]; into
vector<unsigned> homework.