從文件和最小/最大邏輯讀取。最小/最大邏輯和文件讀取錯誤
隨着更多的信息進入,我會每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是排在第一位。 山姆排在最後。
你應該做的是保存所有的名字到一個數組,然後你安排他們..那就節省了大量的時間 – Lamar
我沒有學習數組剛好。 – NonCreature0714
當您閱讀它們時,您一次只顯示一個名稱,而不在其間插入任何空格。 – 1201ProgramAlarm