Created
February 11, 2026 17:38
-
-
Save Marie-zaj/ca89638ee27e58fc7911e1fb60e20df8 to your computer and use it in GitHub Desktop.
WeatherPract3
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 <string> | |
| #include <windows.h> | |
| #include <urlmon.h> | |
| #pragma comment(lib, "urlmon.lib") | |
| using namespace std; | |
| int main() { | |
| cout << "Enter the name of the city: "; | |
| string city; | |
| cin >> city; | |
| string srcURL = "https://wttr.in/" + city; | |
| const char* destFile = "weather.html"; | |
| if (URLDownloadToFileA(0, srcURL.c_str(), destFile, 0, 0) == S_OK) | |
| { | |
| cout << "Save to " << destFile << " "; | |
| } | |
| else | |
| { | |
| cout << "Error"; | |
| return 1; | |
| } | |
| FILE* f; | |
| fopen_s(&f, destFile, "r"); | |
| if (!f) | |
| { | |
| cout << "Error - can`t open file"; | |
| return 1; | |
| } | |
| char text[2000]; | |
| while (fgets(text, sizeof(text), f)) | |
| { | |
| string line = text; | |
| size_t pos = line.find("ef0"); | |
| if (pos != string::npos) | |
| { | |
| size_t start = line.find(">", pos); | |
| if (start != string::npos) | |
| { | |
| start++; | |
| size_t end = line.find("<", start); | |
| if (end != string::npos) | |
| { | |
| string temp = line.substr(start, end - start); | |
| cout << endl << "The temperature: " << temp << " C"; | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| fclose(f); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment