40 line code with errors on line 148?

Not sure why it won't compile. Usually gives me errors and highlights the lines that need work. When I try to compile it does not highlight anything. Just prints a lot of messages. Even messages for line 568, which does not exist in this code.



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

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[40], out_file[40], 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);
}
}
well... the first two arguements for get_file are chars (i.e. single bytes) and u passed char arrays. and why r u passing variables with no values?
The purpose of the function is to get the variables values from the user (the array), and use them to open the files. I am not trying to return anything, nor pass the chars with values, but have the chars defined as files in the function by the user and have the files opened(I think?). Not sure were I am going wrong. I changed the arguments into arrays.



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

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?"
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);
}
}
then why pass them? ur overloading the chars and passing variables with no values just declare them in the function
Even messages for line 568, which does not exist in this code.

It may show to line in library file not yours file. And use source code tag for your code.
Last edited on
Good point. So, I did that. Still getting the same response when I try to compile.


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

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


int main(int argc, char *argv[])
{

ifstream fin;
ofstream fout;
get_file (fin, fout);


system("PAUSE");
return EXIT_SUCCESS;
}
void get_file (ifstream fin, ofstream fout)
{
char in_file[40], out_file[40];
cout << "What is the name of the file containing data to be analyzed?\n"
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);
}
}
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
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

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


int main(int argc, char *argv[])
{

     ifstream fin;
     ofstream fout;
     get_file(fin, fout); 


     system("PAUSE");
     return EXIT_SUCCESS;
}
void get_file (ifstream &fin, ofstream &fout)
{
     char in_file[40], out_file[40];
     cout << "What is the name of the file containing data to be analyzed?\n";
     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);
     }
}


Ok. Lines 7 and 22 you should be passing the ifstream and ofstream by reference. You had it correct on an earlier edit. Also you were missing semicolons at on lines 25 and 33. Other than that it looks good.
Thanks, I changed those things, but it is still not compiling. The strange thing to me is that it is not highlighting anything to show were the errors are. It simply prints numerous messages and does not run. Must all type ifstream and ofstream be used as reference in functions?
The fact that I am not getting a highlighted error message makes me wonder if there is a problem with my compiler. Does this happen?
The new code. Still not highlighting errors or compiling. Is that normal.




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

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


int main(int argc, char *argv[])
{

ifstream fin;
ofstream fout;
get_file (fin, fout);


system("PAUSE");
return EXIT_SUCCESS;
}
void get_file (ifstream &fin, ofstream &fout)
{
char in_file[40], out_file[40];
cout << "What is the name of the file containing data to be analyzed?\n";
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 << "Input file failed.\n";
exit(1);
}
}
The code I posted earlier ran just fine using DevC++.

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
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

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


int main(int argc, char *argv[])
{

ifstream fin;
ofstream fout;
get_file (fin, fout); 


system("PAUSE");
return EXIT_SUCCESS;
}
void get_file (ifstream &fin, ofstream &fout)
{
char in_file[40], out_file[40];
cout << "What is the name of the file containing data to be analyzed?\n";
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 << "Input file failed.\n";
exit(1);
}
}


In lines 27 and 35 the in_file and out_file should not be in quotations. This will cause the open function to actually try to open a file called in_file or out_file. Even with those errors it still compiles on DevC++. What compiler are you using?
Topic archived. No new replies allowed.