0

I have a program for my class to read in information from a file, as an array, using a function and I've coded this the same way I did for classes last year and I'm not sure why it's not working. I'm supposed to also add more to this but wanted to try to figure out why it's not working with what it already has.

I don't think it's reading the file in because nothing comes out on the output file or the window that pops up when it runs.

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int maxs = 50;
struct stype
{
    int crn;
    string name;
    int crhrs;
    int numstu;
    int stucrhrs;
    string prof;
};
stype initrec = { 0.0, "course", 0.0, 0.0, 0.0, "prof" };

void initem(stype p[], int &numc)
{
    int i;
    for (i = 0; i < maxs; i++) p[i] = initrec;
    numc = 0;
}
void readem(stype p[], int &numc)
{
    int i = 0;
    ifstream fin;
    fin.open("program1.dat");
    while (!fin.eof())
    {
        fin >> p[i].crn >> p[i].name >> p[i].crhrs >> p[i].numstu >> p[i].stucrhrs   >> p[i].prof;
        i++;
    }
    numc = i;
    cout << "readem reached " << endl;
}
void printem(stype p[], int &numc, ofstream &fout)
{
    int i;
    for (i = 0; i < numc; i++)
    {
        fout << left << setw(10) << p[i].crn << left << setw(15) << p[i].name << left
            << setw(15) << p[i].numstu << right << setw(2) << p[i].crhrs << right <<
            setw(2) << p[i].stucrhrs << right << setw(10) << p[i].prof << endl;
    }
    cout << "printem reached " << endl;

}
void swapem(stype &a, stype &b)
{
    stype temp;
    temp = a;
    a = b;
    b = temp;
    cout << "swapem reached " << endl;
}
void sortem()
{
    cout << "sortem reached " << endl;
}
void getaverage()
{
    cout << "getaverage reached " << endl;
}
void credithours()
{
    cout << "Credit hours totalled. " << endl;
}
void main()
{
    int crn[maxs], crhrs[maxs], stucrhrs[maxs];
    string name[maxs], prof[maxs];
    stype p[maxs];
    int numc;
    ofstream fout;
    fout.open("program1.out");
    fout.setf(ios::fixed);
    fout.precision(2);
    initem(p, numc);
    readem(p, numc);
    printem(p, numc, fout);
    getaverage();
    sortem();
    printem(p, numc, fout);
    system("pause");
}
  • You should definitely have something that checks if the file opened correctly. – yizzlez Aug 23 '14 at 16:41
  • 5
    First of all, don't do `while (!fin.feof(...))`, it will not work as you intend. Secondly, you need to check that the file is actually opened properly. You can fix both by having the input operations as the loop condition (like `while (fin >> ...)`). – Some programmer dude Aug 23 '14 at 16:42
  • I just tried using `while (fin >> p[i].crn >> p[i].name >> p[i].crhrs >> p[i].numstu >> p[i].stucrhrs >> p[i].prof) ` and it's still not working. The window comes up with my cout statements I have in but nothing is happening with the output file. – Dankstuff420 Aug 23 '14 at 17:21
  • `ifstream fin("filename.ext"); if(!fin) throw std::runtime_error("failed to open filename.ext");` Use this to make sure that your program successfully opened the file (dito for the output file). Note that this will cause an abnormal termination unless you catch the exception, so the nonportable `system("pause");` won't do its job to keep open the terminal window. – Ulrich Eckhardt Aug 23 '14 at 17:53
  • Just got it to read in the file, I was trying to read in too many p[i] than the file had but it now shows up on the output file! I'll have to go in and start doing my calculation functions but at least it does read in the file now. Yay! `void readem(stype p[], int &numc) { int i = 0; ifstream fin; fin.open("program11.dat"); while (!fin.eof()) { fin >> p[i].crn >> p[i].name >> p[i].crhrs >> p[i].numstu >> p[i].prof; i++; } numc = i; cout << "readem reached " << endl; }` works to read the file in. And the window does stay up for me with the `system("pause");`. – Dankstuff420 Aug 23 '14 at 18:07
  • Seriously. Can't stress this enough: [**`while (!fin.eof())` is wrong**](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – WhozCraig Aug 24 '14 at 03:16

0 Answers0