2016-11-27 60 views
0

該程序涉及從關聯的文本文件向用戶輸出笑話和妙語。笑話文件應該顯示文件的全部內容,而笑線文件應該只顯示文本的最後一行(以前的行是隨機字符,並不意味着要讀取enter code here)。從文本文件獲取內容以正確輸出時出現問題

我遇到的問題是,許多行上的笑話文件內容輸出,當它應該都在同一行。這是笑話文件的內容。

我開始了一個名爲999兆頻段...

它輸出如下...

I 
started 
a 
band 
called 
999 
megabytes... 

點睛之筆文件從最後一排一讀,但只有在顯示最後一個字該行。下面是該文件的內容...

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; 
    } 
} 
+2

您需要使用'的std :: getline'。字符串的流輸入操作符只會讀取一個「單詞」。 – paddy

+0

謝謝你的更新,稻穀。你能告訴我在代碼的哪裏我應該改變這個以及如何?我可以想象它在void displayLastLine函數中,但我不確定做什麼調整 – user7217915

+1

想象一下,你剛剛被告知了什麼函數來讀取文本行。你所做的就是去查找一個參考或例子來說明如何使用這個功能。如果你有任何問題,你回來問一個問題。或者做一個[StackOverflow搜索該功能](http://stackoverflow.com/search?q=%5Bc%2B%2B%5D+getline),看看發生了什麼。 – paddy

回答

0
while (infile >> text); 

operator>>不讀取整個文本行。它讀取單個空白分隔的單詞。這就是爲什麼你最終只能顯示文件中的最後一個單詞。您正在閱讀文件錯誤。

正確的函數讀取整個文本行是std::getline()

string text; 
string lastline; 

while (getline(infile, text)) 
    lastline=text; 

cout << lastline << endl; 
+0

謝謝,山姆!我根據需要對程序進行了更改,並顯示了重點。我試着對這個笑話做同樣的事情,看它是否會在一行中顯示,但現在沒有顯示。我用原來的代碼輸出了所有內容,但它是在多行而不是全部行上輸出的。我檢查了文件中是否有奇怪的回車換行符,但沒有任何顯示出來。我已經做了幾次關於如何糾正這個問題的搜索,但沒有任何東西會出現。 – user7217915