C
C#16mo 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
Kouhai16mo ago
What does your input look like?
danimasona
danimasona16mo ago
Bread Juice Fruits Lemons 10 50 20 30 2.34 1.23 3.42 1.50 Bread Juice done
Kouhai
Kouhai16mo ago
So it fails on when inputting this line 2.34 1.23 3.42 1.50 Right?
danimasona
danimasona16mo ago
yes
Thinker
Thinker16mo ago
Issue is that Parse uses the current culture by default
Kouhai
Kouhai16mo ago
Adding to what Thinker said, make sure to trim your input
Thinker
Thinker16mo ago
Also oh god please don't use Array.ConvertAll
Kouhai
Kouhai16mo ago
You might have a trailing space This is my first time seeing it used actually 😅
Thinker
Thinker16mo 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
Kouhai16mo ago
this And also trim input to ensure trailing spaces are removed ThumbsUpSmile
danimasona
danimasona16mo ago
it still shows the same problem @🌈 Thinker 🌈 @Kouhai
Kouhai
Kouhai16mo ago
You need to change Thinker's code to use decimal instead of long
Thinker
Thinker16mo ago
That it can't convert?
danimasona
danimasona16mo ago
yes
Kouhai
Kouhai16mo ago
Did you modify it to use decimal.Parse?
danimasona
danimasona16mo ago
yes
Kouhai
Kouhai16mo ago
Did you remove leading and trailing spaces? Console.ReadLine().Trim()
danimasona
danimasona16mo ago
yes
Kouhai
Kouhai16mo 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
More Posts