テキスト ファイルから整数を取得し、数値、最小数値、最大数値、平均、合計、N 個の数値などを出力するプログラムを作成する必要があります。以下のコードでこれをうまく行うことができます。しかし、行ごとにテキストを処理する必要もあります。私のサンプル ファイルには、1 行にタブで区切られた 7 つの数字があり、合計で 8 行ありますが、1 行あたりの数字、ファイルあたりの行数などはわかりません。
また、ベクトルと配列の使用方法を知っていても、私がいる特定のクラスはそれらに到達していないため、使用したくありません。
ありがとう。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
int num;
int count = 0;
int total = 0;
int average = 0;
string str = "";
int numLines = 0;
int lowNum = 1000000;
int highNum = -1000000;
ifstream fileIn;
fileIn.open("File2.txt");
if (!fileIn) {
cout << "nError opening file...Closing program.n";
fileIn.close();
}
else {
while (!fileIn.eof()) {
fileIn >> num;
cout << num << " ";
total += num;
count++;
if (num < lowNum) {
lowNum = num;
}
if (num > highNum) {
highNum = num;
}
}
average = total / count;
cout << "nnTotal is " << total << "." << endl;
cout << "Total amount of numbers is " << count << "." << endl;
cout << "Average is " << average << "." << endl;
cout << "Lowest number is " << lowNum << endl;
cout << "Highest number is " << highNum << endl;
fileIn.close();
return 0;
}
}