>Im just wondering if any of you guys would do it a diffrent way?
>But as i said just some input and perhaps some ideas on how to do it other ways.
When this question came up once before, I tried something like this.
Code:
#include <stdio.h>
#include <string.h>
const char Description[] =
"Currency Conversion As of June 11, 2003 2:35am GMT";
void convert(const char *from, const char *to, double amount)
{
static const struct
{
const char *name;
double rate;
} Xchg[] = /* US Dollar conversion factor */
{
{ "USD", 1.0 },
{ "Lire", 0.000603049 },
{ "Yen", 0.008464602 },
{ "Pound", 1.65123 },
{ "Peso", 0.0940910 },
{ "Canadian", 0.734061 },
};
size_t i,j;
for ( i = 0; i < sizeof(Xchg)/sizeof(*Xchg); ++i )
{
if ( strcmp(Xchg[i].name, from) == 0 )
{
for ( j = 0; j < sizeof(Xchg)/sizeof(*Xchg); ++j )
{
if ( strcmp(Xchg[j].name, to) == 0 )
{
double value = amount * Xchg[i].rate / Xchg[j].rate;
printf("%g %s = %g %s\n", amount, from, value, to);
return;
}
}
printf("Cannot convert to \"%s\"\a\n", to);
return;
}
}
printf("Cannot convert from \"%s\"\a\n", from);
}
int main(void)
{
puts(Description);
convert("USD", "Pound", 20.0);
convert("Yen", "Lire", 100.0);
convert("Euro", "Krone", 20.0);
convert("USD", "Krone", 20.0);
return 0;
}
/* my output
Currency Conversion As of June 11, 2003 2:35am GMT
20 USD = 12.1122 Pound
100 Yen = 1403.63 Lire
Cannot convert from "Euro"
Cannot convert to "Krone"
*/