www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - "readln" after "readf"

reply pascal111 <judas.the.messiah.111 gmail.com> writes:
I've a problem when I'm using "readln" after "readf" that I 
couldn't see my program rest and the lines of the execution ran 
fast:

module main;

import std.stdio;
import std.string;
import std.conv;

int main(string[] args)
{

         char[] yy;
         int x,y,z;


         writef("Enter a number: ");
         readf(" %d",&x);


         writef("Enter a number: ");
         readf(" %d",&y);

         z=x+y;
         writefln("Hi! %d",z);

         writef("Enter a number: ");
         readln(yy);
         yy = strip(yy);
         x=to!int(yy);

         writef("Enter a number: ");
         readln(yy);
         yy = strip(yy);
         y=to!int(yy);

         z=x+y;
         writefln("Hi! %d",z);

     	return 0;
     }
Sep 21 2023
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On Thursday, 21 September 2023 at 09:14:14 UTC, pascal111 wrote:
 I've a problem when I'm using "readln" after "readf" that I 
 couldn't see my program rest and the lines of the execution ran 
 fast:

 module main;

 import std.stdio;
 import std.string;
 import std.conv;

 int main(string[] args)
 {

         char[] yy;
         int x,y,z;


         writef("Enter a number: ");
         readf(" %d",&x);


         writef("Enter a number: ");
         readf(" %d",&y);

         z=x+y;
         writefln("Hi! %d",z);

         writef("Enter a number: ");
         readln(yy);
         yy = strip(yy);
         x=to!int(yy);

         writef("Enter a number: ");
         readln(yy);
         yy = strip(yy);
         y=to!int(yy);

         z=x+y;
         writefln("Hi! %d",z);

     	return 0;
     }
Your readfs are not consuming the newline at the end. Add `\n` to the end of the format text. -Steve
Sep 21 2023
parent pascal111 <judas.the.messiah.111 gmail.com> writes:
On Thursday, 21 September 2023 at 11:30:02 UTC, Steven 
Schveighoffer wrote:
 On Thursday, 21 September 2023 at 09:14:14 UTC, pascal111 wrote:
     [...]
Your readfs are not consuming the newline at the end. Add `\n` to the end of the format text. -Steve
It works fine now, thanks!
Sep 21 2023