Need som assistense with readf();

Ali Çehreli acehreli at yahoo.com
Sat Jul 7 16:37:46 PDT 2012


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



More information about the Digitalmars-d-learn mailing list