> For the complete documentation index, see [llms.txt](https://apwt.gitbook.io/zie-scherp-scherper/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://apwt.gitbook.io/zie-scherp-scherper/h4-werken-met-data/4b_inputconverten.md).

# Input verwerken en omzetten

En applicatie die geen input van de gebruiker vergt kan even goed een screensaver zijn. We hebben reeds gezien hoe we met `Console.ReadLine()` de gebruiker tekst kunnen laten invoeren en die we dan vervolgens kunnen verwerken om bijvoorbeeld z'n naam op het scherm te tonen:

![Deze vereenvoudiging van de meeste van onze applicaties blijft gelden.](https://2320684026-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FouoIA7Dvx8ty0RZ0Hqs1%2Fuploads%2Fgit-blob-b14d2822ff356c0557aa8c6e3cd0b1ed29e43c3b%2Finuit.png?alt=media)

**De uitdaging met `ReadLine` is dat deze ALTIJD een string teruggeeft:**

```csharp
string userInput = Console.ReadLine();
```

Dit mag dus niet: `int userInput = Console.ReadLine();` en zal in een *conversion error* resulteren.

Willen we dat de gebruiker een getal invoert dan zal dit nog steeds als `string` moeten worden ingelezen. **Vervolgens zullen we dit vervolgens moeten converteren**.

Invoer van de gebruiker verwerken (dat een andere type dan `string` moet zijn) zal dus uit 3 stappen bestaan:

1. Input **uitlezen** met `Console.ReadLine()`.
2. Input **bewaren** in een `string` variabele.
3. De variabele **parsen** met de `Parse()` bibliotheek naar het gewenste type.

Stel dat we aan de gebruiker z'n gewicht vragen, dan moeten we dus doen:

```csharp
Console.WriteLine("Geef je gewicht:");
string inputGewicht = Console.ReadLine();
double gewicht = double.Parse(inputGewicht);
```

Voorgaande code kan nog 1 lijntje sneller door `ReadLine` ogenblikkelijk als invoer aan de Parse-methode te geven:

```csharp
Console.WriteLine("Geef je gewicht:");
double gewicht = double.Parse(Console.ReadLine());
```

## Foutloze input

Voorgaande code veronderstelt dat de gebruiker géén fouten invoert[^1]. De conversie zal namelijk mislukken indien de gebruiker bijvoorbeeld `Ik weeg 10kg` invoert in plaats van `10,3`.

**In de komende hoofdstukken mag je er altijd van uitgaan dat de gebruiker foutloze input geeft.**

## Kommagetallen in C\#

Goed opletten nu.

De invoer van kommagetallen door de gebruiker is afhankelijk van de landinstellingen van je besturingssysteem. Staat deze in Belgisch/Nederlands dan moet je kommagetallen met een **komma** invoeren (`9,81`). Staat je computer in het Engels dan moet je een **punt** gebruiken (`9.81`).

Maar: **In je C# code moet je kommagetal literals altijd met een punt schrijven**. Dit is onafhankelijk van je taalinstellingen.

[^1]: En wat als je toch foute invoer wilt opvangen? Dan is `TryParse` je vriend. Meer informatie hierover in de appendix.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://apwt.gitbook.io/zie-scherp-scherper/h4-werken-met-data/4b_inputconverten.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
