0
我對編程非常陌生,我似乎碰到了一堵牆。我試圖寫一個簡單的代碼,讀取不同Bowlers的分數,然後找到每個Bowler的平均值。我的代碼似乎讀取了分數的第一行然後停止。有什麼建議?下面是我有從文本文件中讀取數據,然後獲取平均值
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
ifstream inFile;
ofstream outFile;
double score1, score2, score3;
double average;
string firstname;
string lastname;
inFile.open ("scores.txt");
outFile.open ("scoreavg.out");
ifstream ("scores.txt");
string content;
while (inFile >> content)
{
cout << content << ' ';
inFile >> firstname >> lastname ;
outFile << "Bowler name: " <<firstname << " " << lastname << endl;
inFile >> score1 >> score2 >> score3;
outFile << "scores: " << setw(4) << score1 << setw(4) << score2 << setw(4)<< score3 << endl;
average = (score1 + score2 + score3)/3;
outFile << "Average score: " << setw(4) << average<< endl;
inFile.close();
outFile.close();
return 0;
}
}
您可能需要爲您的帖子添加語言特定的標籤。它是'C++'嗎? –
'return 0;'存在你的函數。從你的while循環中取出。 – grim
哦,我很抱歉,這是...它是C++ –