0
我想要一個程序來讀取數據文件中的成績和其他信息,然後輸出每個學生的成績和平均值,而不是知道有多少學生將在課堂上。如何獲取程序來讀取數據文件,直到數據文件以C++結尾
我的數據文件看起來像這樣:
傑克遜Greaves的1 2 3 4 5 6 7 8 9 10 1 1 1 1 1 1 1 1 100 100
丹尼爾德爾加迪略1 2 3 4 5 6 7 8 9 10 2 2 2 2 2 2 2 2 95 13
Nolyn prestion 10 10 10 10 10 10 10 10 10 10 8 8 8 8 8 8 8 8 100 0
這裏是節目我有這麼遠(它只讀第一行,並不會循環讀取其他行,如何獲得此循環和逐行循環直到文件結束?):
//Lab 1, 9/17/14
//George W. Bush
//Miley Cyrus
//Bob Dylan
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
float lab1, lab2, lab3, lab4, lab5, lab6, lab7, lab8, lab9, lab10;
float hw1, hw2, hw3, hw4, hw5, hw6, hw7, hw8;
float midterm;
float finall;
float labScore;
float labAvg;
float hwScore;
float hwAvg;
float finalGrade;
string letterGrade;
string studentNameFirst;
string studentNameLast;
ifstream inFile;
inFile.open("inputGrades.txt", ios::in); // opens input file
if (!inFile) {
cout << "Cannot open input file. "; // file not open
return 1;
}
while (!inFile.eof()) {// until file ends
inFile >> studentNameFirst >> studentNameLast >> lab1 >> lab2 >> lab3 >> lab4 >> lab5 >> lab6 >> lab7 >> lab8 >> lab9 >> lab10 >> hw1 >> hw2 >> hw3 >> hw4 >> hw5 >> hw6 >> hw7 >> hw8 >> midterm >> finall;
labScore = ((lab1 + lab2 + lab3 + lab4 + lab5 + lab6 + lab7 + lab8 + lab9 + lab10)/100) * 100;
labAvg = (lab1 + lab2 + lab3 + lab4 + lab5 + lab6 + lab7 + lab8 + lab9 + lab10)/10;
hwScore = ((hw1 + hw2 + hw3 + hw4 + hw5 + hw6 + hw7 + hw8)/80) * 100; // calculate homework scores
hwAvg = (hw1 + hw2 + hw3 + hw4 + hw5 + hw6 + hw7 + hw8)/8; //calculates lab averages
finalGrade = (.2 * (labScore) + .1 * (hwScore) + .3 * (midterm) + .4 * (finall)); // calculates percent final grade
// Assigns a letter grade based on percent
if (finalGrade >= 90)
letterGrade = "A";
else if (finalGrade >= 85)
letterGrade = "A-";
else if (finalGrade >= 80)
letterGrade = "B+";
else if (finalGrade >= 75)
letterGrade = "B";
else if (finalGrade >= 70)
letterGrade = "B-";
else if (finalGrade >= 65)
letterGrade = "C+";
else if (finalGrade >= 60)
letterGrade = "C";
else if (finalGrade >= 55)
letterGrade = "C-";
else if (finalGrade >= 50)
letterGrade = "D+";
else if (finalGrade >= 45)
letterGrade = "D";
else if (finalGrade >= 40)
letterGrade = "D-";
else letterGrade = "F";
cout << "Name: " << studentNameFirst << " " << studentNameLast << "Lab Average: " << labAvg << "Hw Average: " << hwAvg << "Final Grade: " << finalGrade << "Letter Grade: " << letterGrade << endl;
return 0;
}
}
您期待'eof'預測將來會發生的讀取結果,而不是測試您的讀取是否成功。 – 2014-09-29 23:02:20