undefined ifstream ofstream

On line 6 the compiler found fin and fout to be undefined. I tried calling by reference and no avail. Any ideas?




#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>

void get_file(char in_file, char out_file, ifstream fin, ofstream fout);
/* to get, open, and test the files for input and output data.*/


using namespace std;

int main(int argc, char *argv[])
{
char in_file[40], out_file[40];
ifstream fin;
ofstream fout;
get_file (in_file, out_file, fin, fout);


system("PAUSE");
return EXIT_SUCCESS;
}
void get_file (char in_file, char out_file, ifstream fin, ofstream fout)
{
cout << "What is the name of the file containing data to be analyzed?"
cin >> in_file;
fin.open(in_file);
if (fin.fail())
{
cout << "Input file failed.\n.";
exit(1);
}
cout << "What is the name of the file where you would like to store data? \n"
cin >> out_file;
fout.open(out_file);
if (fout.fail())
{
cout << "imput file failed.\n.";
exit(1);
}

You're trying to use objects that live in the std namespace before you've sorted out the namespaces.
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
38
39
40
41
42
43
44
45
46
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>


// this goes first before referencing classes, objects and functions inside the namespace
using namespace std;

// you forgot char*
// also pass ifstream and ofstream by reference or by pointer
// why?? because its copy constructor is private (can't be copied)
void get_file(char* in_file, char* out_file, ifstream& fin, ofstream& fout);
/* to get, open, and test the files for input and output data.*/

int main(int argc, char *argv[])
{
    char in_file[40], out_file[40];
    ifstream fin;
    ofstream fout;
    get_file (in_file, out_file, fin, fout);


    system("PAUSE");
    return EXIT_SUCCESS;
}

void get_file (char* in_file, char* out_file, ifstream& fin, ofstream& fout)
{
    cout << "What is the name of the file containing data to be analyzed?"; // forgot semicolon
    cin >> in_file;
    fin.open(in_file);
    if (fin.fail())
    {
        cout << "Input file failed.\n.";
        exit(1);
    }
    cout << "What is the name of the file where you would like to store data? \n"; // forgot semicolon
    cin >> out_file;
    fout.open(out_file);
    if (fout.fail())
    {
        cout << "imput file failed.\n.";
        exit(1);
    }
}
Topic archived. No new replies allowed.