C
C#•2y ago
danimasona

C#

I have issues with the problem on the pic, when I put the code on the judge system it passed all tests but when I tried to run it on visual studio the compiler says this: System.FormatException: 'Input string was not in a correct format.' for the line: decimal[] prices = Array.ConvertAll(Console.ReadLine().Split(), decimal.Parse); This is the whole code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace zadacha5 { class Program { static void Main(string[] args) { string[] products = Console.ReadLine().Split(); long[] quantities = Array.ConvertAll(Console.ReadLine().Split(), long.Parse); decimal[] prices = Array.ConvertAll(Console.ReadLine().Split(), decimal.Parse); string productName = Console.ReadLine(); while (productName != "done") { int index = Array.IndexOf(products, productName); Console.WriteLine("{0} costs: {1}; Available quantity: {2}", productName, prices[index], quantities[index]); productName = Console.ReadLine(); } } } }
19 Replies
Kouhai
Kouhai•2y ago
What does your input look like?
danimasona
danimasonaOP•2y ago
Bread Juice Fruits Lemons 10 50 20 30 2.34 1.23 3.42 1.50 Bread Juice done
Kouhai
Kouhai•2y ago
So it fails on when inputting this line 2.34 1.23 3.42 1.50 Right?
danimasona
danimasonaOP•2y ago
yes
Thinker
Thinker•2y ago
Issue is that Parse uses the current culture by default
Kouhai
Kouhai•2y ago
Adding to what Thinker said, make sure to trim your input
Thinker
Thinker•2y ago
Also oh god please don't use Array.ConvertAll
Kouhai
Kouhai•2y ago
You might have a trailing space This is my first time seeing it used actually 😅
Thinker
Thinker•2y ago
long[] qualitities = Console.ReadLine()
.Split()
.Select(s => long.Parse(s, System.Globalization.CultureInfo.InvariantCulture))
.ToArray();
long[] qualitities = Console.ReadLine()
.Split()
.Select(s => long.Parse(s, System.Globalization.CultureInfo.InvariantCulture))
.ToArray();
Kouhai
Kouhai•2y ago
this And also trim input to ensure trailing spaces are removed ThumbsUpSmile
danimasona
danimasonaOP•2y ago
it still shows the same problem @🌈 Thinker 🌈 @Kouhai
Kouhai
Kouhai•2y ago
You need to change Thinker's code to use decimal instead of long
Thinker
Thinker•2y ago
That it can't convert?
danimasona
danimasonaOP•2y ago
yes
Kouhai
Kouhai•2y ago
Did you modify it to use decimal.Parse?
danimasona
danimasonaOP•2y ago
yes
Kouhai
Kouhai•2y ago
Did you remove leading and trailing spaces? Console.ReadLine().Trim()
danimasona
danimasonaOP•2y ago
yes
Kouhai
Kouhai•2y ago
Okay, read the line and assign it to a variable Place a breakpoint and examine the string
Want results from more Discord servers?
Add your server