Help with a todo app using io and switch

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.

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;
}

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

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:

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

1 Like

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

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

check out args and argsWithAllocator in 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.

1 Like

I will definitely have to look into that!

1 Like

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.

You can find @ratfactor 's explanation here, 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.

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?

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.

1 Like