File cannot be opened

Write a program that reads students’ names followed by theirtest scores. The program should output each students’ namefollowed by the test scores and relevant grade. It should also findand print the highest test score and the name of the studentshaving the highest test score.
Student data should be stored in a struct variable of typeStudentType, which had four components:studentFName and studentLName of typestring,
testScore of type int (testScore isbetween 0 and 100), and
grade of type char.
Suppose that the data has 20 students. Use an array of 20components of type studentType.
Program must contain at least the following functions:
A function to read the students data into the array
A function to assign the relevant grade to each student
A function to print the names of the students having the highesttest score.
Program must output each students name in this form: Last name,followed by a comma, followed by a space, followed by first name,and the name must be left justified. Moreover, other than declaringthe variables and opening the input and out files, the functionmain should only be a collection of function calls.

The following program compiles without erors, but in the terminal window it says file cannot be opened. Please tell me where to edit and what to edit

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

const int ARRAY_SIZE = 20;

struct studentType
{
string studentFName, studentLName;
char grade;
int testScore;
}students[20];

bool openinfile(ifstream& inFile, char filename[]);
void openoutfile(ofstream& outFile, char filename[]);
int readData(ifstream& inFile, studentType students[]);
void getgrade(studentType students[], int n);
void printData(ofstream& outFile, studentType students[], int n);
void highScore(ofstream& outFile, studentType students[], int n);

int main()
{
bool good;
int n;
ifstream infile;
ofstream outfile;
good = openinfile(infile, "Afham.txt");
if (good)
{
openoutfile(outfile, "output.txt");
n = readData(infile, students);
getgrade(students, n);
printData(outfile, students, n);
highScore(outfile, students, n);
outfile.close();
infile.close();
}
system("pause");
return 0;
}
void openoutfile(ofstream& outFile, char filename[])
{
outFile.open(filename);
}
bool openinfile(ifstream& infile, char filename[])
{
infile.open(filename);
if (infile.fail())
{
cout << "file did not open please check it\n";
system("pause");
return false;
}
return true;
}
int readData(ifstream& inFile, studentType students[])
{
int n = 0;
inFile >> students[n].studentFName >> students[n].studentLName >> students[n].testScore;
while (inFile)
{
n++;
inFile >> students[n].studentFName >> students[n].studentLName >> students[n].testScore;
}
return n;
}
void getgrade(studentType students[], int n)
{
int i;
for (i = 0; i<n; i++)
switch ((int)(students[i].testScore / 10))
{
case 10:
case 9: students[i].grade = 'A';
break;
case 8: students[i].grade = 'B';
break;
case 7: students[i].grade = 'C';
break;
case 6: students[i].grade = 'D';
break;
default: students[i].grade = 'F';
break;
}

}
void printData(ofstream& out, studentType students[], int n)
{
out << "name score grade\n";
for (int i = 0; i<n; i++)
{
out << left << students[i].studentLName << "," << students[i].studentFName << " \t";
out << students[i].testScore << "\t" << students[i].grade << endl;
}

}
void highScore(ofstream& out, studentType students[], int n)
{
int max = 0, i;
for (i = 1; i<n; i++)
if (students[i].testScore>students[max].testScore)
max = i;
out << "Highest test score: " << students[max].testScore << endl;
out << "Students receiveing highest score:" << endl;
for (i = 0; i<n; i++)
if (students[i].testScore == students[max].testScore)
out << students[i].studentLName << "," << students[i].studentFName << endl;

}
Last edited on
Please tell me where to edit and what to edit

Your program contains the correct instructions to the user in function openinfile(),

 
        cout << "file did not open please check it\n";


That is to say, the program could not find the file "Afham.txt" - you need to check that it exists, and is in the correct directory. Usually this will be the same directory as that of the executable program (not where you put the source code).




Works fine on my PC, make sure that the file is in the right directory.
To get a better error message you can use perror:
1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
bool openinfile (ifstream& infile, char filename[])
{
  infile.open (filename);
  if (infile.fail ())
  {
    perror ("\nFile error: ");
    system ("pause");
    return false;
  }
  return true;
}


If you use Visual Studio the file must be in the same directory as the solution - if you run it inside VS.

If you run it from the shell it must be in the same directory as the .exe
I use VS and i placed the file in the right directory. Now the problem is fixed but when I run the program it doesn't display anything.
when I run the program it doesn't display anything.

Check the contents of the output file "output.txt".
Suggestion: change the header of the output functions from
 
void printData(ofstream& out, studentType students[], int n)
to
 
void printData(ostream& out, studentType students[], int n)

Do the same with highScore() - ostream instead of ofstream.

Then you will be able to write within main() like this:
1
2
3
4
5
        printData(outfile, students, n);
        highScore(outfile, students, n);
        
        printData(cout, students, n);
        highScore(cout, students, n);
I did everything, It still doesn't display anything. So in "Afham.txt" I have all the student information like names and test scores and it is in the correct directory. What do I do with output.txt ?
Last edited on
Are you reading the data correctly ?
For testing you should print the data you read to the console.
What data does the input contain?
kakrot97 wrote:
What do I do with output.txt ?
Open it in your favourite text editor: notepad, wordpad, even word would do.

Thomas1965 wrote:
What data does the input contain?
Yes, please show us the contents of you input file "Afham.txt".
Contents of 'Afham.txt'

Bob Smith 87
Mike Christy 90
Steven Michaels 33
Michael John 78
Amy Clark 99
Nicole Hasty 91
Bob Robert 89
Chris Nash 77
Chris Thomas 67
Joe Tea 93
Bob Young 87
Mike Stevenson 95
Trent Fiore 70
Anthony Joe 82
Matt Coffee 90
Mark Tomlinson 89
Phil Rope 94
Mike Stopper 88
Mark Robinson 78
Betty Lee 77
I get the following output:
name score grade
Smith,Bob       87      B
Christy,Mike    90      A
Michaels,Steven         33      F
John,Michael    78      C
Clark,Amy       99      A
Hasty,Nicole    91      A
Robert,Bob      89      B
Nash,Chris      77      C
Thomas,Chris    67      D
Tea,Joe         93      A
Young,Bob       87      B
Stevenson,Mike  95      A
Fiore,Trent     70      C
Joe,Anthony     82      B
Coffee,Matt     90      A
Tomlinson,Mark  89      B
Rope,Phil       94      A
Stopper,Mike    88      B
Robinson,Mark   78      C
Lee,Betty       77      C
Highest test score: 99
Students receiveing highest score:
Clark,Amy
Press any key to continue . . .


Note, I made the changes mentioned in a previous post:
http://www.cplusplus.com/forum/beginner/194507/#msg935740

The original code outputs simply:
Press any key to continue . . .
however the output file is still generated properly.
Last edited on
Thanks a lot guys, finally got it. Appreciate it
Topic archived. No new replies allowed.