View previous topic :: View next topic |
Author |
Message |
scnclr
Joined: 23 Apr 2005 Posts: 3
|
Posted: Sat Apr 23, 2005 1:54 pm Post subject: variable won't calculate right (mayba a newbee question?) |
|
|
Hi there!
i came along, read some things about d, saw an incredible demonstration of d(http://www.asahi-net.or.jp/~cs8k-cyu/windows/tt_e.html) and decided to go a little bit in d. i have some programming skills in flash and processing (java) and now it's time to move over a real compiler language.
so far so good. now i am testing the beginner tutorials. but, when i tried this
Code: |
int main()
{
for (int i=1; i<=10; i++)
{
printf("?d\n", i);
}
return 0;
}
|
i getonly the numbers 1..8 in return. when i tried the variables i = 0; i<100 i get 0..80, 8. huh?
the next example:
Code: |
import std.c.stdio; /* for using printf */
void main()
{
int i, j;
i = 12 * 12; /* multiplication */
printf("12 x 12 = ?d\n", i); /* i should be 144 */
j = i / 8; /* division */
printf("144 / 8 = ?d\n", j); /* j should be 18 */
i -= 44; /* same as saying "i = i - 44;" */
printf("144 - 44 = ?d\n", j); /* i should be 100 */
}
|
the last calculation should equal 100. but i get 18.
when testing this:
Code: |
import std.c.stdio; /* for using printf */
void main()
{
int i, j;
i = 12 * 12; /* multiplication */
printf("12 x 12 = ?d\n", i); /* i should be 144 */
i -= 44; /* same as saying "i = i - 44;" */
printf("144 - 44 = ?d\n", j); /* i should be 100 */
}
|
i get as final result 0.
what went wrong?? _________________ node3000.org
Last edited by scnclr on Sat Apr 23, 2005 2:12 pm; edited 1 time in total |
|
Back to top |
|
|
scnclr
Joined: 23 Apr 2005 Posts: 3
|
Posted: Sat Apr 23, 2005 2:07 pm Post subject: |
|
|
hmm, stange. i tried again the 'for loop' and this is now working. the other example still fails. maybe because of the "import..." in the first line? _________________ node3000.org |
|
Back to top |
|
|
Derek Parnell
Joined: 22 Apr 2004 Posts: 408 Location: Melbourne, Australia
|
Posted: Sat Apr 23, 2005 5:14 pm Post subject: Re: variable won't calculate right (mayba a newbee question? |
|
|
Glad you got the first example working. It worked for me too.
scnclr wrote: |
the next example:
Code: |
import std.c.stdio; /* for using printf */
void main()
{
int i, j;
i = 12 * 12; /* multiplication */
printf("12 x 12 = ?d\n", i); /* i should be 144 */
j = i / 8; /* division */
printf("144 / 8 = ?d\n", j); /* j should be 18 */
i -= 44; /* same as saying "i = i - 44;" */
printf("144 - 44 = ?d\n", j); /* i should be 100 */
}
|
|
You've coded a bug. The line
Code: |
printf("144 - 44 = ?d\n", j); /* i should be 100 */
|
should read
Code: |
printf("144 - 44 = ?d\n", i); /* i should be 100 */
|
Same with the next example. _________________ --
Derek
skype name: derek.j.parnell |
|
Back to top |
|
|
Derek Parnell
Joined: 22 Apr 2004 Posts: 408 Location: Melbourne, Australia
|
Posted: Sat Apr 23, 2005 5:50 pm Post subject: |
|
|
Also, before you get to deep into D, try using writef() instead. printf() is an old C function and writef() is its D equivalent.
Code: |
import std.stdio; // required for writef()
void main()
{
int i, j;
i = 12 * 12; /* multiplication */
writefln("12 x 12 = ?d", i); /* i should be 144 */
j = i / 8; /* division */
writefln("144 / 8 = ?d", j); /* j should be 18 */
i -= 44; /* same as saying "i = i - 44;" */
writefln("144 - 44 = ?d", i); /* i should be 100 */
}
|
_________________ --
Derek
skype name: derek.j.parnell |
|
Back to top |
|
|
scnclr
Joined: 23 Apr 2005 Posts: 3
|
Posted: Sun Apr 24, 2005 8:46 am Post subject: |
|
|
oh, damn. what a silly mistake! i copy&pasted it from the original tutorial, so i think it shluld be corrected there too!
PS: what is the '?d\n' thing at the printf? where can i get good help upon syntax-questions? are there any good documented references? (the reference coming with d seems too cryptic for me..)
thanks and greetings
martin _________________ node3000.org |
|
Back to top |
|
|
Derek Parnell
Joined: 22 Apr 2004 Posts: 408 Location: Melbourne, Australia
|
Posted: Sun Apr 24, 2005 9:30 am Post subject: |
|
|
scnclr wrote: | PS: what is the '?d\n' thing at the printf? where can i get good help upon syntax-questions? are there any good documented references? (the reference coming with d seems too cryptic for me..)
|
The writef() function uses format codes to format your data in specific ways that you choose. These codes begin with a ? symbol and end with specific letter codes. The full list of these format codes can be found in the DMD documentation ...
http://www.digitalmars.com/d/std_format.html
The full list can be very confusing at first. So I'll just mention the common codes used.
?s -- Format data as a string
?d -- Format data as an integer
?f -- Format data as a floating point number
After the ? and before the final letter code, you can specify to output length of the formatted data.
?10s -- formats the data to take up ten characters.
For floating points, you can also specify how many decimals to display...
?10.2f -- A total of ten characters, including two decimal places. _________________ --
Derek
skype name: derek.j.parnell |
|
Back to top |
|
|
jcc7
Joined: 22 Feb 2004 Posts: 657 Location: Muskogee, OK, USA
|
Posted: Sun Apr 24, 2005 9:40 am Post subject: |
|
|
scnclr wrote: | what a silly mistake! i copy&pasted it from the original tutorial, so i think it shluld be corrected there too! | Sorry about that. I've fixed that example in the tutorial now. You might be the first person to the first person to try that example (or at least you're the first person to point out that mistake). I've tried to check and double-check the examples for accuracy, but mistakes can still slip in.
Please let me know if you find anything else weird in the examples. (By the way, I've stopped putting in new printf examples, and I plan to start replacing the current printf examples with writef code for consistency.) |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|