Jump to page: 1 2
Thread overview
D program code
Nov 13, 2013
Vincent
Nov 13, 2013
Adam D. Ruppe
Nov 13, 2013
Jacob Carlborg
Nov 13, 2013
Dejan Lekic
Nov 13, 2013
Dicebot
Nov 13, 2013
John Colvin
Nov 13, 2013
Vincent
Nov 13, 2013
bachmeier
Nov 13, 2013
Adam D. Ruppe
Nov 13, 2013
Vladimir Panteleev
Nov 13, 2013
Adam D. Ruppe
Nov 13, 2013
Vladimir Panteleev
November 13, 2013
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
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
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
>
> 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
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
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
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
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
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
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
« First   ‹ Prev
1 2