L
L
lotuzakim2021-06-29 20:13:02
C++ / C#
lotuzakim, 2021-06-29 20:13:02

Problem with random characters on input in C#?

Good day!
Such a problem:
When you run the code, when you need to input, the number 1 appears, which cannot be erased (Sometimes, the number that I entered can be duplicated)
When you input with a random character and another number, the output will be valid
But when you input only with a random character, this mistake:

unhandled exception. System.FormatException: Input string was not in a correct format.
at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type)
at System.Number.ParseInt32(ReadOnlySpan`1 value, NumberStyles styles, NumberFormatInfo info)
at System.Convert.ToInt32(String value)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
LiptonOlolo, 2021-06-30
@LiptonOlolo

DO NOT use Convert.ToInt32 or int.Parse unless you are sure you will have a 100% number as input. Use int.TryParse to avoid these errors. Vitaliy Kachan noticed correctly that your input is definitely not just a number.

V
Vitaly Kachan, 2021-06-29
@MANAB

So maybe there is another character besides a number? The error says that there is definitely not only a number, so it cannot parse. Maybe spaces, newline, tab, what else?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question