Re: [Vala] Beginners question about keyboard input



Clément DAVID wrote:
Hi,

I don't use the Genie syntax but a working sample for vala is :

using GLib;

public class SumCol {

        public static it main(string [] args)
        {
                int MAXLINELEN = 128;
                
                int sum = 0;
                char[] str = new char [MAXLINELEN];
                
                while(stdin.gets(str) != null)
                {
                        sum += ((string)str).to_int();
                }
                
                stdout.printf("%d\n",sum);
                return 0;
        }
}
<<

You can simply cast a char array to string as the string class is only
a wrapper around NULL terminated unichar array.

I would suggest using GNU Readline for string input:

--------
namespace ReadLine {
    [CCode (cheader_filename = "readline/readline.h", cname = "readline")]
    public extern string? read_line (string prompt);
}

static int main () {
    var name = ReadLine.read_line ("Please enter your name: ");
    if (name != null && name != "") {
        stdout.printf ("Hello, %s\n", name);
    }
    return 0;
}
--------
$ valac -X -lreadline readlinesample.vala
$ ./readlinesample


Regards,

Frederik



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]