Fetching specific foreign exchange rates from fixer using curl and jsconcpp in C++ I am trying to create my own algorithmic trading system using C++. I have searched the web for a nice tutorial for such systems and I didnt find any. Then I started to learn about curl and jsoncpp libraries and even documentation of those libraries are not made for a novice programmer like me. I mostly reused most of the codes for creating this scrapper especially WriteCallback function which I directly copy pasted from another c++ code without understanding it. I am sharing this whole code because some one else dosent need to reinvent the wheel and can use this code as a template.Any way this code is working properly (Ubuntu 14.04, CodeBlocks IDE, API: fixer.io) and any kind of suggestion or feedback is welcomed. 

#include &lt;string.h&gt;
#include &lt;iostream&gt;
#include &lt;stdio.h&gt;
#include &lt;jsoncpp/json/json.h&gt;
#include &lt;curl/curl.h&gt;

//writing call back function for storing fetched values in memory
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
    ((std::string*)userp)-&gt;append((char*)contents, size * nmemb);
    return size * nmemb;
}


int main(void)
{
  std::string readBuffer;


 //global initiliation of curl before calling a function
 curl_global_init( CURL_GLOBAL_ALL );

 //creating session handle
 CURL * myHandle;

 // We’ll store the result of CURL’s webpage retrieval, for simple error checking.
 CURLcode result;


 // notice the lack of major error-checking, for brevity
 myHandle = curl_easy_init ( ) ;

 //after creating handle we ill start transfering webpage

 //curl_easy_setopt is used to tell libcurl how to behave.
 //By setting the appropriate options, the application can change libcurl's behavior.

 //CURLOPT_URL provide the URL to use in the request. Pass in a pointer to the URL to work with.
 //sample json output &gt;&gt; {"base":"EUR","date":"2016-07-22","rates":{"GBP":0.84108,"USD":1.1014}}
 curl_easy_setopt(myHandle, CURLOPT_URL, "http://api.fixer.io/latest?symbols=USD,GBP");

   /* send all data to this function  */
  curl_easy_setopt(myHandle, CURLOPT_WRITEFUNCTION, WriteCallback);

   /* we pass our 'chunk' struct to the callback function */
  curl_easy_setopt(myHandle, CURLOPT_WRITEDATA, &amp;readBuffer);

 //perform a blocking file transfer
 result = curl_easy_perform( myHandle );

 /* check for errors */
  if(result != CURLE_OK) {
    fprintf(stderr, "curl_easy_perform() failed: %s\n",
            curl_easy_strerror(result));
  }
  else {
    /*
     * Now, our chunk.memory points to a memory block that is chunk.size
     * bytes big and contains the remote file.
     *
     * Do something nice with it!
     */

    //std::cout &lt;&lt; readBuffer &lt;&lt; std::endl;

    //json parsing
Json::Value root;   // will contains the root value after parsing.
Json::Reader reader;
bool parsingSuccessful = reader.parse( readBuffer, root );
if(not parsingSuccessful)
 {
   // Report failures and their locations
   // in the document.
   std::cout&lt;&lt;"Failed to parse JSON"&lt;&lt;std::endl
       &lt;&lt;reader.getFormatedErrorMessages()
       &lt;&lt;std::endl;
   return 1;
 }else{
 std::cout&lt;&lt;"\nSucess parsing json\n"&lt;&lt;std::endl;
 std::cout &lt;&lt; root&lt;&lt; std::endl;
 std::cout &lt;&lt;"Base = "&lt;&lt; root["base"].asString() &lt;&lt; std::endl;
 std::cout &lt;&lt;"Date = "&lt;&lt; root["date"].asString() &lt;&lt; std::endl;
 std::cout &lt;&lt;"Rate GBP ="&lt;&lt; root["rates"]["GBP"].asFloat() &lt;&lt; std::endl;
 std::cout &lt;&lt;"Rate USD ="&lt;&lt; root["rates"]["USD"].asFloat() &lt;&lt; std::endl;

 }

  }




 //End a libcurl easy handle.This function must be the last function to call for an easy session
 curl_easy_cleanup( myHandle );


 return 0;
}
These are C headers

#include &lt;string.h&gt;
#include &lt;stdio.h&gt;


You should probably use the C++ version (I hope you are not using C-String anywhere).

#include &lt;strings&gt;
#include &lt;cstdio&gt;


Header include paths

#include &lt;jsoncpp/json/json.h&gt;


This should probably be json/json.h and you should specify the location where jsoncpp is installed with compiler flags. 

Curl is a C library.

Therefore any callbacks should be C functions (not C++ functions). There is no guarantee in the standard thay they use the same ABI.

static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)


Should be:

extern "C" size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)


Never use C-Casts

C casts are hard to spot (in general). But also using C++ casts you can mark dangerious casts by uisng the appropriate cast. 

    ((std::string*)userp)-&gt;append((char*)contents, size * nmemb);


I would use reinterpret_cast&lt;&gt; here. So that it is easy to spot and you intuitively know that this is a dangerous operation.

reinterpret_cast&lt;std::string*&gt;(userp)-&gt;append(static_cast&lt;char*&gt;(contents), size * nmemb);


Always check error codes.

All the following return an error code. You should validate these functions worked by checking this error code.

 curl_global_init( CURL_GLOBAL_ALL );
 curl_easy_setopt(myHandle, CURLOPT_URL, "http://api.fixer.io/latest?symbols=USD,GBP");
 curl_easy_setopt(myHandle, CURLOPT_WRITEFUNCTION, WriteCallback);
 curl_easy_setopt(myHandle, CURLOPT_WRITEDATA, &amp;readBuffer);


Prefer "\n" over std::endl

 std::cout&lt;&lt;"\nSucess parsing json\n"&lt;&lt;std::endl;
 std::cout &lt;&lt; root&lt;&lt; std::endl;
 std::cout &lt;&lt;"Base = "&lt;&lt; root["base"].asString() &lt;&lt; std::endl;
 std::cout &lt;&lt;"Date = "&lt;&lt; root["date"].asString() &lt;&lt; std::endl;
 std::cout &lt;&lt;"Rate GBP ="&lt;&lt; root["rates"]["GBP"].asFloat() &lt;&lt; std::endl;
 std::cout &lt;&lt;"Rate USD ="&lt;&lt; root["rates"]["USD"].asFloat() &lt;&lt; std::endl;


The difference is that std::endl also calls std::flush on the stream. There is no need to flush the stream manually. The stream is designed to flush when it needs too. When you do it manually you will make your code much more eneficent.    

RAII

 myHandle = curl_easy_init ( ) ;
 // STUFF
 curl_easy_cleanup( myHandle );


When you see this pattern. You should be thinking of using RAII. This will make sure your resources are cleaned up correctly in all situations (including when exceptions are thrown) are the function returns early (aka return 1;).