#include <iostream> #include <fstream> using namespace std; // Functions Prototy
ID: 3554069 • Letter: #
Question
#include <iostream>
#include <fstream>
using namespace std;
// Functions Prototype
void jokeFile(fstream &);
void punchFile(fstream &);
int main()
{
// Create the 2 files
fstream file1,file2;
// Open the files
file1.open("joke.txt", ios::in);
file2.open("punchline.txt", ios::in);
while (!file1.eof())
{
jokeFile(file1);
}
while (!file2.eof())
{
punchFile(file2);
}
// Close the files
file1.close();
file2.close();
system ("pause");
return 0;
}
void jokeFile (fstream &file)
{
while (!file.eof())
{
char line[20];
file.getline (line,21, " ");
cout << line << endl;
}
}
void (punchFile (fstream &file))
{
int i = 3;
char ch;
char line[30];
file.seekg (0L, ios::end);
file.seekg (-3L, ios::end);
ch = file.get();
while (ch != " ")
{
file.seekg (-i, ios::end);
ch = file.get();
i++;
}
file.getline (line, i);
cout << line;
cout << endl;
}
The errors are in bold and below:
punchline.cpp(55): error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize,_Elem)' : cannot convert parameter 3 from 'const char [2]' to 'char'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
There is no context in which this conversion is possible
punchline.cpp(76): error C2446: '!=' : no conversion from 'const char *' to 'int'
There is no context in which this conversion is possible
punchline.cpp(76): error C2040: '!=' : 'int' differs in levels of indirection from 'const char [2]'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Explanation / Answer
try this code:
#include <iostream>
#include <fstream>
using namespace std;
// Functions Prototype
void jokeFile(fstream &);
void punchFile(fstream &);
int main()
{
// Create the 2 files
fstream file1,file2;
// Open the files
file1.open("joke.txt", ios::in);
file2.open("punchline.txt", ios::in);
while (!file1.eof())
{
jokeFile(file1);
}
while (!file2.eof())
{
punchFile(file2);
}
// Close the files
file1.close();
file2.close();
system ("pause");
return 0;
}
void jokeFile (fstream &file)
{
while (!file.eof())
{
char line[20];
file.getline (line,21, ' ');
cout << line << endl;
}
}
void (punchFile (fstream &file))
{
int i = 3;
char ch;
char line[30];
file.seekg (0L, ios::end);
file.seekg (-3L, ios::end);
ch = file.get();
while (ch != ' ')
{
file.seekg (-i, ios::end);
ch = file.get();
i++;
}
file.getline (line, i);
cout << line;
cout << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.