How to Convert Prices to a New Currency in C/C++

Cloudmersive
1 min readMay 24, 2021

--

Does your business provide services to customers from across the globe? If so, the implementation of a price conversion tool into your website or application could improve transactions from both a processing and user experience perspective. In this brief tutorial, we will provide a demonstration of how you can use an API in C/C++ to automatically convert a price from the source currency into the destination currency by referencing up-to-date currency exchange rate data.

Our first step is to install libcurl:

libcurl/7.75.0

Next, we can call the currency conversion function with the following code:

CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, "https://api.cloudmersive.com/currency/exchange-rates/convert/%3Cstring%3E/to/%3Cstring%3E");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "Apikey: YOUR-API-KEY-HERE");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
const char *data = "\"<double>\"";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
res = curl_easy_perform(curl);
}
curl_easy_cleanup(curl);

Your swiftly returned result will indicate the converted price, ISO currency code, and destination currency symbol. To retrieve your API key, head to the Cloudmersive website to register for a free account: this will provide access to 800 monthly calls across our multitude of APIs.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet