We help IT Professionals succeed at work.

# getting printed out put to appear before user input

on
246 Views
Im working on a homework assignment and Ihave to prompt the user for input, but I can't figure out how to get the output to appear before the user input... my code is below any help/advice would greatly be apprciated

#!/usr.bin/perl -w

\$first_name = <STDIN>;
\$last_name=<STDIN>;
print "Please enter a temperature in fahrenheit\n";
\$f_temp=<STDIN>;
\$c_temp = ((\$f_temp - 32) * 5 )/9;
print "Thank You \$first_name \$last_name, \$f_temp degrees Fahrenheit is \$c_temp degrees Celsius\n";
Comment
Watch Question

## View Solutions Only

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)

Commented:
Somehow, that last line gt repeated above.  Corrected:

#!/usr/bin/perl -w
use strict;

my \$first_name = <STDIN>;
chomp (\$first_name);
\$first_name = ucfirst(\$first_name);  # change first letter of first name to upper case
my \$last_name=<STDIN>;
chomp (\$last_name);
\$last_name = ucfirst(\$last_name);   # change first letter of last name to upper case
print "Please enter a temperature in Fahrenheit: ";
my \$f_temp=<STDIN>;
chomp (\$f_temp);
my \$c_temp = sprintf ("%.2f", ((\$f_temp - 32) * 5 )/9);   # show only 2 decimal places in converted value
print "Thank you \$first_name \$last_name, \$f_temp degrees Fahrenheit is \$c_temp degrees Celsius\n";

Commented:
Sorry to confuse, I am trying to have this out put

// now the user enters their first name

// now the user enters their last name

please enter a temp in fahreneheit

// now the user enters a temperature

then teh name and celsius temp are outputted

my problem is it is looking for input before I ask for the names and temp

Commented:
Does it help to add the line below right after "use strict;" to flush output buffer?

\$|++;

Commented:
I found it is the difference between compiling in windows and XP.  when I runthe program in XP it asks for all input before any output, but under UNIX it behaves properly.  I am going to split th points between you both for helping my out, it was just the environment, I didn't know that :)  thanks

Commented:
When I run your program on XP the lead text is shown before I can enter my name.
What version of Perl are you using? Try Activestate if you are using a different version.
http://www.activestate.com/activeperl

Commented:
I'm still confused (as usual).  You say "in XP it asks for all input before any output, but under UNIX it behaves properly".  Isn't asking for input before calculating output not only proper, but required?

I find no difference in the script if run under Windows or Linux:

Output using ActiveState ActivePerl 5.8.7 Build 813 on MS XP Professional w/ SP2:

Please enter a temperature in Fahrenheit: 45
Thank you Joe Blow, 45 degrees Fahrenheit is 7.22 degrees Celsius

Output using Perl 5.8.5 on Fedora Core 4:

Please enter a temperature in Fahrenheit: 45
Thank you Joe Blow, 45 degrees Fahrenheit is 7.22 degrees Celsius

Thanks for the points, but I still don't understand the question...:)
Unlock the solution to this question.

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.