Skip to main content



      Home
Home » Newcomers » Newcomers » Running interactive command line program
Running interactive command line program [message #257061] Sun, 25 May 2008 02:47 Go to next message
Eclipse UserFriend
Originally posted by: lbalbalba.gmail.com

Hi,


Ive just build the following simple commandline C program in Eclipse, that
asks for your name and then prints it :


#include <stdio.h>
int main()
{
char me[20];
printf("What is your name?");
scanf("%s",&me);
printf("Darn glad to meet you, %s!\n",me);
return(0);
}


But when I run it with the 'Run' button in Eclipse, it doesn't seem to
work right in the console window. It doesn't print out the "What is your
name" text, and does not appear to wait for input by the user.

Maybe there is something I am obviously doing wrong here, but what is it ?
How do I make Eclipse run the program correctly ?


Thanks in advance,


John Smith
Re: Running interactive command line program [message #257065 is a reply to message #257061] Sun, 25 May 2008 04:42 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: subs._nospam_consertum.com

This is because of buffering in the C library. You will either need to
put a "\n" at the end of your printf, or you should fflush the output.

John Smith wrote:
>
> Hi,
>
>
> Ive just build the following simple commandline C program in Eclipse,
> that asks for your name and then prints it :
>
>
> #include <stdio.h>
> int main()
> {
> char me[20];
> printf("What is your name?");
> scanf("%s",&me);
> printf("Darn glad to meet you, %s!\n",me);
> return(0);
> }
>
>
> But when I run it with the 'Run' button in Eclipse, it doesn't seem to
> work right in the console window. It doesn't print out the "What is your
> name" text, and does not appear to wait for input by the user.
> Maybe there is something I am obviously doing wrong here, but what is it
> ? How do I make Eclipse run the program correctly ?
>
>
> Thanks in advance,
>
>
> John Smith
>


--
Derek
Re: Running interactive command line program [message #257074 is a reply to message #257065] Sun, 25 May 2008 11:38 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: lbalbalba.gmail.com

Thanks for clearing that up for me. Still, im somewhat confused as when I
run the executable from a command prompt outside of eclipse, it works just
as I expect: It asks the question, waits for input, and then presents the
output. Why is this behavior from the Windows commandline different than
from running it from within Eclipse ?


Regards,


John Smith
Re: Running interactive command line program [message #257078 is a reply to message #257074] Sun, 25 May 2008 12:16 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: subs._nospam_consertum.com

John Smith wrote:
>
> Thanks for clearing that up for me. Still, im somewhat confused as when
> I run the executable from a command prompt outside of eclipse, it works
> just as I expect: It asks the question, waits for input, and then
> presents the output. Why is this behavior from the Windows commandline
> different than from running it from within Eclipse ?
>
>
> Regards,
>
>
> John Smith
>
Because they are entirely different things. You'll find that most
"command line processors" (windows cmd, linux sh/csh/bash/...) will
automatically flush output when asked to read. The Eclipse console is
NOT a command line processor and doesn't have this auto-flushing behavior.

--
Derek
Re: Running interactive command line program [message #257081 is a reply to message #257078] Sun, 25 May 2008 13:10 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: lbalbalba.gmail.com

Ok, thanks for clearing that up, again. But adding the suggested \n to the
first printf statement does not solve the problem: When run in eclipse the
executable still waits for input, and only when I hit the enter key does
it do the actual print statements ... ?
Re: Running interactive command line program [message #257084 is a reply to message #257078] Sun, 25 May 2008 13:12 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: lbalbalba.gmail.com

Ok, thanks for clearing that up, again. But adding the suggested \n to the
first printf statement does not solve the problem: When run in eclipse the
executable still waits for input, and only when I hit the enter key does
it do the actual print statements ... ?
Re: Running interactive command line program [message #257160 is a reply to message #257084] Sun, 25 May 2008 13:38 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: subs._nospam_consertum.com

John Smith wrote:
> Ok, thanks for clearing that up, again. But adding the suggested \n to
> the first printf statement does not solve the problem: When run in
> eclipse the executable still waits for input, and only when I hit the
> enter key does it do the actual print statements ... ?
>
try fflush then.


--
Derek
Re: Running interactive command line program [message #257168 is a reply to message #257160] Mon, 26 May 2008 13:57 Go to previous message
Eclipse UserFriend
Originally posted by: lbalbalba.gmail.com

Yes, fflush worked. Changing the program to :

#include <stdio.h>
int main()
{
char me[20];
printf("What is your name?");
fflush(stdout);
scanf("%s",&me);
printf("Darn glad to meet you, %s!\n",me);
return(0);
}

made it behave the same as when run on the command line.


Sorry for my ignorance, but Im not only new to Eclipse; Im new to C as
well and havent gotten any farther than using printf and scanf yet.

Thanks for the help,


Regards,

John Smith
Previous Topic:Reg. Credentials Provider in RSE
Next Topic:Image in JLabel in java application not showing when run in Eclipse
Goto Forum:
  


Current Time: Wed May 28 08:50:09 EDT 2025

Powered by FUDForum. Page generated in 0.34245 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top