View previous topic :: View next topic |
Author |
Message |
ViolentAJ
Joined: 05 Feb 2009 Posts: 56
|
Posted: Sun Feb 08, 2009 10:33 pm Post subject: Simple Problem |
|
|
I'm writing a simpleprogram to test I/O.
Code: |
module TestingIO;
import std.stdio;
import std.cstream;
void main(string[]args){
string str;
const string strA = "exit";
do {
writefln("Please input a string> ");
str = readln();
if(str != strA){
writefln(str);
}
} while(str != strA);
}
|
That is my program. It is supposed to tell the user to input a string and it'll output the same string that was input until "exit" is input. For whatever reason, str is never checked against strA properly, because the program doesn't terminate. I have tried a few other outputs and such to debug, and it doesn't work. I've come to the conclusion that str is never truly checked against strA.
I tried the same algorithm in other languages, and of course it worked. Is there something worng I am doing here in D? |
|
Back to top |
|
|
csauls
Joined: 27 Mar 2004 Posts: 278
|
Posted: Tue Feb 10, 2009 12:11 am Post subject: |
|
|
readln() returns the string complete with its line terminator, so when you enter "exit" the string returned is actually "exit\n", which doesn't match. It is a simple fix, though.
Code: |
module TestingIO ;
import std .stdio ;
import std .string ;
void main (string[] args) {
static invariant strA = "exit" ;
string
str ;
do {
writeln("Please input a string> ");
str = chomp(readln());
if (str != strA) {
writeln(str);
}
}
while (str != strA);
}
|
A couple of things to point out here. First, note the call to chomp() from std.string, which will strip the newline off for you. That alone should fix your problem.
Second, note that I'm passing str to writeln() rather than writefln(). This is generally a good idea when just writing out strings, as writeln() won't check the string for format specifiers, the presence of which would cause errors with writefln(). Sure, this is just a little experiment program, and you probably already knew that, but it doens't hurt to point it out. _________________ Chris Nicholson-Sauls |
|
Back to top |
|
|
ViolentAJ
Joined: 05 Feb 2009 Posts: 56
|
Posted: Tue Feb 10, 2009 8:14 pm Post subject: |
|
|
Thanks. I'm a total n00b to D (and a general n00b to programming, with experience primarily in Java and ActionScript but a little bit of Python here and there). |
|
Back to top |
|
|
ViolentAJ
Joined: 05 Feb 2009 Posts: 56
|
Posted: Wed Feb 11, 2009 12:10 am Post subject: |
|
|
Well, I tried "writeln" instead of "writefln", and I got this compiler output:
Code: |
C:\WorkingInD\Test\TestingIO.d(11): Error: undefined identifier writeln
C:\WorkingInD\Test\TestingIO.d(11): Error: function expected before (), not writeln of type int
C:\WorkingInD\Test\TestingIO.d(14): Error: undefined identifier writeln
C:\WorkingInD\Test\TestingIO.d(14): Error: function expected before (), not writeln of type int
|
Other than that it works though. Thanks for the help. |
|
Back to top |
|
|
doob
Joined: 06 Jan 2007 Posts: 367
|
Posted: Wed Feb 11, 2009 4:43 am Post subject: |
|
|
writeln is only available in D2 so you'll have to use writefln. Watch out for "%" in strings. |
|
Back to top |
|
|
csauls
Joined: 27 Mar 2004 Posts: 278
|
Posted: Wed Feb 11, 2009 8:23 am Post subject: |
|
|
Oy, my mistake... when I see 'string' as a type now I automatically think D2. ^^
Anyhow, the D1 alternative for safe string output is simple enough:
Code: | writefln("%s", str); |
_________________ Chris Nicholson-Sauls |
|
Back to top |
|
|
ViolentAJ
Joined: 05 Feb 2009 Posts: 56
|
Posted: Wed Feb 11, 2009 2:48 pm Post subject: |
|
|
Would you suggest me getting D2? If so, will I have to change a lot of stuff?
I'm sorry, I'm new to progging out of Java/Actionscript so...
I've never used %s and things of that nature in my life. A bit confusing heh. |
|
Back to top |
|
|
csauls
Joined: 27 Mar 2004 Posts: 278
|
Posted: Thu Feb 12, 2009 8:16 am Post subject: |
|
|
Probably best to cut teeth on D1, being a stable language. D2 is still under active development, so breaking changes can and do occur.
Format specifiers (%s, etc) aren't bad at all, once you play with them a little. You can read up on them here:
http://digitalmars.com/d/1.0/phobos/std_format.html#format-string
Generally speaking, if you don't have any special needs for how a given value is output, just use %s to get the default.
Code: |
writefln("myAssoc.length == %s", myAssoc.length);
foreach (key, value; myAssoc) {
writefln(" %s => %s", key, value);
}
|
_________________ Chris Nicholson-Sauls |
|
Back to top |
|
|
|