Help with an error learning arrays for a c++ class

Nov 29, 2013 at 10:38pm
I am doing this for a class project. We are supposed to open a data file p6.dat and read the data which includes 15 scores (3 tests, 5 students). I am getting an error online 20 from the compiler. Could anyone direct me as far as what I can do to fix this? Thank you!

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

using namespace std;

int main()

{
ifstream inputFile;
const int NumbofStudents = 5;
const int NumbofTests = 3;
string fileName;
double Scores [NumbofStudents] [NumbofTests];

cout << "Please enter the name of the file to read: " << endl;
cin >> fileName;

inputFile.open(fileName);

for(int x = 0; x < NumbofStudents; x++)
{
for (int y = 0; y < NumbofTests; y++)
{
inputFile >> Scores[x][y];
}
}

inputFile.close();



cout << "Please enter the name of the file to read: " << endl;
cin >> fileName;

inputFile.open(fileName);

for(int x = 0; x < NumbofStudents; x++)
{
for (int y = 0; y < NumbofTests; y++)
{
inputFile >> Scores[x][y];
}
}

inputFile.close();


cout << "These are the averages of the five students: " << endl;

for (int x = 0; x < NumbofTests; x++)
{
double studentSums = 0;
for (int y = 0; y < NumbofTests; y++)
{
studentSums = studentSums + Scores[x][y];
}
double studentAvg = studentSums / NumbofTests;

cout << setiosflags(ios::fixed) << setprecision(2) << studentAvg;
}

cout << " These are the averages for each of the three tests: " << endl;

for(int x = 0; x < NumbofTests; x++)
{
double testSum = 0;
for(int y = 0; y < NumbofStudents; y++)
{
testSum = testSum + Scores [x][y];
}
double testAvg = testSum / NumbofStudents;

cout << setiosflags(ios::fixed) << setprecision(2) << testAvg;
}


double highest = 0;
double numbofAs = 0;

for (int x = 0; x < NumbofStudents; x++)
{
for (int y = 0; y < NumbofTests; y++)
{
if (Scores[x][y] > highest)
highest = Scores[x][y];
if (Scores[x][y] >= 90)
++numbofAs;
}
}

cout << "The best score was: " << highest << endl;

cout << " There were " << numbofAs << " total A's." << endl;

system("pause");

return 0;
}



This is the error message:

ajanch8project.cpp: In function `int main()':
ajanch8project.cpp:20: error: no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::open(std::string&)'
/usr/local/gcc3/bin/../lib/gcc/sparc-sun-solaris2.8/3.4.5/../../../../include/c++/3.4.5/fstream:570: note: candidates are: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]

I am assuming it has something to do with not being able to open the file, but I am not sure how to fix. Any assistance would be appreciated. Thank you!
Last edited on Nov 29, 2013 at 10:41pm
Nov 29, 2013 at 11:43pm
Guys, please learn how to use code tags around your code. Makes things much easier to read.

You need to provide open() with a c-style string. use the c_str() accessor of the string class like so:

inputFile.open(fileName.c_str());
Nov 30, 2013 at 12:43am
Thank you. Can you direct me to a site or section with in this site that will show me how to use code tags like you have requested? Thanks!
Nov 30, 2013 at 1:11am
Use [code] before your code, and [/code ] after your code.
(Leave the trailing space out before the final bracket.)
Topic archived. No new replies allowed.