1 of 29

Windows

Variable Conversion

Loops in Errortrapping

Windows

2 of 29

Lesson Goal

  • Create a program that displays a multiple of 4
  • The user will enter the multiple
    • If input isn’t valid an error window will appear
    • We’ll ask until the input is valid

Don’t worry about making this now, we’ll build towards this for the end of the lesson.

3 of 29

Variable Types Review

  • Every variable has a type
  • This limits the values it can have
    • Ints only hold whole numbers
    • You can’t put text into an int or a real
    • Chars only hold 1 character at a time
    • Booleans can only be true or false

4 of 29

Type Incompatibility

  • Trying to store values out of a variable’s type in a variable will crash
  • Trying to add number types to text types will crash
  • Ints and reals can be added safely, but you can’t put a real in an int
  • Chars and strings can be added safely

5 of 29

Variable Types Summary

Type

Values

Examples

int

Integers, whole numbers

5, -2000, 127

real

Numbers with decimals

7.1, 83.0, -157.6

boolean

True or False

true, false

string

Text (a string of characters)

“Apple”, “i!^”, “”, “0.9”

char

A single character

‘A’, ‘=’, ‘3’, ‘p’

6 of 29

Variable Conversion

Command

Effect

Usage

strreal(real)

Converts string to real

Taking input

strint(int)

Converts string to int

Taking input

realstr(real, n)

Converts real to string, with n reserved spaces

Text formatting

intstr(int)

Converts int to string

Text formatting

7 of 29

Example

8 of 29

Safe Conversion

  • Some strings can’t be converted to reals and ints
    • Eg. strint(“ABCD”) and strreal(“100.0..0”)
  • strrealok() and strintok() return whether the string can be converted

9 of 29

Error-Trapping Input

  • Protect your code from bad inputs
    • Take all input as strings
    • Check if the strings can be converted
  • This “traps” any errors, your code will never crash on the user

10 of 29

Error-Trapping Example

11 of 29

Pauses and Delays

Two methods are pausing a program

  1. getchar

var ch : char

ch := getchar

  • delay(milliseconds)

delay(1000)

12 of 29

Loops

  • Will run until exit command is called
    • You can call exit inside a conditional structure
    • You can also call exit when condition
  • If exit is never reached, it loops on forever
  • Use when you don’t know how many repetitions are needed

13 of 29

Loop Example

14 of 29

Common Uses for Loops

  • Animations that loop over (eg. GIFs)
  • Asking for input until an expected answer is given
  • Error-trapping
    • Loop until input can be safely converted
    • Loop until input is in expected range

15 of 29

Practice

Ask the user for an integer, and output its value doubled. If the input isn’t an int, ask for input again until it’s valid.

16 of 29

Solution

17 of 29

Windows

  • Output and input happens in Turing’s default window
    • Instantly made when you use put, draw, or get
  • You can create windows from within your program
  • Input and output only happens to 1 window at a time
    • This is called the current window

18 of 29

Creating Windows

  • This creates a new window
    • Specifies position on screen with x and y
    • Specifies width and height with w and h
      • You can put max;max for fullscreen
  • The position and dimensions are in pixels
  • You can omit position or graphics

19 of 29

Practice

Create a window with:

  1. Position (200, 250) and dimensions 100x250
  2. Position (50, 50) and dimensions 800x500
  3. Position (0, 0) and full-screen dimensions

20 of 29

Solution

  • There are more ways you can customize windows
  • “title:name”, “text:rows;columns”, “nocursor”
  • For more options:

21 of 29

Window Commands

Command

Purpose

Window.Show(winID)

Shows a window, user can’t with it

Window.Hide(winID)

Hides a window, user can’t interact with it

Window.Close(winID)

Closes a window, program can’t interact with it anymore

Window.Select(winID)

Selects the current window

22 of 29

Program States

  • Your program is made of multiple “states”
    • Each window can be its own state
  • Each state has a purpose (intro, input, output, etc.)
  • Events cause the program to change states
  • When planning, know what states you’ll have
    • Know what events cause states to change

23 of 29

Switching States

There are 2 ways to handle windows:

  1. Create all windows at the beginning
    1. Hide and show windows with each event
    2. You can still use offscreen windows

  • Each event, create the window you need
    • Close the window after the event
    • Easier to switch states

24 of 29

States Example

STATE: SLEEPING

STATE: EATING

EVENT: GIVEN FOOD

EVENT: DONE EATING

25 of 29

Windows Example

(Window 1)

I’M SLEEPY!

(Window 2)

I’M HUNGRY!

  1. User enters input in window 1
  2. User enters input in window 2

1

2

26 of 29

Code Example

Create windows

Event 1 hides win1, shows and selects win2

Event 2 hides win1, shows and selects win2

Events are triggered by user input in a loop

27 of 29

Main Challenge

  • Create a program that displays a multiple of 4
  • The user will enter the multiple
    • If input isn’t valid an error window will appear
    • They can press a key to enter another input
  • We’ll ask until the input is valid

28 of 29

Main Challenge Plan

(Input)

ENTER

AN INTEGER

(Output)

THE INTEGER

MULTIPLIED BY 4 IS...

(Error-Trap)

YOUR INPUT

WAS INVALID

  • User enters valid input
  • User enters invalid input
  • User presses key

2

3

1

29 of 29

Solution