# Help with a todo app using io and switch

**URL:** <https://ziggit.dev/t/help-with-a-todo-app-using-io-and-switch/2586>\
**Category:** Help\
**Created:** [December 25, 2023, 11:45am UTC](https://ziggit.dev/t/help-with-a-todo-app-using-io-and-switch/2586 "2023-12-25T11:45:48Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![z1fire](https://ziggit.dev/user_avatar/ziggit.dev/z1fire/32/742_2.png) [@z1fire](https://ziggit.dev/u/z1fire)\
**Post date:** [December 25, 2023, 11:45am UTC](https://ziggit.dev/t/help-with-a-todo-app-using-io-and-switch/2586/1 "2023-12-25T11:45:48Z")

</div>

I’m trying to build a simple todo app without asking for help, but here I am. I’m not sure but i think the problem is in my readOption function. The printMenu function works just fine, I’m try to get it to print something with the switch statement when I type in 1. I’m not looking for someone to complete the program but maybe nudge me in the right direction.

```zig
const std = @import("std");
const fmt = std.fmt;

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();
    try printMenu();
    const option = try readOption();
    
      switch (option) {
          1 => try stdout.print("you choose to list items.\n", .{}),
          else => std.debug.print("not an option", .{}),
      }   
}

fn printMenu() !void {
    const stdout = std.io.getStdOut().writer();
    try stdout.print(" ***To-Do App*** \n\n", .{});
    try stdout.print("1. List items\n", .{});
    try stdout.print("2. Add item\n", .{});
    try stdout.print("3. Mark item completed\n", .{});
    try stdout.print("4. Remove item\n", .{});
    try stdout.print("5. Quit\n\n", .{});
    try stdout.print("Enter your choice: ", .{});

}

fn readOption() !u8 {
    const stdin = std.io.getStdIn();
    var line_buf: [2]u8 = undefined;
    const amt = try stdin.read(&line_buf);
    const cleaned = std.mem.trimRight(u8, line_buf[0..amt], "r\n");
    const user_choice = fmt.parseUnsigned(u8, cleaned, 10);
    return user_choice;
}

```

---

<div class="post-metadata">

**Author:** ![iceghost](https://ziggit.dev/user_avatar/ziggit.dev/iceghost/32/460_2.png) [@iceghost](https://ziggit.dev/u/iceghost)\
**Post date:** [December 25, 2023, 12:01pm UTC](https://ziggit.dev/t/help-with-a-todo-app-using-io-and-switch/2586/2 "2023-12-25T12:01:37Z")

</div>

What is the problem? I can run this just fine. Typing 1 and pressing enter prints the correct message.

---

<div class="post-metadata">

**Author:** ![z1fire](https://ziggit.dev/user_avatar/ziggit.dev/z1fire/32/742_2.png) [@z1fire](https://ziggit.dev/u/z1fire)\
**Post date:** [December 25, 2023, 12:05pm UTC](https://ziggit.dev/t/help-with-a-todo-app-using-io-and-switch/2586/3 "2023-12-25T12:05:28Z")

</div>

It doesn’t for me. I get error: IvalidCharacter then it lists other errors,  
0x7ff7718b7aec in charToDigit (todo\_app.exe.obj)  
else =\> return error.InvalidCharacter,  
^  
0x7ff771883142 in parseWithSign\_\_anon\_3548 (todo\_app.exe.obj)  
const digit = try charToDigit(c, buf\_base);  
0x7ff7718819c4 in parseUnsigned\_\_anon\_3449 (todo\_app.exe.obj)  
return parseWithSign(T, buf, base, .pos);  
^  
0x7ff771881621 in readOption (todo\_app.exe.obj)  
return user\_choice;  
^  
0x7ff771881a5e in main (todo\_app.exe.obj)  
const option = try readOption();

run todo\_app: error: the following command exited with error code 1:

---

<div class="post-metadata">

**Author:** ![permutationlock](https://ziggit.dev/user_avatar/ziggit.dev/permutationlock/32/9958_2.png) [@permutationlock](https://ziggit.dev/u/permutationlock)\
**Post date:** [December 25, 2023, 12:07pm UTC](https://ziggit.dev/t/help-with-a-todo-app-using-io-and-switch/2586/4 "2023-12-25T12:07:04Z")

</div>

> [@z1fire](#):
>
> ` const cleaned = std.mem.trimRight(u8, line_buf[0..amt], "r\n");`

I think you meant `"\r\n"` here. If you are on Windows, the error is because you are passing `\r` to parseUnsigned.

---

<div class="post-metadata">

**Author:** ![z1fire](https://ziggit.dev/user_avatar/ziggit.dev/z1fire/32/742_2.png) [@z1fire](https://ziggit.dev/u/z1fire)\
**Post date:** [December 25, 2023, 12:08pm UTC](https://ziggit.dev/t/help-with-a-todo-app-using-io-and-switch/2586/5 "2023-12-25T12:08:21Z")

</div>

Oh thank you!!! Such a little nuance. It works for me nowA

---

<div class="post-metadata">

**Author:** ![z1fire](https://ziggit.dev/user_avatar/ziggit.dev/z1fire/32/742_2.png) [@z1fire](https://ziggit.dev/u/z1fire)\
**Post date:** [December 25, 2023, 12:10pm UTC](https://ziggit.dev/t/help-with-a-todo-app-using-io-and-switch/2586/6 "2023-12-25T12:10:30Z")

</div>

I need to learn the slight differences that programming in zig has when comes to windows vs linux.

---

<div class="post-metadata">

**Author:** ![mscott9437](https://ziggit.dev/user_avatar/ziggit.dev/mscott9437/32/574_2.png) [@mscott9437](https://ziggit.dev/u/mscott9437)\
**Post date:** [December 25, 2023, 3:05pm UTC](https://ziggit.dev/t/help-with-a-todo-app-using-io-and-switch/2586/7 "2023-12-25T15:05:26Z")

</div>

check out args and argsWithAllocator in [std.process](https://ziglang.org/documentation/master/std/#A;std:process). when you grab command line arguments you need to use an allocator in windows, which will also work on linux. but if you are only on linux, you can just use std.process.args.

---

<div class="post-metadata">

**Author:** ![z1fire](https://ziggit.dev/user_avatar/ziggit.dev/z1fire/32/742_2.png) [@z1fire](https://ziggit.dev/u/z1fire)\
**Post date:** [December 25, 2023, 4:29pm UTC](https://ziggit.dev/t/help-with-a-todo-app-using-io-and-switch/2586/8 "2023-12-25T16:29:17Z")

</div>

I will definitely have to look into that!

---

<div class="post-metadata">

**Author:** ![z1fire](https://ziggit.dev/user_avatar/ziggit.dev/z1fire/32/742_2.png) [@z1fire](https://ziggit.dev/u/z1fire)\
**Post date:** [December 25, 2023, 7:24pm UTC](https://ziggit.dev/t/help-with-a-todo-app-using-io-and-switch/2586/9 "2023-12-25T19:24:21Z")

</div>

Do you know any other sources that talk about argsWtihAllocator, maybe with examples or something? I’m not really following or understanding it from the standard documentation.

---

<div class="post-metadata">

**Author:** ![mscott9437](https://ziggit.dev/user_avatar/ziggit.dev/mscott9437/32/574_2.png) [@mscott9437](https://ziggit.dev/u/mscott9437)\
**Post date:** [December 25, 2023, 7:37pm UTC](https://ziggit.dev/t/help-with-a-todo-app-using-io-and-switch/2586/10 "2023-12-25T19:37:31Z")

</div>

You can find @ratfactor 's explanation [here](https://ziggit.dev/t/read-command-line-arguments/220/7), in particular the example using std.process.argsAlloc. std.process.argsAlloc works basically the same as std.process.args, expect that you have to pass in an allocator along with it and add an additional defer statement `defer std.process.argsFree(allocator, args);` afterwords to clear the memory. argsAlloc() will work for both Windows and Linux, whereas args() only works for Linux.

---

<div class="post-metadata">

**Author:** ![z1fire](https://ziggit.dev/user_avatar/ziggit.dev/z1fire/32/742_2.png) [@z1fire](https://ziggit.dev/u/z1fire)\
**Post date:** [December 25, 2023, 7:54pm UTC](https://ziggit.dev/t/help-with-a-todo-app-using-io-and-switch/2586/11 "2023-12-25T19:54:55Z")

</div>

Ok thank you, so it matters what system your on when writing, but when you cross compile does that matter still? Like is it best practice to just use the one that works on both?

---

<div class="post-metadata">

**Author:** ![mscott9437](https://ziggit.dev/user_avatar/ziggit.dev/mscott9437/32/574_2.png) [@mscott9437](https://ziggit.dev/u/mscott9437)\
**Post date:** [December 25, 2023, 8:02pm UTC](https://ziggit.dev/t/help-with-a-todo-app-using-io-and-switch/2586/12 "2023-12-25T20:02:25Z")

</div>

To answer your question I just sent you a message. Check your inbox on the left pane. So you can follow up with me there if you have any more questions. Since this thread was already solved. I am available on the messages or you can start a new thread if you want.
