Skip to content

Instantly share code, notes, and snippets.

@vlaleli
Created February 8, 2026 17:25
Show Gist options
  • Select an option

  • Save vlaleli/4e9f68a61987935150fd606ac685cb33 to your computer and use it in GitHub Desktop.

Select an option

Save vlaleli/4e9f68a61987935150fd606ac685cb33 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <curl/curl.h>
using namespace std;
size_t WriteToString(void* ptr, size_t size, size_t nmemb, void* data) {
((string*)data)->append((char*)ptr, size * nmemb);
return size * nmemb;
}
string getTemperature(const string& city) {
CURL* curl = curl_easy_init();
if (!curl) return "error";
string response;
char* enc = curl_easy_escape(curl, city.c_str(), (int)city.size());
string url = "https://wttr.in/" + string(enc) + "?format=%t";
curl_free(enc);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteToString);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl");
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res != CURLE_OK)
return "error";
response.erase(remove(response.begin(), response.end(), '\n'), response.end());
response.erase(remove(response.begin(), response.end(), '\r'), response.end());
return response;
}
int main() {
vector<string> cities = {
"Kyiv", "Lviv", "Mykolaiv", "Lisbon", "Paris",
"Stuttgart", "Prague", "Warsaw", "Rome", "Kherson"
};
cout << "Температура в улюблених містах:\n\n";
for (const auto& city : cities) {
string temp = getTemperature(city);
cout << city << " : " << temp << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment