www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Need som assistense with readf();

reply "Mr. P-teo" <dthp18 gmail.com> writes:
So iv just been getting into D programming, im liking it alot so 
far but i have come across one issue that i am unable to find a 
solution for.

Basically whenever i use readf(); to gather console input it wont 
run the program if there is code afterwards.

Here is an example:


import std.stdio;

int main(){
     long number, number2;
     write("Enter an integer: ");
     readf("%d", &number);
     return 0;
}


That works fine, but if i try and imput 2 numbers to seperate 
variables it doesn't work.


import std.stdio;

int main(){
     long number, number2;
     write("Enter an integer: ");
     readf("%d", &number);
     write("another no";
     readf("%d", &number2);
     return 0;
}



can anyone help me.
Jul 07 2012
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 07/07/2012 03:35 PM, Mr. P-teo wrote:

 Basically whenever i use readf(); to gather console input it wont run
 the program if there is code afterwards.

readf() does not automatically consume the end-of-line character.
 That works fine, but if i try and imput 2 numbers to seperate variables
 it doesn't work.


 import std.stdio;

 int main(){
 long number, number2;
 write("Enter an integer: ");
 readf("%d", &number);

A space character in the format string reads and ignores any number of whitespace at that position.
 write("another no";
 readf("%d", &number2);
 return 0;

Just insert spaces before the format specifiers: import std.stdio; int main(){ long number, number2; write("Enter an integer: "); readf(" %d", &number); write("another no"); readf(" %d", &number2); writeln(number, ' ', number2); return 0; } Ali -- D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html
Jul 07 2012
prev sibling parent "Mr. P-teo" <dthp18 gmail.com> writes:
 Just insert spaces before the format specifiers:

 import std.stdio;

 int main(){
     long number, number2;
     write("Enter an integer: ");
     readf(" %d", &number);
     write("another no");
     readf(" %d", &number2);

     writeln(number, ' ', number2);
     return 0;
 }

 Ali

Thanks very much, that got it sorted,
Jul 08 2012