From: bochard Date: Sun, 20 Jul 2025 10:30:25 +0000 (+0800) Subject: added currency converter X-Git-Url: https://git.bochard.net/?a=commitdiff_plain;h=b959ac7054454726bc0621e331682fb1c0b347f8;p=AIO-converter.git added currency converter --- diff --git a/.gitignore b/.gitignore index 88d050b..68c2d26 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -main \ No newline at end of file +main +tempCodeRunnerFile.c \ No newline at end of file diff --git a/main.c b/main.c index 6bedf0a..1b7885e 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,5 @@ #include +#include char intro(); void tempConverter(); @@ -6,20 +7,20 @@ void currConverter(); void massConverter(); int main(){ - char converter = intro(); + int converter = intro(); switch(converter){ - case '1': + case 1: tempConverter(); break; - case '2': + case 2: currConverter(); break; - case '3': + case 3: massConverter(); break; default: - printf("Oops...invalid option: '%c'", converter); + printf("Oops...invalid option: '%d'", converter); printf("\nTry again."); return 1; } @@ -30,7 +31,7 @@ int main(){ } char intro(){ - char choice; + int choice; printf("%.*s", 10, "================="); printf(" Welcome to the Basic Unit Converter! "); @@ -38,14 +39,14 @@ char intro(){ printf("\nChoose one from the list:\n"); printf("(1)Temperature Converter\t(2)Currency Converter\n"); printf("\t\t(3)Mass Converter\n"); - scanf(" %c", &choice); + scanf(" %d", &choice); printf("\n"); return choice; } void tempConverter(){ - char choice; + int choice; float value; float result; @@ -56,64 +57,104 @@ void tempConverter(){ printf("(1)Celcius to Fahrenheit\t(2)Celcius to Kelvin\n"); printf("(3)Fahrenheit to Celcius\t(4)Fahrenheit to Kelvin\n"); printf("(5)Kelvin to Celcius\t\t(6)Kelvin to Fahrenheit\n"); - scanf(" %c", &choice); + scanf(" %d", &choice); printf("Enter a value: "); scanf("%f", &value); switch(choice){ - case '1': + case 1: result = (((float) 9 / 5) * value) + 32; printf("%.2f°C is %.2f°F", value, result); break; - case '2': + case 2: result = value + 273.15; printf("%.2f°C is %.2f°K", value, result); break; - case '3': + case 3: result = (value - 32) * ((float) 5 / 9); printf("%.2f°F is %.2f°C", value, result); break; - case '4': + case 4: result = (value + 459.67) * ((float) 5 / 9); printf("%.2f°F is %.2f°K", value, result); break; - case '5': + case 5: result = value - 273.15; printf("%.2f°K is %.2f°C", value, result); break; - case '6': + case 6: result = (value * ((float) 9 / 5)) - 459.67; printf("%.2f°K is %.2f°F", value, result); break; default: - printf("Oops...invalid option: '%c'", choice); + printf("Oops...invalid option: '%d'", choice); printf("\nTry again."); } } void currConverter(){ - char choice; + int choice; float value; float result; + const float listOfConversions[] = { + // as of Jul 20, 2025 17:01:20 from openexchangesrates.org + 57.110003, //usd to php + 1.53705, //usd to aud + 148.81007759, //usd to jpy + 0.0175100674, //php to usd + 0.0269138491, //php to aud + 2.6056744839, //php to jpy + 0.6505969227, //aud to usd + 37.1555922058, //aud to php + 96.8153785433, //aud to jpy + 0.006719975, //jpy to usd + 0.383777792, //jpy to php + 0.0103289376, //jpy to aud + }; + printf("%.*s", 10, "-----------------"); printf(" Welcome to the Currency Converter! "); printf("%.*s", 10, "-----------------"); printf("\nChoose one from the list:\n"); printf("(1)USD to PHP\t(2)USD to AUD\t(3)USD to JPY\n"); - printf("(4)USD to EUR\t(5)USD to GBP\t(6)PHP to USD\n"); - printf("(7)PHP to AUD\t(8)PHP to JPY\t(9)PHP to EUR\n"); - printf("(10)PHP to GBP\t(11)AUD to USD\t(12)AUD to PHP\n"); - printf("(13)AUD to JPY\t(14)AUD to EUR\t(15)AUD to GBP\n"); - printf("(16)JPY to USD\t(17)JPY to PHP\t(18)JPY to AUD\n"); - printf("(19)JPY to EUR\t(20)JPY to GBP\t(21)EUR to USD\n"); - printf("(22)EUR to PHP\t(23)EUR to AUD\t(24)EUR to JPY\n"); - printf("(25)EUR to EUR\t(26)EUR to GBP\t(27)GBP to USD\n"); - printf("(28)GBP to PHP\t(29)GBP to AUD\t(30)GBP to JPY\n"); - printf("(31)GBP to EUR\n"); - scanf(" %c", &choice); + printf("(4)PHP to USD\t(5)PHP to AUD\t(6)PHP to JPY\n"); + printf("(7)AUD to USD\t(8)AUD to PHP\t(9)AUD to JPY\n"); + printf("(10)JPY to USD\t(11)JPY to PHP\t(12)JPY to AUD\n"); + scanf(" %d", &choice); printf("Enter a value: "); scanf("%f", &value); + + float conversionRate = listOfConversions[choice - 1]; + char currencyToUse[4]; + result = value * conversionRate; + + if(choice == 4 || choice == 7 || choice == 10){ + char currency[] = "USD"; + strcpy(currencyToUse, currency); + } else if(choice == 1 || choice == 8 || choice == 11){ + char currency[] = "PHP"; + strcpy(currencyToUse, currency); + } else if(choice == 2 || choice == 5 || choice == 12){ + char currency[] = "AUD"; + strcpy(currencyToUse, currency); + } else if(choice == 3 || choice == 6 || choice == 9){ + char currency[] = "JPY"; + strcpy(currencyToUse, currency); + } + + if(choice >= 1 && choice <= 3){ + printf("%.2f USD is %.2f %s", value, result, currencyToUse); + } else if(choice >= 4 && choice <= 6){ + printf("%.2f PHP is %.2f %s", value, result, currencyToUse); + } else if(choice >= 7 && choice <= 9){ + printf("%.2f AUD is %.2f %s", value, result, currencyToUse); + } else if(choice >= 10 && choice <= 12){ + printf("%.2f JPY is %.2f %s", value, result, currencyToUse); + } else{ + printf("Oops...invalid option: '%d'", choice); + printf("\nTry again."); + } } void massConverter(){