#How to Add two optional variables

1 messages · Page 1 of 1 (latest)

daring roost
#

I had written Zig program. which is get values from user and add those two values and display them but problem is how to add two optional variables

rapid ginkgo
#

if you are certain they have a value you can use either variable.? or, more secure, variable orelse your_preferred_way_to_handle_the_error

#

you can also use the if (optional_variable) |variable| pattern but it will get a bit more verbose

#

I am not sure I grasped the meaning of your question, if I don't don't hesitate to ask again

daring roost
#

I am new to zig and I didn't know how to get user input like C function scanf or gets is there any way to get user inputs like C?

shell copper
#

The 4096 is the maximum number of bytes to read before giving up - which is useful if the amount of data being read is very large, for instance.
But for a line of user input, a relatively small value is probably more useful, depending on exactly what you're doing.

rapid ginkgo
#

yeah, it's a little more convoluted in Zig because you have to be very specific. In scanf you get a ton of implicit stuff

shell copper
#

Right.

#

There also readUntilDelimiterOrEof which takes a buffer to write into instead of allocating, if that's more to your liking, as well, incidentally.

daring roost
shell copper
#

Should be 4096))

daring roost
#

Allocator also does not declared

shell copper
#

You can put this in main, and then pass this allocator around to anything that needs it.