Jump to content

Newbie question. Getting the user to pick an option through a keboard key is being ignored


Valda

Recommended Posts

Howdy folks! Please go easy on me, I'm relatively new to programming and still climbing through the basics.

My code is pretty simple. User gotta pick "d" or "w" to deposit or withdraw his fake virtual money, then he picks the amount and the awesomely cool calculation transpires :P

But it doesn't work. No matter what is being picked the keyboard always goes to the "else" condition (which tells the user "You can only use enter 'w' or 'd'" -- despite the fact I input "d" or "w"). Basically it completely skips my "if" and "else if" statements.

 

I tried putting "d" in quotations, without, with tags, with what not! 

 

So, any clue what is being done wrong here?

 

<?php

$balance=1000;

echo "Welcome to your bank. Your balance is " . $balance . " Which action would you like to take? (w=withdraw, d=deposit) \n" ;

$user=fgets(STDIN);

 

if ($user=="w")

{

echo "How much would you like to withdraw? \n";

(int)$sum=fgets(STDIN);

$balance = $balance-$sum;

echo "Money successfully withdrawn. Your balance now stands at" . $balance . "";

}

 

else if ($user=="d")

{

echo "How much would you like to deposit? \n";

$sum=fgets(STDIN);

$balance = $balance+$sum;

echo "Money successfully deposited. Your balance now stands at" . $balance . "" ; 

}

else

{

echo "You can only use enter 'w' or 'd'";

    }

?>

Link to comment
Share on other sites

According to a google search you might try this:

 

$stdin = fopen('php://stdin', 'r');

$response = fgets($stdin);

 

PS - it is NOT completely skipping your if/else statements.  It is logically proceeding through them and coming to a completely anticipated conclusion.

Edited by ginerjm
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.