2015-09-23 37 views
1

從文件和最小/最大邏輯讀取。最小/最大邏輯和文件讀取錯誤

隨着更多的信息進入,我會每30分鐘更新一次我的問題,陳述和代碼,所以我不會比一些人能夠回答的速度更快。


我的問題是,我怎麼設定的程序一次讀一個名稱,而不是串聯的名字?

該文件是一個文本文件,並寫着: 成龍山姆湯姆·比爾瑪麗保羅澤夫倒鉤約翰

#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

int main() 
{ 
// File stream objects 
ifstream inputFile; 
inputFile.open("LineUp.txt"); 

// Non-user variables 
string first_In_Line = "", 
     last_In_Line = "", 
     previous_Name = "", 
     next_name = ""; 



if (inputFile) 
{ 

    // Display message to user 
    cout << "Reading file... \n"; 

    while (inputFile >> next_name) 
    { 
    cout << next_name; 

    if (next_name > last_In_Line) 
     { 
     first_In_Line = last_In_Line; 
     last_In_Line = next_name; 
     } 
    else if (next_name < first_In_Line) 
     { 
     last_In_Line = first_In_Line; 
     first_In_Line = next_name; 
     } 
    // This else clause should only apply to the first iteration 
    else 
     { 
     first_In_Line = next_name; 
     } 
    } 

    //Close the file 
    inputFile.close(); 

    // Display first in line and last in line 
    cout << first_In_Line << " is first in line." << endl; 
    cout << "And " << last_In_Line << " is last in line." << endl; 

} 
else 
{ 
    // Display error message. 
    cout << "Error opening the file.\n"; 
} 

return 0; 
} 

輸出是: 讀文件...... JackieSamTomBillMaryPaulZevBarbJohnJohn是排在第一位。 山姆排在最後。

+0

你應該做的是保存所有的名字到一個數組,然後你安排他們..那就節省了大量的時間 – Lamar

+0

我沒有學習數組剛好。 – NonCreature0714

+0

當您閱讀它們時,您一次只顯示一個名稱,而不在其間插入任何空格。 – 1201ProgramAlarm

回答

1

我所建議給你的是使用陣列然後使用算法排序功能

數組是一種數據結構,它是利用程序運行時保存數據。

因此,我們可以將這些數據從文件保存到該數組。數組的名稱是dataFromFile可以保存多達9個字符串值。因此,如果你在你的文件中有多個名稱,只需更新數組或使用矢量

ifstream file("dataToRead.txt"); 
    string dataFromFile[9]; 
    string line; 
    int index = 0; 

    if(!file) 
    { 
    cout<<"cannot find this file" <<endl; 
} 
    else 
    { 
    if(file.is_open()) 
     { 
       while (getline(file,line)) 
       { 
       dataFromFile[index] = line; 
       index++; 
      } 
      file.close(); 
     } 
    } 

的大小,然後使用一個循環

for(int j=0;j<9;j++) 
    { 
     // to do display 
    cout<<dataFromFile[j] <<endl; 
    } 

顯示我們所擁有的數組現在裏面來對它們進行排序只是#include <algorithm>然後使用排序方法,該方法被稱爲dataFromFile

sort(begin(dataFromFile),end(dataFromFile)); 
0陣列上

然後重新顯示你有什麼入陣

for(int j= 0 ;j < 9;j++) 
{ 
    // after sorting 
    cout<<dataFromFile[j] <<endl; 
} 
+0

這是最好的答案。 – NonCreature0714

+0

哈哈歡迎您,很高興我能幫到您 – Lamar

+0

如果超過9行,這段代碼會崩潰。你爲什麼不使用vector和push_back? –

0

不使用陣列,這是最好的解決辦法,有在if語句中的邏輯錯誤。

首先,字符串初始化爲空,所以空字符串總是按first_In_Line排序。 first_In_Line需要在while循環的第一次迭代中分配一個值。

接下來,通過while循環的第四次迭代,變量變得不合邏輯地分配,並且「Sam」通過while循環的其餘部分在first_In_Line和last_In_Line之間來回傳遞。

以下是我解決了這個問題:

#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

int main() 
{ 
// File stream objects 
ifstream inputFile; 
inputFile.open("LineUp.txt"); 

// Non-user variables 
string first_In_Line = "", 
     last_In_Line = "", 
     next_name = ""; 

if (inputFile) 
{   
    // Display message to user 
    cout << "Reading file... \n\n"; 

    while (inputFile >> next_name) 
    { 
    cout << next_name << endl; // list the names 

    if (last_In_Line == first_In_Line) 
     { 
     first_In_Line = next_name; 
     } 
    else if (next_name > last_In_Line) 
     { 
     last_In_Line = next_name; 
     } 
    else if (next_name < first_In_Line) 
     { 
     first_In_Line = next_name; 
     } 
    } 

    //Close the file 
    inputFile.close(); 

    // Display first in line and last in line 
    cout << endl << first_In_Line << " is first in line." << endl; 
    cout << "And " << last_In_Line << " is last in line." << endl; 

} 
else 
{ 
    // Display error message. 
    cout << "Error opening the file.\n"; 
} 

return 0; 
}