8 Replies - 1561 Views - Last Post: 15 March 2013 - 03:48 AM Rate Topic: -----

#1 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Reading user input

Posted 14 March 2013 - 08:36 AM

Hi I am new to PHP and am having a tough time finding a tutorial to show me how to read user input in the program. Basically I need the user to enter exchange rates and the dollar amounts they wish to convert but I can't figure it out. The first issue is getting user input for the danish krones exchange rate. Any help would be appreciated.
Here is the program:

# Variable Declarations

$us_dollars = 0;
$number_of_us_dollars = 3 	;
$us_dollar_total = 0.0;
$i = 1 ;


print("Welcome to the Money Exchange\n");
print("The program will convert three US dollar amounts to Danish, Krones, Euros and Hong Kong Dollars.\n");
print("First, please enter the exchange rates:\n");

do{
	  print("Danish Krones: ");	# Reads the user input for Danish Krones
      scanf
	  if ($danish_exchange <= 0)
      {
	    print("Invalid entry. Please enter a number greater than 0.");
      }
      }while($danish_exchange <= 0);

do{
	  $euros_exchange = float(raw_input("Euros: ")) ;# Reads the user input for Euros
	  if ($euros_exchange <= 0)
      {
	  print("Invalid entry. Please enter a number greater than 0.");
      }
    }while($euros_exchange <= 0);

do{
	  $hk_exchange = float(raw_input("Hong Kong Dollars: ")); # Reads the user input for Hong Kong Dollars
	  if ($hk_exchange <= 0)
      {
          print("Invalid entry. Please enter a number greater than 0.");
      }
    }while($hk_exchange <= 0);

# Begin for loop

for($i = 1; $i < $number_of_us_dollars; $i++){

    # Dollar amounts are entered by user */
    do{
	       $us_dollars = float(raw_input("\nEnter US dollars %i to be converted: " % (int(i))));
	       if ($us_dollars <= 0)
           {
               print("Invalid entry. Please enter a number greater than 0.");
           }
	      else
          {
              print "\n The amount of %.2f US dollars is equivalent to:\n" % (float($us_dollars));
          }
    }while($us_dollars <= 0);

     $danish_krones = $us_dollars * $danish_exchange ;	#US dollars are converted to other currencies */
     $euros = $us_dollars * $euros_exchange ;
     $hong_kong_dollars = $us_dollars * $hk_exchange ;

     print "	%.2f Danish Krones\n" % (float($danish_krones))	;		# Display dollar value in the other currencies */
     print "	%.2f Euros\n" % (float($euros));
     print "	%.2f Hong Kong Dollars" % (float($hong_kong_dollars));

     $us_dollar_total = $us_dollar_total + $us_dollars ;		# Accumulator for total US dollars converted */

    }


print "\nYou have converted a total of %.2f dollars.\n" % (float(us_dollar_total));


?>


Is This A Good Question/Topic? 1
  • +

Replies To: Reading user input

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3473
  • View blog
  • Posts: 11,788
  • Joined: 12-December 12

Re: Reading user input

Posted 14 March 2013 - 08:54 AM

PHP is a server-side language. It runs on the server and present a page to the users' browser. It cannot receive text-input - data can be passed to it, however, through the submission of an HTML form. There are no such functions as scanf or raw_input in PHP.

I suggest you find a tutorial and study the fundamentals of PHP, including an understanding of how a server-side language works. There is a Tutorials section here at DIC.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: Reading user input

Posted 14 March 2013 - 09:00 AM

ferguson32's education plan is to take programs in other languages, throw a couple of things in that approximate the language he's TRYING to program in, then come here and help-vampire his way through the rest of the assignment.

This is his Python assignment -- which started as a mish-mash of C and Python -- with a couple of PHPisms thrown in. Now he waits for the hand-holding over a multiple-page thread.

And PHP can in fact be run at the command line and receive user input.
Was This Post Helpful? 0
  • +
  • -

#4 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: Reading user input

Posted 14 March 2013 - 09:29 AM

If you're looking to read from the console's standard input, then I believe you want this page. It's actually quite easy - you just need to know the magic file name/handle to use.
Was This Post Helpful? 0
  • +
  • -

#5 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Re: Reading user input

Posted 14 March 2013 - 04:47 PM

Yea buddy I'm taking a class that covers Perl, Python, and PHP and I have to take the same program and use it in each language; that is why it is the same as my Python program, so relax before you go thinking your such a genius for realizing it is the same program from Python. The whole thing is in PHP except reading the user input by the way. But thanks for keeping tabs on me, insulting me, and offering no help.
Was This Post Helpful? 0
  • +
  • -

#6 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

Re: Reading user input

Posted 14 March 2013 - 09:56 PM

that's a pretty whack class, never heard of learning three languages at once. An I'm pretty sure your professor wanted you to make the same program in all three languages - not one script that'll work for all three (because that's pretty impossible).

In your code, syntax aside, there's no scanf function I'm aware of on line 15.
Was This Post Helpful? 0
  • +
  • -

#7 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Re: Reading user input

Posted 15 March 2013 - 03:42 AM

I know I'm trying to figure out how to read user input with PHP. I can't find a tutorial that shows what used used for PHP that does what scanf does for C.
Was This Post Helpful? 1
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: Reading user input

Posted 15 March 2013 - 03:43 AM

View Postferguson32, on 14 March 2013 - 07:47 PM, said:

Yea buddy I'm taking a class that covers Perl, Python, and PHP and I have to take the same program and use it in each language; that is why it is the same as my Python program, so relax before you go thinking your such a genius for realizing it is the same program from Python. The whole thing is in PHP except reading the user input by the way. But thanks for keeping tabs on me, insulting me, and offering no help.


You don't get it. I understand what your class/assignments are doing; I'm criticizing how you're going about it, which is
1. Copy/paste old assignment in different language
2. Add a couple of things from the new language to it.
3. Come here and create a marathon thread in which you expect someone to hand-hold you through removing all the old language syntax and converting it to the new language syntax.

That's not learning, that's spoon-feeding. And it's a poor way to accomplish your task.

You SHOULD have the logic down quite well at this point for this program. You should start with a fresh document in your editor with NO CODE from your previous assignment. Take what you've learned in the class and try to implement the assignment.
Was This Post Helpful? 0
  • +
  • -

#9 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Re: Reading user input

Posted 15 March 2013 - 03:48 AM

And I know I'm not using the same script for all three just the same program. This isn't how I wrote it in Perl or Python.

I looked at PHP tutorials, what is wrong with it that is not PHP compatible besides reading user input which was my question in the first place?? I'm not asking for someone to write my program, just show me how to do it or let me know if there is a tutorial you know of that could help me. That's all I said...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1