views:

505

answers:

2

Is there a way to write a C program to convert say Dollar to Indian Rupee (or visa-versa). The conversion parameter should not be hardcoded but dynamic. More preciously it should get the latest value of Rupee vs Dollar automatically(from Internet) ?

+21  A: 

Step 1 would be to get the latest conversion rate. You can use a web-service for that. There are many available. You can try this.

Request:

GET /CurrencyConvertor.asmx/ConversionRate?FromCurrency=INR&ToCurrency=USD HTTP/1.1
Host: www.webservicex.net

Response:

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<double xmlns="http://www.webserviceX.NET/"&gt;SOME_RATE_IN_DOUBLE&lt;/double&gt;

For sending the request you can make use of cURL.

Once you have the response, just parse it to get the rate. Once you've the rate you can easily write the program to convert.

EDIT:

If using cURL is something you are not comfortable with you can make use of good old system and wget. For this you need to construct the URL first like:

www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=INR&ToCurrency=USD

then from the C program you can do:

char cmd[200];
char URL[] = "www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=INR&ToCurrency=USD";
sprintf(cmd,"wget -O result.html '%s'",URL); // ensure the URL is in quotes.
system(cmd);

After this the conversion rate is in the file result.html as XML. Just open it and parse it.

If you are using windows, you need to install wget for windows if you don't have it. You can get it here.

codaddict
Hi.. thank you for your response and the solution... But i am getting some problem with this, when i run the above code i am getting error like below.(i am running on ubuntu)HTTP request sent, awaiting response... 500 Internal Server Error2010-02-08 11:50:50 ERROR 500: Internal Server Error.(My net connection is proper)thank you...
ganapati
Ganpati, my bad, you need to enclose the URL in quotes. I'll update my answer in a min.
codaddict
codaddict, it is working superb... Thank you, very much :)
ganapati
codaddict, hi can you do a favour for me?,,, can you reload the parsing part of the code once again?
ganapati
A: 

First, you need to find a server that can provides the conversion rate. After that, you write your program to fetch the rates from that server and use those information further in your program.

This site, http://www.csharphelp.com/2007/01/currency-converter-server-with-c/ although provides a tutorial for C# + Web, it can give you a general technical idea of how to do it.

unknownthreat