I always prefer to write my programs as scalable as possible. Something like:
Code:
#include <stdio.h>
#include <stdlib.h>
struct
{
char *name; // Name of currency
float convert_factor; // Percent of dollar worth
} moneys[] =
{
{ "dollar", 1.00f },
{ "zoola", 0.32f },
{ "barwi", 1.94f },
{ "warmo", 0.86f },
{ NULL }
};
int main(void)
{
int choice, from, to;
float amount;
char buf[50];
do
{
puts("Convert money from:");
for(from = 0;moneys[from].name;from++)
printf("\t%d) %s\n", from+1, moneys[from].name);
printf("Which one (1-%d)? ", from);
fflush(stdout);
fgets(buf, sizeof(buf), stdin);
choice = atoi(buf);
} while(choice < 1 || choice > from);
from = choice-1;
do
{
puts("Convert money to:");
for(to = 0;moneys[to].name;to++)
if(to != from)
printf("\t%d) %s\n", to+1, moneys[to].name);
printf("Which one (%d-%d)? ", from == 0?2:1, (to == from+1)?to-1:to);
fflush(stdout);
fgets(buf, sizeof(buf), stdin);
choice = atoi(buf);
} while(choice < 1 || choice > to || choice == from+1);
to = choice-1;
printf("Amount? ");
fflush(stdout);
fgets(buf, sizeof(buf), stdin);
amount = atof(buf);
printf("%s: %f\n", moneys[from].name, amount);
amount /= moneys[from].convert_factor;
amount *= moneys[to].convert_factor;
printf("%s: %f\n", moneys[to].name, amount);
return 0;
}
To add another type of currency it's a matter of adding 1 simple line to the program.