Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C program to convert Dollar to Rupee

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 hard coded but dynamic. More preciously it should get the latest value of Rupee vs Dollar automatically(from Internet) ?

like image 939
ganapati Avatar asked Feb 08 '10 05:02

ganapati


1 Answers

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/">SOME_RATE_IN_DOUBLE</double>

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.

like image 60
codaddict Avatar answered Oct 06 '22 11:10

codaddict