digitalmars.D.learn - User input readline || readf
- kerze (15/15) Apr 23 2015 Hy, i'm new @ D and i come from python.
- kerze (44/44) Apr 23 2015 hmm the paste is away ?!? here i post my source one more time.
- Steven Schveighoffer (6/7) Apr 23 2015 readf reads the element from the stream, and NOTHING MORE, it leaves the...
- "Jacques =?UTF-8?B?TcO8bGxlciI=?= <jacques.mueller gmx.de> (13/17) Apr 23 2015 The paste is still there.
- kerze (2/3) Apr 23 2015 So many Thanks @Steven and Jacques, answer's more than a only;
- Dennis Ritchie (43/58) Apr 23 2015 I can offer, for example, such an option:
- Dennis Ritchie (49/49) Apr 23 2015 It also works:
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
Apr 23 2015
hmm the paste is away ?!? here i post my source one more time.
[code]
import std.stdio;
import std.string;
struct Human {
string name;
ushort age;
};
void print_human_list(Human[] human_list){
foreach(human; human_list){
writeln(human.name);
writeln(human.age);
}
writeln("");
}
void add_new_human(Human[] human_list){
ushort age; //
write("Name: "); // The Output is
string name = strip(stdin.readln()); // Name: Age:
write("Age: "); // And the Error
readf(" %u", &age); //
std.conv.ConvException /usr/include/dlang/ldc/std/conv.d(1968):
Human tmp_human = {name, age}; // Unexpected 's'
when converting from type LockingTextReader to type uint
human_list ~= tmp_human;
}
int main(){
Human[] human_list;
char choice;
for(;;){
writeln("A)dd New Human.");
writeln("P)rint Human List.");
writeln("Q)uit.");
write(": ");
readf("%c", &choice);
switch(choice){
case('A') : add_new_human(human_list);
case('P') : print_human_list(human_list);
case('Q') : return 0;
default : continue;
}
}
}
[/code]
Apr 23 2015
On 4/23/15 1:17 PM, kerze wrote:The problem is, it writes instant "Name: Age:" at the standart output.readf reads the element from the stream, and NOTHING MORE, it leaves the newline on the stream. So the next readf then reads the newline as a string. You can fix this by using readf(" %s"), which means "read any whitespace, then my data". -Steve
Apr 23 2015
The paste is still there. readf leaves the \n from pressing enter in stdin, which gets read by the next function that's accessing it. I answered a similiar question in another thread: http://forum.dlang.org/post/jwxfaztgsyzwqpzajqmf forum.dlang.org I see two other mistakes in your code as well: 1. I'm pretty sure you want to pass print_human_list and add_new_human a reference to the human_list array. Just add "ref" before "Human[] human_list".void print_human_list(ref Human[] human_list)2. In D there exists something called switch fallthrough. Just add a "break;" after each case and only the matching block will be executed.case('A'): add_new_human(human_list); break;Willkommen in der D Community. ;)
Apr 23 2015
Willkommen in der D Community. ;)So many Thanks Steven and Jacques, answer's more than a only; "do this" ;)
Apr 23 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. KerzeI 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; } } }
Apr 23 2015
It also works:
-----
import std.conv;
import std.stdio;
import std.string;
struct Human {
string name;
ushort age;
}
void print_human_list(Human[] human_list) {
foreach(human; human_list) {
writeln(human.name);
writeln(human.age);
}
writeln();
}
void add_new_human(ref Human[] human_list) {
write("Name: ");
string name = readln.strip;
write("Age: ");
ushort age = readln.strip.to!ushort;
Human tmp_human = {name, age};
human_list ~= tmp_human;
}
void main() {
Human[] human_list;
for(;;) {
writeln("A)dd New Human.");
writeln("P)rint Human List.");
writeln("Q)uit.");
write(": ");
char choice = readln.strip.to!char;
switch (choice) {
case('A'):
add_new_human(human_list);
break;
case('P'):
print_human_list(human_list);
break;
case('Q'):
return;
default:
continue;
}
}
}
-----
http://ideone.com/0cS7Y4
Apr 23 2015









"kerze" <kurz.andi gmx.ch> 