Hello Everyone,
I am new to c++ and am having trouble with a program that I am working on. The program involves outputting a joke and punchline to the user from associated text files. The joke file should display the entire contents of the file, while the punchline file should only display the last line of text (the prior lines are random characters that are not meant to be read. The problem I am experiencing is that the joke file content outputs on numerous lines, when it should all be on the same line. Here is the content of the joke file...
I started a band called 999 megabytes...
It is outputting as follows...
I
started
a
band
called
999
megabytes...
The punchline file is reading from the last row, but only displaying the last word in the row. Here is the contents of the file...
asfasdfasdfasdfsdf
asdfasdfsadfsadfsadf
asdfsadfsdfsdf
We haven't gotten a gig yet.
Here is what is outputting to the screen...
yet.
I have checked the file in notepad++ for any odd carriage return line feeds, but none are present that would explain this. Any assistance is tremendously appreciated, as I have been plugging away at this for hours to no avail.
Here is my code...
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
/*Include Section*/
#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;
char decision;
char y;
char n;
/*Beginning of program. Prompts user, asking them if they are ready to proceed. If yes, will display the joke\punchline. If no,
ends program sequence*/
cout << "*******************************************************************************" << endl;
cout << setw(48) << "Punchline Program" << endl;
cout << "*******************************************************************************" << endl;
cout << endl;
cout << "Welcome to the Punchline Program!" << endl;
cout << "Are you ready to hear a joke? (y or n): ";
cin >> decision;
if (decision == 'y')
{
cout << endl;
cout << "Great! Prepare to laugh!" << endl;
cout << endl;
}
else if (decision == 'n')
{
cout << endl;
cout << "Ah, no sense of humor, I see. Time to make like a tree and leaf (queue rimshot)!" << endl;
exit(EXIT_FAILURE);
}
/*When user chooses "y", the following opens joke and punchline text files, outputting them to the user. The punchline file will
only display the last line of the file to the user*/
joke.open("joke.txt");
punchline.open("punchline.txt");
cout << endl;
displayAllLines(joke);
displayLastLine(punchline);
cout << endl;
system("PAUSE");
}
void displayAllLines(ifstream &infile)
{
string text;
while (infile >> text)
{
cout << text << endl;
}
}
void displayLastLine(ifstream &infile)
{
string text;
while (infile >> text);
{
cout << text << endl;
}
}
|