digitalmars.D.learn - Why does calling readln() more than once not work
- Ruby The Roobster (19/19) Jan 28 2021 I call readln() on a variable in a loop. On the next iteration,
- Ruby The Roobster (3/22) Jan 28 2021 Here is the declaration for tempz(outside the loop): char[] tempz;
- Adam D. Ruppe (6/7) Jan 28 2021 This leaves the \n at the end. A next readf thanks to the leading
- Ruby The Roobster (4/11) Jan 28 2021 I replaced readln with the following: readf(" %s\n",&tempz);
I call readln() on a variable in a loop. On the next iteration, it's as if the readln() is ignored, as it moves on to the next line apparently. Here is the code: for(int i = 1;i<1000;i++) { int tempx; int tempy; int high = 0; double highs = 0; writeln("Type in data for an egg:"); write("Chicken: "); readln(tempz); write("Width: "); readf(" %d",&tempx); write("Hight: "); readf(" %d",&tempy); data[i] = new egg(tempx,tempy,cast(string)tempz); //... Anything that will fix this?
Jan 28 2021
On Thursday, 28 January 2021 at 19:25:52 UTC, Ruby The Roobster wrote:I call readln() on a variable in a loop. On the next iteration, it's as if the readln() is ignored, as it moves on to the next line apparently. Here is the code: for(int i = 1;i<1000;i++) { int tempx; int tempy; int high = 0; double highs = 0; writeln("Type in data for an egg:"); write("Chicken: "); readln(tempz); write("Width: "); readf(" %d",&tempx); write("Hight: "); readf(" %d",&tempy); data[i] = new egg(tempx,tempy,cast(string)tempz); //... Anything that will fix this?Here is the declaration for tempz(outside the loop): char[] tempz;
Jan 28 2021
On Thursday, 28 January 2021 at 19:25:52 UTC, Ruby The Roobster wrote:readf(" %d",&tempy);This leaves the \n at the end. A next readf thanks to the leading space would ignore that \n and keep going, but a readln stops at the first \n it sees, even if it is a leftover item in the buffer from a readf before.
Jan 28 2021
On Thursday, 28 January 2021 at 19:31:35 UTC, Adam D. Ruppe wrote:On Thursday, 28 January 2021 at 19:25:52 UTC, Ruby The Roobster wrote:I replaced readln with the following: readf(" %s\n",&tempz); That seemed to do the trick(and note here, when you press enter, it stops taking input).readf(" %d",&tempy);This leaves the \n at the end. A next readf thanks to the leading space would ignore that \n and keep going, but a readln stops at the first \n it sees, even if it is a leftover item in the buffer from a readf before.
Jan 28 2021