www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - using readf in D

reply "Carlos" <checoimg gmail.com> writes:
Hi guys! I'm writing a little program in D from another version I 
did for my homework written in C. But it doesn't work very well. 
First it takes two reads for the first input from the user and 
second it only calculates tcsleep.

Here is the code:

import std.stdio;
import std.c.stdlib;
void main()
{
immutable sitc = 1.66;
immutable sleepc = 1.08;
float tcsleep, tcsit, tc;
int minsleep, minsit;
write("Input number of minutes sleep : \n");
readf("%d ", &minsleep);

write("Input number of minutes sitting : \n");
readf("%d ", &minsit);

write("Thanks!\n");
tcsleep = minsit*sitc;
tcsit = minsleep*sleepc;
tc = tcsleep+tcsit;
writeln("Your total calories is : ", tc);
exit (0);
}
Apr 20 2013
next sibling parent reply "Chris Cain" <clcain uncg.edu> writes:
On Saturday, 20 April 2013 at 12:52:30 UTC, Carlos wrote:
 Hi guys! I'm writing a little program in D from another version 
 I did for my homework written in C. But it doesn't work very 
 well. First it takes two reads for the first input from the 
 user and second it only calculates tcsleep.

 Here is the code:

 import std.stdio;
 import std.c.stdlib;
 void main()
 {
 immutable sitc = 1.66;
 immutable sleepc = 1.08;
 float tcsleep, tcsit, tc;
 int minsleep, minsit;
 write("Input number of minutes sleep : \n");
 readf("%d ", &minsleep);

 write("Input number of minutes sitting : \n");
 readf("%d ", &minsit);

 write("Thanks!\n");
 tcsleep = minsit*sitc;
 tcsit = minsleep*sleepc;
 tc = tcsleep+tcsit;
 writeln("Your total calories is : ", tc);
 exit (0);
 }
First off, great on you for rewriting an old assignment in a new language. That's one of the better ways I've found to learn new languages (and I learn new languages all the time) because it allows you to focus on the new language and how you might use that language rather than solving the problem at hand. Second, this probably should be in D.learn (if I don't say it, someone else will). http://forum.dlang.org/group/digitalmars.D.learn if you're using the web interface. Third, "exit(0)" is redundant in D. It'll exit with code 0 as long as execution terminates normally (i.e., no exceptions thrown). Finally, what "doesn't work very well"? Without more information, I don't think anyone will be able to help you. Take care.
Apr 20 2013
parent 1100110 <0b1100110 gmail.com> writes:
"Chris Cain" <clcain uncg.edu> Wrote in message:
 On Saturday, 20 April 2013 at 12:52:30 UTC, Carlos wrote:
 Hi guys! I'm writing a little program in D from another version 
 I did for my homework written in C. But it doesn't work very 
 well. First it takes two reads for the first input from the 
 user and second it only calculates tcsleep.

 Here is the code:

 import std.stdio;
 import std.c.stdlib;
 void main()
 {
 immutable sitc = 1.66;
 immutable sleepc = 1.08;
 float tcsleep, tcsit, tc;
 int minsleep, minsit;
 write("Input number of minutes sleep : \n");
 readf("%d ", &minsleep);

 write("Input number of minutes sitting : \n");
 readf("%d ", &minsit);

 write("Thanks!\n");
 tcsleep = minsit*sitc;
 tcsit = minsleep*sleepc;
 tc = tcsleep+tcsit;
 writeln("Your total calories is : ", tc);
 exit (0);
 }
First off, great on you for rewriting an old assignment in a new language. That's one of the better ways I've found to learn new languages (and I learn new languages all the time) because it allows you to focus on the new language and how you might use that language rather than solving the problem at hand. Second, this probably should be in D.learn (if I don't say it, someone else will). http://forum.dlang.org/group/digitalmars.D.learn if you're using the web interface. Third, "exit(0)" is redundant in D. It'll exit with code 0 as long as execution terminates normally (i.e., no exceptions thrown). Finally, what "doesn't work very well"? Without more information, I don't think anyone will be able to help you. Take care.
as soon as I saw readf I knew what was wrong. its a pretty common issue. -- sigh... ----Android NewsGroup Reader---- http://www.piaohong.tk/newsgroup
Apr 20 2013
prev sibling next sibling parent "Chris Cain" <clcain uncg.edu> writes:
On Saturday, 20 April 2013 at 12:52:30 UTC, Carlos wrote:
 First it takes two reads for the first input from the user and 
 second it only calculates tcsleep.
Oh, I see what you mean by this now. You were describing the problem (I thought you were telling us what the program does). Ok, so this will fix the problem: readf(" %d", &minsleep); and readf(" %d", &minsit); Notice, the space is before the %d instead of after. The space means "eat all of the whitespace before the next input". So, as you can see, it has to "take two reads" because it's waiting for some non-whitespace information. If you kept hitting enter (or inputting spaces), you'd see that it'd keep prompting you.
Apr 20 2013
prev sibling parent reply "Chris Cain" <clcain uncg.edu> writes:
On Saturday, 20 April 2013 at 12:52:30 UTC, Carlos wrote:
 First it takes two reads for the first input from the user and 
 second it only calculates tcsleep.
Oh, I see what you mean by this now. You were describing the problem (I thought you were telling us what the program does). Ok, so this will fix the problem: readf(" %d", &minsleep); and readf(" %d", &minsit); Notice, the space is before the %d instead of after. The space means "eat all of the whitespace before the next input". So, as you can see, it has to "take two reads" because it's waiting for some non-whitespace information. If you kept hitting enter (or inputting spaces), you'd see that it'd keep prompting you.
Apr 20 2013
parent "Carlos" <checoimg gmail.com> writes:
On Saturday, 20 April 2013 at 14:00:50 UTC, Chris Cain wrote:
 On Saturday, 20 April 2013 at 12:52:30 UTC, Carlos wrote:
 First it takes two reads for the first input from the user and 
 second it only calculates tcsleep.
Oh, I see what you mean by this now. You were describing the problem (I thought you were telling us what the program does). Ok, so this will fix the problem: readf(" %d", &minsleep); and readf(" %d", &minsit); Notice, the space is before the %d instead of after. The space means "eat all of the whitespace before the next input". So, as you can see, it has to "take two reads" because it's waiting for some non-whitespace information. If you kept hitting enter (or inputting spaces), you'd see that it'd keep prompting you.
Ok now it works perfectly. Thanks you very much Chris Cain! I was using it with space before and after because in the book "The D Progamming language" by Andrei Alexandrescu page 22 it was using it that way but I didnt read everything maybe I missed that detail.
Apr 20 2013