Created
October 2, 2025 17:31
-
-
Save l3ngli/4327a6482f50c868362c5fdecfd82529 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <fstream> | |
| #include <string> | |
| #include <cstdlib> | |
| #include <ctime> | |
| using namespace std; | |
| int main() { | |
| setlocale(0, ""); | |
| ofstream outFile("cities.txt"); | |
| if (!outFile) { | |
| cout << "error\n"; | |
| return 1; | |
| } | |
| string cities[10] = { | |
| "Beijing", "Shanghai", "Guangzhou", "Shenzhen", "Chengdu", | |
| "Xi'an", "Hangzhou", "Wuhan", "Nanjing", "Suzhou" | |
| }; | |
| for (int i = 0; i < 10; ++i) { | |
| outFile << cities[i] << "\n"; | |
| } | |
| outFile.close(); | |
| ifstream inFile("cities.txt"); | |
| if (!inFile) { | |
| cout << "error\n"; | |
| return 1; | |
| } | |
| srand(time(0)); | |
| int randomCity = rand() % 10; | |
| string line; | |
| for (int i = 0; i <= randomCity; ++i) { | |
| getline(inFile, line); | |
| } | |
| cout << "random city: " << line << "\n"; | |
| inFile.close(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment