24: error: invalid conversion from ‘char’ to ‘const char*’

I keep getting the above error message, and I can't seem to figure out what's wrong. I also get this error:
24: error:   initializing argument 1 of ‘void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]’

I figure the two are related. Here's my code. (There's another function after all this, but it's not relevant, so I didn't include it.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <fstream>
using namespace std;
int *readFile (char, int&);
double computeAverage (int*, int, double&);
int main ()
{
    char fileName [20];
    int sum=0;
    int SIZE=0;
    cout << "Input file name: ";
    cin >> fileName;
    int *intptr;
    intptr=readFile (fileName, SIZE); 
    cout << "Number of students: " << endl;
    cout << "Total number of movies watched by students: " << sum << endl;
    cout << "Average number of movies watched by students: " << computeAverage(intptr, SIZE, &sum) << endl;
}
int *readFile (char fileName, int &SIZE)
{
    ifstream inFile;
    inFile.open (fileName);
    if (!inFile) 
    {
        cout << "Error opening file." << endl;
    }
    inFile >> SIZE;
    int *ptr;
    int num;
    ptr=new int [SIZE];
    for (int i=0; i<SIZE; i++) 
    {
        inFile >> num;
        *(ptr+i)=num;
    }
    
}


Thank you!
closed account (zb0S216C)
EricaFH wrote:
int *readFile (char fileName, int &SIZE)

The fileName parameter is a single char, not a string. A solution would be to declare fileName as a pointer to a char:

1
2
3
int *readFile (char *fileName, int &SIZE)
// Or:
int *readFile (char fileName[], int &SIZE)

Wazzak
Last edited on
Make sure it is const char *
That fixed it. Thank you!
Topic archived. No new replies allowed.