// ============================================ // C/C++ Strings and File I/O (p. 6) int array[10]; cin >> array; return 0; char array[10]; cin >> array; return 0; // ============================================ // C/C++ Strings and File I/O (p. 7) char str[10]; cin >> str; // if we type "abcde" cout << str[0]; // 'a' cout << str[2]; // 'c¡¦ // ============================================ // C/C++ Strings and File I/O (p. 8) int values[5] = {0}; cout << values; // an address char array[10] = {'a', 'b', 'c'}; cout << array; // "abc" // ============================================ // C/C++ Strings and File I/O (p. 11) char a[100] = "abcde FGH"; cout << a << endl; // abcde FGH char b[100] = "abcde\0 FGH"; cout << b << endl; // abcde // ============================================ // C/C++ Strings and File I/O (p. 12) char c[100]; cin >> c; // "123456789" cin >> c; // "abcde"; cout << c << endl; // "abcde" c[5] = '*'; cout << c << endl; // "abcde*789" // ============================================ // C/C++ Strings and File I/O (p. 13) char a[5]; cin >> a; // "123456789" cout << a; // "123456789" or an error // ============================================ // C/C++ Strings and File I/O (p. 14) char a1[100]; cin >> a1; // "this is a string" cout << a1; // "this" char a2[100] = {'a', 'b', ' ', 'c', '\0', 'e'}; cout << a2; // ab c // ============================================ // C/C++ Strings and File I/O (p. 15) char a[100]; cin.getline(a, 100); // "this is a string" cout << a << endl; // "this is a string" // ============================================ // C/C++ Strings and File I/O (p. 36) #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; } // ============================================ // C/C++ Strings and File I/O (p. 40) #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; }