Das Programm beinhaltet die Ausgabe eines Witzes und einer Pointe aus den zugehörigen Textdateien an den Benutzer. Die Joke-Datei sollte den gesamten Inhalt der Datei anzeigen, während die Punchline-Datei nur die letzte Textzeile anzeigen sollte (die vorherigen Zeilen sind zufällige Zeichen, die nicht zum Lesen enter code here
gemeint sind).Probleme mit der korrekten Ausgabe von Inhalten aus Textdateien
Das Problem, das ich erfahre, ist, dass der Witz Datei Inhalt in zahlreichen Zeilen ausgibt, wenn es alle auf der gleichen Linie sein sollte. Hier ist der Inhalt der Witzdatei.
begann ich eine Band 999 Megabyte genannt ...
Es ist wie folgt ...
Ausgabe vonI
started
a
band
called
999
megabytes...
Die Pointe-Datei aus der letzten Reihe ist zu lesen, aber nur das letzte Wort Anzeige in die Reihe. Hier ist der Inhalt der Datei ...
asfasdfasdfasdfsdf
asdfasdfsadfsadfsadf
asdfsadfsdfsdf
We haven't gotten a gig yet.
Here is what is outputting to the screen...
yet.
I have checked the file 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...
/*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;
}
}
Sie müssen 'std :: getline' verwenden. Der Stream-Eingabeoperator für Strings liest nur ein "Wort". – paddy
danke für das Update, Paddy. Könnten Sie mir bitte zeigen, wo im Code ich das ändern soll und wie? Ich könnte mir vorstellen, dass es innerhalb der Funktion für void displayLastLine, aber ich bin mir nicht sicher, welche Einstellung zu machen – user7217915
Stellen Sie sich vor, dass Sie genau gesagt, welche Funktion zum Lesen von Zeilen des Textes verwendet werden. Was Sie tun, ist, eine Referenz oder ein Beispiel für die Verwendung dieser Funktion zu suchen. Und wenn Sie irgendwelche Probleme haben, kommen Sie zurück und stellen Sie eine Frage. Oder machen Sie eine [StackOverflow Suche nach dieser Funktion] (http://stackoverflow.com/search?q=%5Bc%2B%2B%5D+getline) und sehen Sie, was auftaucht. – paddy