Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ExtremeFine21/4a94792f7d34d12d12d2dda0ecd4ad10 to your computer and use it in GitHub Desktop.

Select an option

Save ExtremeFine21/4a94792f7d34d12d12d2dda0ecd4ad10 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <array>
#include <cstdio>
#include <vector>
using namespace std;
string runCommand(const string& cmd) {
array<char, 256> buffer{};
string result;
FILE* pipe = popen(cmd.c_str(), "r");
if (!pipe) return "";
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) {
result += buffer.data();
}
pclose(pipe);
while (!result.empty() && (result.back() == '\n' || result.back() == '\r')) {
result.pop_back();
}
return result;
}
int main() {
vector<string> cities = {
"Odesa",
"Kyiv",
"Lviv",
"Warsaw",
"Berlin",
"Paris",
"London",
"Rome",
"Madrid",
"Prague"
};
cout << "Місто\tТемпература\n";
cout << "----------------------\n";
for (const string& city : cities) {
string url = "https://wttr.in/" + city + "?format=%t";
string temp = runCommand("curl -s \"" + url + "\"");
if (temp.empty()) {
temp = "N/A";
}
cout << city << "\t" << temp << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment