Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 13, 2013 D program code | ||||
---|---|---|---|---|
| ||||
import std.stdio,std.cstream; void main(string[] args) { int first; int second; write ("First Number : "); readf (" %s", &first); write ("Second Number : "); readf (" %s", &second); int result = first + second; writeln("Result: ", result); din.getc(); } _________________________________ Problem: When I run this code why it is automatically exit when it shows the result. It allows me to input first numbers and second number but when it shows the result it will totally exit and I can't see the output. I use D-IDE as my editor and dmd2 as my compiler. |
November 13, 2013 Re: D program code | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vincent | On Wednesday, 13 November 2013 at 14:36:22 UTC, Vincent wrote:
> it shows the result it will totally exit and I can't see the output.
din.getc has something in its buffer already (the newline from when you hit enter after readf - it uses the number but left the line), so it doesn't have to wait.
Simplest fix is to read two lines at the end:
writeln("Result: ", result);
readln(); // skip past the enter left from second number
readln(); // wait for the user to hit enter again to exit
(I'm using readln instead of din so you can just use std.stdio alone)
|
November 13, 2013 Re: D program code | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vincent | On 2013-11-13 15:36, Vincent wrote: > Problem: When I run this code why it is automatically exit when it shows > the > result. It allows me to input first numbers and second number > but when > it shows the result it will totally exit and I can't see the > output. > > I use D-IDE as my editor and dmd2 as my compiler. You need to tell your IDE to not close the console when the application exists. -- /Jacob Carlborg |
November 13, 2013 Re: D program code | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vincent | >
> Problem: When I run this code why it is automatically exit when it shows the
> result. It allows me to input first numbers and second number but when
> it shows the result it will totally exit and I can't see the output.
>
> I use D-IDE as my editor and dmd2 as my compiler.
Add the following line to the end of your main() function:
stdin.readln();
|
November 13, 2013 Re: D program code | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vincent | Changing readf (" %s", &second); to readf (" %s ", &second); is likely to fix it (untested). There is a "\n" symbol left in buffer from last entry (which gets read by `getc`) |
November 13, 2013 Re: D program code | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vincent | On Wednesday, 13 November 2013 at 14:36:22 UTC, Vincent wrote: > import std.stdio,std.cstream; > > void main(string[] args) > { > int first; > int second; > > write ("First Number : "); > readf (" %s", &first); > > write ("Second Number : "); > readf (" %s", &second); > > int result = first + second; > > writeln("Result: ", result); > din.getc(); > } > > _________________________________ > > Problem: When I run this code why it is automatically exit when it shows the > result. It allows me to input first numbers and second number but when > it shows the result it will totally exit and I can't see the output. > > I use D-IDE as my editor and dmd2 as my compiler. I see you've got plenty of answers so I'll just add: please ask such questions in digitalmars.D.learn http://forum.dlang.org/group/digitalmars.D.learn |
November 13, 2013 Re: D program code | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | Sorry sir my post is out of place. But thanks anyway next time I know now where will I post my topic/questions. Thanks for the help sirs... |
November 13, 2013 Re: D program code | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | On Wednesday, 13 November 2013 at 14:45:52 UTC, John Colvin wrote:
> On Wednesday, 13 November 2013 at 14:36:22 UTC, Vincent wrote:
>> import std.stdio,std.cstream;
>>
>> void main(string[] args)
>> {
>> int first;
>> int second;
>>
>> write ("First Number : ");
>> readf (" %s", &first);
>>
>> write ("Second Number : ");
>> readf (" %s", &second);
>>
>> int result = first + second;
>>
>> writeln("Result: ", result);
>> din.getc();
>> }
>>
>> _________________________________
>>
>> Problem: When I run this code why it is automatically exit when it shows the
>> result. It allows me to input first numbers and second number but when
>> it shows the result it will totally exit and I can't see the output.
>>
>> I use D-IDE as my editor and dmd2 as my compiler.
>
> I see you've got plenty of answers so I'll just add: please ask such questions in digitalmars.D.learn http://forum.dlang.org/group/digitalmars.D.learn
This wouldn't happen so often if digitalmars.D.learn appeared at the top of the page.
|
November 13, 2013 Re: D program code | ||||
---|---|---|---|---|
| ||||
Posted in reply to bachmeier | On Wednesday, 13 November 2013 at 18:39:59 UTC, bachmeier wrote:
> This wouldn't happen so often if digitalmars.D.learn appeared at the top of the page.
amen, it is so far down that sometimes i forget about it!
|
November 13, 2013 Re: D program code | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On 11/13/13 10:47 AM, Adam D. Ruppe wrote:
> On Wednesday, 13 November 2013 at 18:39:59 UTC, bachmeier wrote:
>> This wouldn't happen so often if digitalmars.D.learn appeared at the
>> top of the page.
>
> amen, it is so far down that sometimes i forget about it!
bugzillize and pullrequestize!
Andrei
|
Copyright © 1999-2021 by the D Language Foundation