void get_input_output_streams (ifstream & in_stream, ofstream & out_stream); //this functions asks the user for the two file names, opens the files, and returns the input
void number_file (in_stream, out_stream); //this function processes the input file and produces the output file.
int main () {
ifstream in_stream;
ofstream out_stream;
get_input_output_streams (in_stream, out_stream);
number_file (in_stream, out_stream);
return EXIT_SUCCESS;
}
void get_input_output_streams (ifstream & in_stream, ofstream & out_stream) {
string filename;
string fileoutput;
cout << "Enter the name of the text file you want the line numbers for." << endl;
cin >> filename;
cout << "Enther the name of the the file you want to output." << endl;
cin >> fileoutput;
in_stream.open (filename.c_str());
out_stream.open (fileoutput.c_str());
}
void number_file (in_stream, out_stream) {
string line;
int lines = 0;
while (!in_stream.eof) {
getline(in_stream, line);
cout << line <<endl;
if (line == "") {
}
else {
++lines;
cout << lines << "line(s) processed." << endl;
}
}
in_stream.close();
system("pause");
}
the errors
lab7.cc:12:19: error: variable or field ‘number_file’ declared void
void number_file (in_stream, out_stream); //this function processes the input file and produces the output file.
^
lab7.cc:12:19: error: ‘in_stream’ was not declared in this scope
lab7.cc:12:30: error: ‘out_stream’ was not declared in this scope
void number_file (in_stream, out_stream); //this function processes the input file and produces the output file.
^
lab7.cc: In function ‘int main()’:
lab7.cc:18:37: error: ‘number_file’ was not declared in this scope
number_file (in_stream, out_stream);
^
lab7.cc:19:10: error: ‘EXIT_SUCCESS’ was not declared in this scope
return EXIT_SUCCESS;
^
lab7.cc: At global scope:
lab7.cc:31:19: error: variable or field ‘number_file’ declared void
void number_file (in_stream, out_stream) {
^
lab7.cc:31:19: error: ‘in_stream’ was not declared in this scope
lab7.cc:31:30: error: ‘out_stream’ was not declared in this scope
void number_file (in_stream, out_stream) {
yea im a dumbass for that my TA gave me that so i didnt think about that part being wrong but i still get compiler errors.
lab7.cc:11:48: error: expected ‘,’ or ‘...’ before ‘&’ token
void get_input_output_streams (string ifstream & in_stream, string ofstream & out_stream); //this functions asks the user for the two file names, opens the files, and returns the input
^
lab7.cc: In function ‘int main()’:
lab7.cc:17:50: error: could not convert ‘in_stream’ from ‘std::ifstream {aka std::basic_ifstream<char>}’ to ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’
get_input_output_streams (in_stream, out_stream);
^
lab7.cc:18:37: error: could not convert ‘in_stream’ from ‘std::ifstream {aka std::basic_ifstream<char>}’ to ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’
number_file (in_stream, out_stream);
^
lab7.cc:19:10: error: ‘EXIT_SUCCESS’ was not declared in this scope
return EXIT_SUCCESS;
^
lab7.cc: At global scope:
lab7.cc:21:48: error: expected ‘,’ or ‘...’ before ‘&’ token
void get_input_output_streams (string ifstream & in_stream, string ofstream & out_stream) {
^
lab7.cc: In function ‘void get_input_output_streams(std::__cxx11::string)’:
lab7.cc:28:2: error: ‘in_stream’ was not declared in this scope
in_stream.open (filename.c_str());
^
lab7.cc:29:2: error: ‘out_stream’ was not declared in this scope
out_stream.open (fileoutput.c_str());
^
lab7.cc: In function ‘void number_file(std::__cxx11::string, std::__cxx11::string)’:
lab7.cc:34:20: error: ‘std::__cxx11::string {aka class std::__cxx11::basic_string<char>}’ has no member named ‘eof’
while (!in_stream.eof) {
^
lab7.cc:35:26: error: no matching function for call to ‘getline(std::__cxx11::string&, std::__cxx11::string&)’
getline(in_stream, line);
^
In file included from /usr/include/c++/5/cstdio:42:0,
from /usr/include/c++/5/fstream:41,
from lab7.cc:6:
/usr/include/stdio.h:678:20: note: candidate: __ssize_t getline(char**, size_t*, FILE*)
extern _IO_ssize_t getline (char **__restrict __lineptr,
^
/usr/include/stdio.h:678:20: note: candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/5/string:52:0,
from /usr/include/c++/5/bits/locale_classes.h:40,
from /usr/include/c++/5/bits/ios_base.h:41,
from /usr/include/c++/5/ios:42,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from lab7.cc:5:
/usr/include/c++/5/bits/basic_string.h:5212:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
getline(basic_istream<_CharT, _Traits>& __is,
^
/usr/include/c++/5/bits/basic_string.h:5212:5: note: template argument deduction/substitution failed:
lab7.cc:35:26: note: ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ is not derived from ‘std::basic_istream<_CharT, _Traits>’
getline(in_stream, line);
^
In file included from /usr/include/c++/5/string:53:0,
from /usr/include/c++/5/bits/locale_classes.h:40,
from /usr/include/c++/5/bits/ios_base.h:41,
from /usr/include/c++/5/ios:42,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from lab7.cc:5:
/usr/include/c++/5/bits/basic_string.tcc:1513:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, _CharT)
getline(basic_istream<_CharT, _Traits>& __in,
^
/usr/include/c++/5/bits/basic_string.tcc:1513:5: note: template argument deduction/substitution failed:
lab7.cc:35:26: note: ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ is not derived from ‘std::basic_istream<_CharT, _Traits>’
getline(in_stream, line);
^
lab7.cc:44:13: error: ‘std::__cxx11::string {aka class std::__cxx11::basic_string<char>}’ has no member named ‘close’
in_stream.close();
^
lab7.cc:45:17: error: ‘system’ was not declared in this scope
system("pause");
void get_input_output_streams (char ifstream& in_stream, char ofstream& out_stream);
void number_file (char in_stream, char out_stream);
idk what im doing wrong but thanks for the help ive tried all i know how to do.
Line 8,27: Your function prototype and definition are still incorrect. The type of in_stream and out_stream are not char.
Hint: The argument types should be the same as get_input_output_streams().
Line 30: eof is a function call. it needs ().
Line 30: Do not loop on (! stream.eof()) or (stream.good()). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> (or getline) operation as the condition in the while statement.
1 2 3
while (stream >> var) // or while (getline(stream,var))
{ // Good operation
}
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.