We help IT Professionals succeed at work.

getting printed out put to appear before user input

urobins
urobins asked
on
246 Views
Last Modified: 2007-12-19
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

print "Please enter your first name\n";
$first_name = <STDIN>;
print "Please enter your last name\n";
$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

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

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

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

print "Please enter your first name: ";
    my $first_name = <STDIN>;
    chomp ($first_name);
    $first_name = ucfirst($first_name);  # change first letter of first name to upper case
print "Please enter your last name: ";
    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";

Author

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

please enter your first name

// now the user enters their first name

please enter your last 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
rj2

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

$|++;

Author

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
rj2

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 your first name: joe
Please enter your last name: blow
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 your first name: joe
Please enter your last name: blow
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.
Join our community and discover your potential

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.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.