punchline code:

Hello everyone I need help with my code please: I can get it to run and compile. The issues is when I added this below the program doesn;t load the error part correctly it stops and doesn't ouput anything. When I take it out it outputs correctly.
if ("joke")
{
cout << "The file " << file1
<< " could not be opened.";
exit(0);
}


#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cctype>

/*Namespace Section*/
using namespace std;

/*Function Prototypes Section*/
void displayAllLines(ifstream &inFile);
void displayLastLine(ifstream &infile);

/*Main section: this is the entry point of the program, which controls the flow of execution*/
int main()
{
string file1;
string file2;
ifstream joke;
ifstream punchline;

//Explain program to user
cout << "This program will print a joke and its punch line.\n\n";

//Get the file names from the user
cout << "Enter the name of the file holding the joke (joke.txt): ";
cin >> file1;

cout << "Enter name of file holding the punch line (punchline.txt): ";
cin >> file2;
//Open the joke file.
joke.open("joke.txt");

// Test for errors.
if ("joke")
{
cout << "The file " << file1
<< " could not be opened.";
exit(0);
}
//Open the punch line file.
punchline.open("punchline.txt");
if ("punchline")
{
cout << "The file " << file2
<< " could not be opened.";
exit(0);
}
cout << endl << endl;

displayAllLines(joke);
displayLastLine(punchline);
cout << endl;
system("PAUSE");
}

void displayAllLines(ifstream &infile)
{
while (infile)
{
string line;
getline(infile, line);
cout << line << "\n";
}
}

void displayLastLine(ifstream &infile)
{
if (infile)
{
//Go to the last character before EOF
infile.seekg(-1, std::ios_base::end);
char ch = ' ';
while (ch != '\n')
{
infile.seekg(-2, std::ios_base::cur); //Two steps back, this means we
//will NOT check the last character
if ((int)infile.tellg() <= 0) //If passed the start of the file, this is the start of the line
{
infile.seekg(0);
break;
}
infile.get(ch); //Check the next character
}
string lastLine;
getline(infile, lastLine);
cout << lastLine << "\n";
}
}

Here is the Sample Joke file: It has to print all of the lines in txt file: Two men who work together in a factory were talking.
"I know how to get some time off," said one.
"How are you going to do that?" asked the other.
"Watch," he said, and climbed a ladder to the ceiling.
The foreman asked what he was doing up there,
and the man replied. "I'm a lightbulb."
"I think you need some time off," the foreman
said, and the first man walked out of the
factory. After a moment, the second man followed
him. "Where do you think you're going?"
the foreman shouted.

Sample Punchline file: Only has to print the last line:

asfasdfasdfasdfsdf
asdfasdfsadfsadfsadf
asdfsadfsdfsdf
"I can't work in the dark," he said.
Last edited on
Hello FSUNole34,

PLEASE ALWAYS 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/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

(joke) and (punchline) are file handles, but after opening the file you are checking a string not the file handle, i.e., the status of the stream. Your if statement should be if (joke) or if (!joke). I think it is the second one. I get confused sometimes and just have to try it both ways.

I will have to look at the rest of the code tomorrow.

Hope that helps,

Andy
closed account (48T7M4Gy)
https://www.experts-exchange.com/questions/24860332/how-do-i-write-a-void-ifstream-infile-function.html
Hello FSUNole34,

Without a sample of the files you are using I have no way of actually test the program. I can set up the program to see if the files are opening, but do not know what it will be reading.

When I tried to compile your program I found that where you used "string" you have misspelled this as "sring".

I created empty files to get the first part working. I did find the if statement should be if (!joke) (no quotes) and I did test reading the file, but only one line.

So the "displayLastLine" function works. Out of a three line file it prints the last line. I am wondering if you display the first line of "joke" why display the last line of "punchline". Should these files be read evenly?

Hope that helps,

Andy
I have changed the if (!joke) and it gives me the error when I run the program, but when I type joke.txt or punchline.txt it gives me the error saying the file couldn't be open!
Hello FSUNole34,

This is your code slightly altered so that I do not have to type in file names each time the program runs. And this is how you should use the code based on your cin" statements.

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
	std::string file1{ "joke.txt" };   // <--- Changed.
	std::string file2{ "punchline.txt" };  // <--- Changed.
	std::ifstream joke;
	std::ifstream punchline;

	//Explain program to user
	std::cout << "This program will print a joke and its punch line.\n\n";

	//Get the file names from the user
	std::cout << "Enter the name of the file holding the joke (joke.txt): ";
	//std::cin >> file1;
	std::cout << file1 << std::endl;

	std::cout << "Enter name of file holding the punch line (punchline.txt): ";
	std::cout << file2 << std::endl;
	//std::cin >> file2;

	//Open the joke file.
	joke.open(file1);  // <--- Changed.

	// Test for errors.
	if (!joke)  // <--- Changed.
	{
		std::cout << "The file " << file1
			<< " could not be opened.";
		exit(0);
	}

	//Open the punch line file.
	punchline.open(file2);  // <--- Changed.

	if (!punchline)  // <--- Changed.
	{
		std::cout << "The file " << file2
			<< " could not be opened.";
		exit(0);
	}


This did not produce any errors for me.

Post the code you have updated and the error messages you receive. Maybe I can duplicate what you have done.

Hope that helps,

Andy
Thank you guys so much especially @Andy you are a life saver! I changed this part only and all the errors are working now! I never thought of changing that at all!

//Open the joke file.
joke.open(file1); // <--- Changed.

// Test for errors.
if (!joke) // <--- Changed.
{
std::cout << "The file " << file1
<< " could not be opened.";
exit(0);
Hello FSUNole34,

You are welcome.

You may find that this code may close the console window when it reaches the "exit(0);".

1
2
3
4
5
6
7
// Test for errors.
if (!joke) // <--- Changed.
{
	std::cout << "The file " << file1
		<< " could not be opened.";
	exit(0);
}


This should work better:

1
2
3
4
5
6
7
8
// Test for errors.
if (!joke) // <--- Changed.
{
	std::cout << "The file " << file1
		<< " could not be opened.";
	std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files "chrono" and "thread".
	exit(1); // <--- Changed.
}


This way it will wait three seconds before the exit.

Also notice I changed exit(0); to exit(1);. Zero usually means that the program ended with no problem and any other number would mean there was a problem. That number could be used by whatever called the program in the first place. Or Some IDEs will put the exit status as the last line of the output, so you will know how the program ended.

Hope that helps,

Andy
Topic archived. No new replies allowed.