以下是我的代碼。我的算法有問題。它顯示來自輸入文件的最大值和最小值的整數值。值。請有人看看並告訴我我做錯了什麼?如何顯示從文件讀取的整數的最小值和最大值?
#include "cstdlib"
#include "iostream"
#include "fstream"
using namespace std;
int main()
{
fstream instream;
ofstream outstream;
instream.open("num.txt");
if(instream.fail())
{
cout<<"The input file failed to open\n";
exit(1);
}
outstream.open("output.txt");
if(outstream.fail())
{
cout<<"The output file failed to open";
exit(1);
}
int next, largest, smallest;
largest = 0;
smallest = 0;
while(instream>>next)
{
largest = next;
smallest = next;
if(largest<next)
{
largest = next;
}
if(smallest>next)
{
smallest = next;
}
}
outstream<<"The largest number is: "<<largest<<endl;
outstream<<"The smallest number is: "<<smallest<<endl;
instream.close();
outstream.close();
return 0;
}
你調試? –
另外,請修正格式。 –
請查看您的問題的[格式不同](http://stackoverflow.com/posts/10993666/revisions)。下次確定你自己做這個。 @jrok,你可能不想修復問題中的代碼。 – Bart