Reading two times

I’m new to zig and am doing basic stuff but trying to make a simple calculator by reading two numbers in a row and can’t because the second reader overlaps and won’t let you enter the first number.

const options =
        \\Hola
        \\1.- Sumar
        \\2.- Restar
        \\3.- Multiplicar
        \\4.- Dividir
        \\:: 
    ;

    const writer = std.io.getStdOut().writer();
    try writer.print("{s}", .{options});
    var input: [1]u8 = undefined;
    _ = try std.io.getStdIn().read(&input);

    const stdin = std.io.getStdIn();
    try writer.print("First number\n: ", .{});

    var buffer: [256]u8 = undefined;
    _ = try stdin.reader().readUntilDelimiterOrEof(&buffer, '\n');
    const n1 = buffer[0];
    _ = try stdin.reader().readUntilDelimiterOrEof(&buffer, '\n');
    const n2 = buffer[0];

    const input_value = input[0] - '0';

    switch (input_value) {
        1 => {
            try writer.print("{any} + {any}\n", .{ n1, n2 });
        },
        2 => {},
        3 => {},
        4 => {},
        else => unreachable,
    }

You are reading the second number into the same buffer.
Just use another buffer.

Thanks, i thought that if i save the value first i can reuse the buffer.

Actually, I think the problem was in the first read to the [1]u8 which doesn’t leave space for the newline. The following works and has comments where I made changes:

const std = @import("std");

pub fn main() !void {
    const options =
        \\Hola
        \\1.- Sumar
        \\2.- Restar
        \\3.- Multiplicar
        \\4.- Dividir
        \\:: 
    ;

    const writer = std.io.getStdOut().writer();
    try writer.print("{s}", .{options});

    // Changed from [1]u8 to [2]u8 to account for newline '\n'
    var input: [2]u8 = undefined;
    _ = try std.io.getStdIn().read(&input);

    const stdin = std.io.getStdIn();
    var buffer: [256]u8 = undefined;

    try writer.print("First number\n: ", .{});
    // Use `orelse` to turn the optional into a stirng (default "42").
    var str = try stdin.reader().readUntilDelimiterOrEof(&buffer, '\n') orelse "42";
    // Parse the string as a base 10 `usize`.
    const n1 = try std.fmt.parseInt(usize, str, 10);

    // Prompt for the second number.
    try writer.print("Second number\n: ", .{});
    str = try stdin.reader().readUntilDelimiterOrEof(&buffer, '\n') orelse "42";
    const n2 = try std.fmt.parseInt(usize, str, 10);

    const input_value = input[0] - '0';

    switch (input_value) {
        1 => {
            try writer.print("{any} + {any}\n", .{ n1, n2 });
        },
        2 => {},
        3 => {},
        4 => {},
        else => unreachable,
    }
}

Yes, I was wrong, sorry.

@dude_the_builder, could you, please, unmark that post as solution.

No need for apologies, it was a subtle little detail to detect. I unmarked it but let’s see what @Asmus has to say before marking a new solution.

Yeah, that was the solution, I didn’t take into account the ‘\n’. Thanks!!