Presentation is loading. Please wait.

Presentation is loading. Please wait.

User Input Keyboard input.

Similar presentations


Presentation on theme: "User Input Keyboard input."— Presentation transcript:

1 User Input Keyboard input

2 What is Input? Data sent into the computer for processing, storage and output Many possible formats Sound (microphone) Graphical (image from a camera) Text (from the keyboard) Positional (from a mouse) Many sources Internet, keyboard, mouse, music file, picture file...

3 Keyboard Input User types in something on the keyboard
The program uses that input to perform some computations (also called processing or calculations) The processed data is eventually output to the screen

4 Does keyboard input have to be characters?
It can be... But each character can represent something else Example: Where have you seen this? w – forward s – back a – left d - right

5 How Does Turing Do Keyboard Input
Turing can do keyboard input many ways For now we will look at one method Keyboard (Today) Mouse

6 Input Whenever we want information from the user, that information is called input Before we can expect the user to enter information we must ask them for it, we call this “prompting” the user. This may come in the form of a put command or some type of indicating screen image, like an arrow

7 Input To use the information we need to store the information we receive from the user, we do this by putting it in a variable Now it’s simply a matter of telling Turing to wait for input from the user and store it in the variable. We have two commands to do this get and getch

8 get The get command when executed simply waits on that line of code until the user enters data and presses the Enter key. It will then store that data into an indicated variable Syntax: get <identifier> E.g. var userName : string get userName

9 Teacher-Led Example: Write a user-friendly program that reads in the
User’s first name User’s last name User’s age User’s Xbox Gamer Score Output the username as their (first name + their last) name (e.g. Bob Joe will produce BobJoe) Output their rating as the Gamer Score divided by their age (age 24, Gamer Score will produce )

10 Input BE CAREFUL WHEN WORKING WITH USER INPUT:
What if the program asks for an age and the user enters “Boomerang” But the variable for age is an int, not a string...what happens? ERROR Ensure the data entered by a user is the data type you expect to work with. Right now all we can do is trust them. Soon we will learn how to force them to enter proper data.

11 Dealing with Spaces Sometimes we may ask the user for more than single word input. E.g. We may ask them to enter their full name The problem is that the get command will only read up until it sees the first space E.g. If the user types in Bart Simpson, the only thing read in is Bart We an get around this by simply adding a bit of extra code to the end of our get statement informing Turing to read in everything until the end of the line That extra “bit” is simply : * E.g var fullName : string put “Please enter your full name” get fullName : *

12 getch( ) getch( ) is short for get character.
The difference between getch( ) and get is that getch will only retrieve one character at a time and you do not have to hit the Enter key after. Where would this be useful? Games (w,a,s,d)...you do not have to hit Enter

13 getch ( ) There is one catch to using a getch( ) command:
The variable used to store the input MUST be a string that can be no longer than 1 character We can specify how long a string can be when we declare it E.g. var key : string (1)

14 getch ( ) Special Note: When typing data for a get command the text you type shows on the screen, this is called echo. However when using a getch( ), there is no echo

15 getch( ) Syntax: var <identifier> : string (1) getch(identifier)
E.g. var key : string (1) getch(key) We will have more detailed practice on this soon


Download ppt "User Input Keyboard input."

Similar presentations


Ads by Google