Title: C++ Console Application Crashing When Fetching Weather Data Using CppRestSdk
Body:
I’m working on a C++ console application that retrieves weather data from the OpenWeather API using the CppRestSdk
. I’ve downloaded and configured Boost, OpenSSL, and CppRestSdk, and I’ve set the appropriate paths for the linker and configuration.
I’ve written the following code to fetch the temperature, humidity, and weather conditions:
#include <iostream>
#include <string>
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
using namespace std;
using namespace web;
using namespace web::http;
using namespace web::http::client;
int main() {
cout << "t+-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-+" << endl;
cout << "ttMy Weather App" << endl;
cout << "t+-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-+" << endl;
cout << "tEnter city name: ";
string city;
getline(cin, city);
// Your API key here
string api_key = "your_actual_api_key";
try {
http_client client(U("https://api.openweathermap.org/data/2.5"));
uri_builder builder(U("/weather"));
builder.append_query(U("q"), utility::conversions::to_string_t(city));
builder.append_query(U("appid"), utility::conversions::to_string_t(api_key));
client.request(methods::GET, builder.to_string()).then([](http_response response) {
if (response.status_code() == status_codes::OK) {
return response.extract_json();
} else {
throw runtime_error("Received response status code " + to_string(response.status_code()));
}
}).then([](web::json::value body) {
if (body.has_field(U("main"))) {
auto main = body[U("main")];
cout << "tTemperature: " << main[U("temp")].as_double() - 273.15 << "C" << endl;
cout << "tHumidity: " << main[U("humidity")].as_integer() << "%" << endl;
}
if (body.has_field(U("weather"))) {
auto weather = body[U("weather")].as_array();
if (!weather.empty()) {
cout << "tWeather: " << utility::conversions::to_utf8string(weather[0][U("main")].as_string()) << endl;
}
}
}).wait();
} catch (const std::exception& e) {
cerr << "Error: " << e.what() << endl;
}
return 0;
}
When I run this code, the console application crashes with the following output:
+-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-+
My Weather App
+-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-+
Enter city name: vadodara
<path>x64Debugmy weather app.exe (process 19800) exited with code -1073741819 (0xc0000005).
Press any key to close this window . . .
Here are the steps I have taken:
- Verified that the API key and city name are correct.
- Tested the API request using Postman, and it returns valid data.
- Configured Boost, OpenSSL, and CppRestSdk paths properly in the linker settings.
What I’ve tried:
- Added error handling to catch exceptions.
- Converted temperature from Kelvin to Celsius in the output.
Can anyone help me diagnose why the application crashes? Are there any common issues with using CppRestSdk
or handling HTTP requests in C++ that I might be missing?
Nilesh Dhankani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.