User input readline || readf

Dennis Ritchie via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Apr 23 12:16:20 PDT 2015


On Thursday, 23 April 2015 at 17:18:01 UTC, kerze wrote:
> Hy, i'm new @ D and i come from python.
> Sorry for my engish, i understand good, but i write like a cow 
> spanish.
>
> I make a little programm to add human's name and age and a 
> function to write out alls humans in the list.
>
> Here is the source:
>
> http://dpaste.dzfl.pl/0a0da462225d
>
> The problem is, it writes instant "Name: Age:" at the standart 
> output.
>
> I'm little confuse about readf and readline, what to use. or 
> its the failure in write ?
>
> I hope anybody can help me, as long as anybody understand me 
> and my problem. ;)
>
> Greetings from Swiss.
>
> Kerze

I can offer, for example, such an option:

-----
import std.conv;
import std.stdio;
import std.string;
import std.typecons;

Tuple!(string, ushort)[] humanList;

void printHumanList() {

	foreach (e; humanList) {
		writeln(e[0]);
		writeln(e[1]);
	}
}

void addNewHuman(){

	write("Age: ");
	ushort age = readln.strip.to!ushort;

	write("Name: ");
	string name = readln.strip;

	humanList ~= tuple(name, age);
}



void main(){

	char choice;

	for(;;) {

		writeln("A)dd New Human.");
		writeln("P)rint Human List.");
		writeln("Q)uit.");
		write(": ");

		choice = readln.strip.to!char;

		switch (choice){
		case('A'):
			addNewHuman;
			break;
		case('P'):
			printHumanList;
			break;
		case('Q'):
			return;
		default:
			continue;
		}
	}
}


More information about the Digitalmars-d-learn mailing list