aboutsummaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
authorJosh Wolfe <thejoshwolfe@gmail.com>2018-04-10 21:46:13 -0400
committerJosh Wolfe <thejoshwolfe@gmail.com>2018-04-10 21:46:13 -0400
commitb553b7ab83894447d53bcdc1b5a10c7cbcd2281f (patch)
treea2614c600dfbf57d7c92cfa8268767b60eb75b91 /example
parent0ba85ea6ff910c7a49ae036625b945c475c0f58c (diff)
parentee3e2790aa85c624fc952bc8326b82d72843bb17 (diff)
downloadzig-b553b7ab83894447d53bcdc1b5a10c7cbcd2281f.tar.gz
zig-b553b7ab83894447d53bcdc1b5a10c7cbcd2281f.zip
Merge branch 'master' into self-hosted-parser
Diffstat (limited to 'example')
-rw-r--r--example/guess_number/main.zig13
1 files changed, 7 insertions, 6 deletions
diff --git a/example/guess_number/main.zig b/example/guess_number/main.zig
index d26518a1ed..7178c5274a 100644
--- a/example/guess_number/main.zig
+++ b/example/guess_number/main.zig
@@ -9,8 +9,6 @@ pub fn main() !void {
var stdout_file_stream = io.FileOutStream.init(&stdout_file);
const stdout = &stdout_file_stream.stream;
- var stdin_file = try io.getStdIn();
-
try stdout.print("Welcome to the Guess Number Game in Zig.\n");
var seed_bytes: [@sizeOf(u64)]u8 = undefined;
@@ -27,12 +25,15 @@ pub fn main() !void {
try stdout.print("\nGuess a number between 1 and 100: ");
var line_buf : [20]u8 = undefined;
- const line_len = stdin_file.read(line_buf[0..]) catch |err| {
- try stdout.print("Unable to read from stdin: {}\n", @errorName(err));
- return err;
+ const line_len = io.readLine(line_buf[0..]) catch |err| switch (err) {
+ error.InputTooLong => {
+ try stdout.print("Input too long.\n");
+ continue;
+ },
+ error.EndOfFile, error.StdInUnavailable => return err,
};
- const guess = fmt.parseUnsigned(u8, line_buf[0..line_len - 1], 10) catch {
+ const guess = fmt.parseUnsigned(u8, line_buf[0..line_len], 10) catch {
try stdout.print("Invalid number.\n");
continue;
};