-2

Okay so I know this program has a lot of problems at the moment. Im going to figure it out soon. But my main concern is why I can not open a simple txt file to read the info in it. this is what I have. I need it to be read right away and cannot figure it out.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;



int main()
{
    readStuData(file, students, scores, id, tooMany);
    studstruct(id, scores, studnum);
    getavg (counter, sum, scores, avg, students);
    getgrade(counter, students, scores, avg, grade);
    PrintTable(counter, students, id, scores, grade);

}

void readStuData(string file,int& students, int scores[MAX], int id[MAX], bool         &tooMany)
{
    cout << "Enter the file name: ";
    cin >> file;

    infile.open(file.c_str());

    if (infile.fail()) cout << "Error. Cannot open " << file;

    while (!infile.eof())
    {
        infile >> students;
        counter++;
    }

    students = counter;

    tooMany = (students > MAX);

    if (tooMany == true) cout << "There are too many students.";

    for (counter = 0; counter < students; counter++)
    {
        infile >> id[counter] >> scores[counter];
    }

}

void studstruct(int id[MAX], int scores[MAX], int studnum[MAX])
{

    for (counter = 0; counter < students; counter++)
    {
        struct student;
        {
            int id[MAX];
            int scores[MAX];
            int studnum = counter + 1;
        }; stu[MAX];
    }

}

void getavg(int counter, float sum, int scores[MAX], float avg, int students)
{
    for (counter = 0; counter < students; counter++)
    {
        sum = scores[counter] + sum;
    }

    avg = sum / students;

}

void getgrade(int counter, int students, int scores[MAX], float avg, char grade[MAX])
{
    for (counter = 0; counter < students; counter++)
    {
        if (scores[counter] <= avg + 10 && scores[counter] >= avg - 10) grade =     "Satisfactory";
        else if (scores[counter] > avg + 10) grade = "Outstanding";
        else if (scores[counter] < avg - 10) grade = "Unsatisfactory";
    }
}

void PrintTable(int counter, int students, int id[MAX], int scores[MAX], char     grade[MAX])
{
    for (counter = 0; counter < students; counter++)
    {
        cout << "ID         Score           Grade\n";
        cout << "----------------------------------";
        cout << id[counter] << "        " << scores[counter] << "           " <<     grade[counter];
    }
}
user3437460
  • 17,253
  • 15
  • 58
  • 106
JakeT
  • 1
  • 1
  • 1
    did u enter an absolute or relative path? Can you cat the file in the shell? – pm100 Nov 13 '14 at 18:57
  • 1
    Check that the inputed file exists. Also check the working directory – Synxis Nov 13 '14 at 18:58
  • 1
    where is infile declared as an ifstream object? – user3288829 Nov 13 '14 at 18:59
  • Sorry I erased all the declarations from this. I do have them in the program though. And the .txt file is made in the same directory as the program. – JakeT Nov 13 '14 at 19:02
  • 1
    @JakeT If you want help finding a bug in your program, its usually counter-productive to introduce more bugs in your example. Us nitpickers get distracted... – user3288829 Nov 13 '14 at 19:05
  • 1
    If you only posted the code that was in any way relevant to reading the file – Zéychin Nov 13 '14 at 19:09
  • ifstream infile; ofstream outfile; string file; const int MAX = 30; int scores[MAX]; int id[MAX]; int stu[MAX]; float avg; float sum; struct student; char grade[MAX]; int counter = 0; int students; int studnum[MAX]; bool tooMany; – JakeT Nov 13 '14 at 19:11
  • these are my declarations. It wouldnt let me post them before. Im new to this guys. cut me some slack. – JakeT Nov 13 '14 at 19:12
  • see http://stackoverflow.com/questions/17337602/how-to-get-error-message-when-ifstream-open-fails – pm100 Nov 13 '14 at 19:27

1 Answers1

1

Where is your declaration for infile?

If you just want to have a feel how to read a file. You can try this.

void readFile(char filename[])
{
    ifstream infile;
    infile.open (filename);

    if (!infile.good())
        cout << "Can't open file." << endl;    

    string data; 
    while (!infile.eof())
    {
        getline(infile, data);    //read one line of data file content at a time
        cout << data << endl;     //Display file content line by line (for testing)
    }
    infile.close();
}
user3437460
  • 17,253
  • 15
  • 58
  • 106
  • 2
    Of course, you're example has the same problem his original code did. You're accessing `data` after the `getline`, without checking that `getline` worked. (What's wrong with `while ( getline( data ) )`?) – James Kanze Nov 13 '14 at 19:45
  • Agree with @JamesKanze don't use `while (!infile.eof())` it's bad – A. D. Nov 13 '14 at 20:03