MMGameslogo  MMGames
TwitterSharebutton  FacebookSharebutton   
learn through sufferingC Language
learn through sufferingC Language

The fear of input

Fear of typos
Perhaps the author is quite eccentric.
While I presented an example of a well-written program in the previous section,
I'm planning to complain about problems with that program from now on.

"What was the problem with the program we just ran?"
The problem is actually using the `scanf` function.

The `scanf` function is a problematic function.
While frequently used in introductory materials, it's rarely used in real-world development.
Some organizations, including schools, may have a ban on the use of the `scanf` function.

However, the program we made earlier was working without any issues.
"I wonder where the problem lies... it's a data entry error."

As long as it's used correctly, the scanf function doesn't typically cause any problems.
Input errors can suddenly trigger terrifying problems.
Now, I will introduce cases of input errors, using the program we just discussed as an example.
Typographical error in punctuation
Let's start with a likely example: Let's try mistyping a delimiter.
In the previous program, it was separated by commas.
I'd like to deliberately ignore that and try separating it by spaces.

Here's the result when a delimiter is entered incorrectly.

Source code
最小値と最大値を , で区切って入力してください。:100 200
100~307 の合計は 9560 is.

The first 100 entries seem to be entered correctly, but...
"The ones labeled '200' are inexplicably showing up as numbers like '307'."

This result may vary depending on the compiler used.
You might get different results if you try it yourselves.
Too large a number.
Next, I'd like to try inputting extremely large numbers.
Moreover, recent compilers can often handle quite large numbers as input.
If you try this yourself, please use a 16-bit compiler (such as LSI C-86).

Next, here are the results when you put in a number that's too big.

Source code
最小値と最大値を , で区切って入力してください。:1,70000
1~4464 の合計は 4408 is.

I entered 70000, but for some reason, it's being recognized as 4464.
"And furthermore, the answer itself is far too small, considering the sum of 1 to 4464."

These are problems that arise because they exceed the limits of what can be remembered.
"I want to check the input, but the variable itself is assigned the value 4464, so..."
I can't tell whether they intended to enter 4464 or if it was a typo.
String Panic
I'd like to conduct some more mischievous experiments.
So, even though you're being told to enter a number, you end up entering a string.

Here's the result when a string is entered.

Source code
最小値と最大値を , で区切って入力してください。:ABCDEF
11975~307 の合計は -16199 is.

It's a complete mess.
While it can be explained, at least in principle, by the correspondence of bits,
It's pointless to explain, so I won't.
The key point is why, despite entering a string, it's being calculated.

Where did numbers like 11957 or 307 come from?
Of course, it's impossible to determine whether this number was entered correctly or is the result of a data entry error.

You might think that no one would ever make such a foolish mistake, but...
I might be dozing off, or my cat might be walking across the keyboard.
To make matters worse, viruses and hackers can intentionally introduce such inputs.
"What if this happened in a program used by large corporations or governments..."
About Solutions
You might be tired of seeing so many input errors.
"If we don't offer a solution soon, everyone will be fed up."

However, the solution to this problem isn't easy.
At least, with your current knowledge, you can't solve this.
A common approach is to accept the input as a string and then parse the data.
That means to solve this problem, you need to know how to handle strings.
Currently, with your existing knowledge, you are unable to perform string manipulation.

We will introduce the solution to this problem at a later time.
For the time being, we'll just have to keep using the scanf function.
Please be extra careful to avoid input errors in the meantime.


About This Site

Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.

Part 0: Program Overview
  1. What is a program?
Chapter 3: Displaying on the Screen
  1. String Display
  2. line break
  3. Practice Problem 3
Chapter 4: Displaying and Calculating Numbers
  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4
Chapter 6: Input from the Keyboard
  1. input function
  2. The fear of input
  3. Practice Problem 6
Chapter 9: Repeating a Fixed Number of Times
  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9
Chapter 10: Repeating Without Knowing the Number of Times
  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10
Chapter 13: Handling Multiple Variables at Once
  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13
Chapter 19: Dynamic Arrays
  1. Create arrays freely.
  2. Practice Problem 19