// =========================================================================== // Week 11: File I/O and C++ Strings // =========================================================================== // ============================================ // Week 11: File I/O and C++ Strings (p. 12) #include #include #include using namespace std; int main() { ofstream scoreFile("temp.txt", ios::out); char name[20] = {0}; int score = 0; char notFin = true; bool con = true; if(!scoreFile) exit(1); while (con) { cin >> name >> score; scoreFile << name << " " << score << "\n"; cout << "Continue (Y/N)? "; cin >> notFin; con = ((notFin == 'Y') ? true : false); } scoreFile.close(); return 0; } // ============================================ // Week 11: File I/O and C++ Strings (p. 16) #include #include using namespace std; int main() { ifstream inFile("score.txt"); if(inFile) { char name[20] = {0}; int score = 0; int sumScore = 0; int scoreCount = 0; while(inFile >> name >> score) // when does it stop? { sumScore += score; scoreCount++; } if(scoreCount != 0) cout << static_cast(sumScore) / scoreCount; else cout << "no grade!"; } inFile.close(); return 0; }