這是我的CS1337類,所以我需要在主函數內工作。我不能使用任何東西,除了循環,if/else和switch語句。使用文件爲學生排隊 - 課堂作業
我在記事本中創建了一個Lines.txt文件,該文件應該有一個需要讀取的名稱列表。最終結果應該是讀取所有名稱並在控制檯輸出中顯示一條消息。
我根據我的代碼關閉前一個條目我在網上找到的位置: Question about my design for my C++ homework
任何幫助將不勝感激。
Lines.txt
Leslie
Ron
Tom
Gerry
Donna
Andy
April
Chris
Ben
Mark
控制檯窗口輸出:
Andy should be at the head of the line.
Tom should be at the end of the line.
我的代碼:
int main()
{
ifstream inputFile;
string filename;
string front, back, student;
int students = 10;
// Get the filename from the user.
cout << "Enter the filename: ";
cin >> filename;
// Open the file.
inputFile.open(filename.c_str());
// If the file successfully opened, process it.
if (inputFile)
{
/* A formula the sets each name entered to
the variables front and back*/
front = back = student;
// Read the numbers from the file and
// display them.
while (inputFile >> student)
{
for (int count = 1; count <= students; ++count)
{
// Create an if/else statement that decides which
// student name is alphabetically first and last
if (student < front)
front = student;
else if (student >= back)
back = student;
}
// Display the message showing which name is alphabetically first and last name
}
cout << endl
<< front << " should be at the head of the line." << endl
<< back << " should be at the end of the line." << endl;
// Close the file.
inputFile.close();
}
else
{
// Display an error message.
cout << "Error opening the file.\n";
}
return 0;
}
這個問題似乎是題外話,因爲它屬於上codereview.stackexchange.com – AShelly
AShelly是正確的 - 你應該明確地說,這是行不通的,什麼輸出你得到這麼有人們的具體問題回答,否則這退化爲代碼審查。 –