I have been working on a currency convert on VS in c# , my current challenging is how to properly convert Dollar to Frank ,Euro to Pound Sterling to Frank, but when i run the programing everything seems working fine until i enter the amount to be converted in Dollar it works and when i try with the other currencies just doesn't show any result or raise any exception. below is my program, I tried searching for similar challenges but couldn't find one that suit my case ! I am very new to the C# language and community, Maybe it might not be a very smart question but i really do need help !
class Program
{
public static void Main()
{
int ;
double val,frank;
Console.WriteLine("Enter your Choice :\n 1- Dollar to Frank \n 2- Euro to Frank \n 3- Sterling to Frank ");
choice = int.Parse(Console.ReadLine());
(choice)
{
case 1:
Double dollar;
Console.Write("Enter the Dollar Amount :");
dollar = Double.Parse(Console.ReadLine());
Console.Write("Enter the Dollar Value :");
val = double.Parse(Console.ReadLine());
frank = dollar * val;
Console.WriteLine("{0} Dollar Equals {1} Frank", dollar, frank);
break;
case 2:
Double Euro;
Console.Write("Enter the Euro Amount :");
Euro Double.Parse(Console.ReadLine());
Console.Write("Enter the Euro Value :");
val = double.Parse(Console.ReadLine());
frank = Euro * val;
Console.WriteLine("{0} Euro Equals {1} Frank", Euro, frank);
break;
case 3:
Double sterling;
Console.Write("Enter the Sterling Amount :");
Double.Parse(Console.ReadLine());
Console.Write("Enter the Sterling Value :");
val = double.Parse(Console.ReadLine());
frank = sterling * val;
Console.WriteLine("{0} Sterling Equals {1} Frank", sterling, frank);
break;
}
Console.ReadLine();
}
}
CodePudding user response:
I'd try to tackle this in a more generic way and with less repetition.
public static void Main()
{
decimal EnterDecimal(string message, bool zero_allowed)
{
while (true)
{
Console.WriteLine(message);
if (decimal.TryParse(Console.ReadLine(), out decimal value))
if (zero_allowed || value != 0m)
return value;
}
}
string EnterCurrency(string message)
{
while (true)
{
Console.WriteLine(message);
string currency = Console.ReadLine().ToUpperInvariant();
if (currency.Length == 3)
return currency;
}
}
string currency_from = EnterCurrency("Enter From Currency");
string currency_to = EnterCurrency("Enter To Currency");
decimal rate = EnterDecimal($"Enter exchange rate from {currency_from} to {currency_to}", false);
decimal amount_from = EnterDecimal($"Enter the amount of {currency_from} to convert", true);
decimal amount_to = amount_from * rate;
Console.WriteLine($"{amount_from} {currency_from} equals {amount_to} {currency_to}");
}
A typical run of this code might be:
Enter From Currency
AUD
Enter To Currency
USD
Enter exchange rate from AUD to USD
0.7
Enter the amount of AUD to convert
100
100 AUD equals 70.0 USD
CodePudding user response:
I'm not 100% sure if this is what you asked for, but code below will work for any of 3 selected. I tried making it as simple as possible, and as much alike as your code already was.
internal class Promena
{
public static void Main()
{
decimal val, frank;
Console.WriteLine("Enter your choice :\n 1- Dollar to Frank \n 2- Euro to Frank \n 3- Sterling to Frank");
int choice = int.Parse(Console.ReadLine());
if(choice == 1 )
{
decimal dollar;
Console.WriteLine("Enter the Dollar amount : ");
dollar = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the Dollar value:");
val = decimal.Parse(Console.ReadLine());
frank = dollar * val;
Console.WriteLine("Amount of " dollar " Dollars = " frank " Franks");
}
else if(choice == 2 )
{
decimal euro;
Console.WriteLine("Enter the Euro amount : ");
euro = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the Dollar value:");
val = decimal.Parse(Console.ReadLine());
frank = euro * val;
Console.WriteLine("Amount of " euro " Euros = " frank " Franks");
}
else if(choice == 3 )
{
decimal sterling;
Console.WriteLine("Enter the Sterling amount : ");
sterling = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the Dollar value:");
val = decimal.Parse(Console.ReadLine());
frank = sterling * val;
Console.WriteLine("Amount of " sterling " Euros = " frank " Franks");
}
}
}
