Presentation is loading. Please wait.

Presentation is loading. Please wait.

Input Based Programs.

Similar presentations


Presentation on theme: "Input Based Programs."— Presentation transcript:

1 Input Based Programs

2 Generic Algorithm Algorithm for every program ever written:

3 Generic Algorithm Algorithm for every program ever written:
Get some input Do some stuff to it Output a value

4 Getting Input cin : console input
"Read from the console into variable1" >> : stream extraction operator

5 Prompt Lines Prompt for input with cout before doing cin:
cout << "Please enter a number between 1 and 10 and " << "press the return key" << endl; int num; cin >> num;

6 Bad Input Bad input = silent failure!!! Variable maintains previous value int x; cin >> x; Input Result 12 X is set to 12 Q X is ??? program continues .5 12.5 X is set to 12 ".5" is saved for next needed input

7 Bad Input Bad input = silent failure!!! No more input accepted int x, y; cin >> x; cin >> y; Input Result 12 2 X is set to 12, y to 2 12 2 12 Q X is 12 y is ???? Q 12 X and Y are ????

8 Bad Input Bad input = silent failure!!! No more input accepted int x, y; cin >> x; cin >> y; Input Result 12 Q X is 12 y is ???? Q 12 X and Y are ????

9 Don't worry about bad input
For now… Don't worry about bad input Don't attempt to "catch" bad input on assignments No "try again" input loops, no "are you sure", no "run again?", etc… DO make sure you accept the input a problem specifies

10 Software Development Lifecycle
The grand process:

11 Software Development Lifecycle
Small scale Understand problem Decide on inputs and outputs How many, what types, what order Start program Read in inputs into variables Do math on variables, store results into new variables Output variables that contain answers

12 Software Development Lifecycle
Testing: Test early and often Read in values – confirm you got them correctly Partway through calculations Work in small steps NO: double answer = (-b + pow( pow(b,2) – 4 * a * c), 0.5) / (2.0 * a); YES: double bSquared = pow (b, 2); double discriminant = bSquared – 4 * a * c double twoA = 2.0 * a; double answer = (-b + pow(discriminant, 0.5)) / twoA;

13 Sample 1 Given the lengths of the two legs of a triangle, print the length of the hypotenuse. Sample run: Input side A: 3 Input side B: 4 Hypotenuse is 5.0

14 Sample 1 Given the lengths of the two legs of a triangle, print the length of the hypotenuse. Input: side lengths a and b Output: hypotenuse

15 Sample 2 Ask the ctime library for number of seconds since Midnight, Jan 1st Print the time of day like hour:minute:second.

16 Sample 2 Ask the ctime library for number of seconds since Midnight, Jan 1st Print the time of day Input: None from user. (Get time from library) Output: hours, minutes, seconds


Download ppt "Input Based Programs."

Similar presentations


Ads by Google