Duke CPS 006 - Programs that Respond to Input

Unformatted text preview:

A Computer Science Tapestry3.1Programs that Respond to Inputz Programs in chapters one and two generate the same output each time they are executed.¾ Old MacDonald doesn’t get new animals without editing and recompiling the program• Drawbacks in editing and recompiling?¾ Allow the user to input values that generate output• Calculators respond to buttons pressed by users, programs respond to values entered by usersz Sequential model of programming: input, process, output¾ Interactive model of programming: entities communicate with each other continuously¾ We’ll start with IPO, input, process, outputA Computer Science Tapestry3.2C++ Review, Programming Processz C++ programs begin execution in main¾ Statements are executed (can you identify a statement?)¾ Sometimes expressions are evaluated:cout << "gpa = " << grades/totalCourses << endl;¾ Function calls execute a group of statements that embody an abstraction (e.g., Verse, EiEiO, …)z C++ programs must import needed declarations via #includedirectives (not statements, why not?)¾ Streams in <iostream>, used for ???¾ Strings in <string>, used for ???¾ Built-in types include int (integer), double (real number) and many operators like +, -, *, … are NOT importedA Computer Science Tapestry3.3C++ and Programming Reviewz Functions have prototypes (or signatures) that indicate to both the compiler and the programmer how to use the function¾ Later functions will return values, like square root¾ For now, void means no value is returned¾ Every function has a parameter list, but it’s possible to have no parametersHello(); Verse(“pig”,”oink”);• What do prototypes look like for these calls?z Function must appear before it’s called, either the function declaration (prototype only) or definition (implementation)A Computer Science Tapestry3.4Programming Reviewz You’ll design and implement C++ programs¾ Written in a high-level language, should run on many platforms, e.g., Windows, Unix, Mac, …¾ Compiler translates C++ into low-level machine language¾ Different compilers generate different low-level programs• Efficiency concerns, portability concerns, proprietary…z To execute, programs must link libraries --- implementations of what’s imported via #include directives¾ iostream library, string library, many more “standard”¾ Tapestry libraryz Errors can result if when programs use libraries incorrectly¾ Fail to include, fail to link, fail to use properlyA Computer Science Tapestry3.5Toward a User-controlled Barnyard#include <iostream>#include <string>using namespace std;void Verse(string animal, string noise){…cout << "on his farm he had a " << animal << endl;}int main(){Verse("pig","oink");Verse("elephant","hrruyaahungh");return 0;}z What can we do to allow user to enter animal and noise?A Computer Science Tapestry3.6Desired Program Behaviorz We want the user to enter/input valuesEnter animal name: sheepEnter noise: baahOld MacDonald had a farm, Ee-igh, Ee-igh, oh!And on his farm he had a sheep, Ee-igh, ee-igh, oh!With a baah baah hereAnd a baah baah thereHere a baah, there a baah, everywhere a baah baahOld MacDonald had a farm, Ee-igh, Ee-igh, oh!z We’ll pass the user-entered values to the Verse function¾ The input stream cin takes input from the keyboard using operator >>¾ Values that are input are stored in variables (aka objects)A Computer Science Tapestry3.7Input values are stored in variablesvoid Verse(string animal, string noise){ // this function doesn’t change}int main(){string animal; // variable for name of animalstring noise; // variable for noise it makescout << "enter animal ";cin >> animal;// what goes here??Verse(animal,noise);return 0;}z Each variable has a type, a name/identifier, and a valueA Computer Science Tapestry3.8Variables and Parametersz Both are placeholders for values. Each has a type and a name¾ Parameters are given values when arguments passed in a function call: void Verse(string animal, string noise){…}Verse("duck", "quack");¾ Variables are given values when initially defined, or as a result of executing a statementstring animal; // defined, no value suppliedcout << "enter animal ";cin >> animal; // user-entered value storedA Computer Science Tapestry3.9Define variables anywhere, but …z Two common conventions for where to define variables.¾ At the beginning of the function in which they’re used:{ string animal,noise;cout << "enter animal ";cin >> animal;cout << "enter noise a " << animal << " makes ";cin >> noise;}¾ Just before the first place they’re used:string animal;cout << "enter animal ";cin >> animal;string noise;cout << "enter noise a " << animal << " makes ";cin >> noise;A Computer Science Tapestry3.10Defensive programmingz When your program fails, you want to be able to find the cause quickly and without tearing your hair out¾ Give each variable a value when it is definedstring animal = “UNASSIGNED”;cout << "enter animal ";cin >> animal;//…¾ If, for some reason, the extraction >> fails, animal will have an identifiable value. ¾ What is the value if no initial assignment and extraction fails?z Read << as “puts-to” or “inserts”, read >> as “extract” ???A Computer Science Tapestry3.11Using numbers in a program#include <iostream>using namespace std;int main(){double degrees;cin << "enter temperature in degrees F. ";cin >> degrees;cout << degrees << " F = "<< (degrees-32) * 5 / 9 << endl;return 0;}z User can enter 80 or 80.5¾ There are two types for numbers, double and int, why?¾ Are parentheses needed in (degrees-32)? Why?A Computer Science Tapestry3.12Some arithmetic detailsz C++ adheres to traditional order of operations¾ * and / have higher precedence than + and –int x = 3 + 5 * 6; int y = (3 + 5) * 6;¾ Parentheses are free, use them liberallyz Arithmetic expressions are evaluated left-to-right in the absence of parenthesesint x = 3 * 4 / 6 * 2; int y = (3*4)/(6*2);z There are limits on int and double value, be aware of them.A Computer Science Tapestry3.13Variables and Parameters for Numbersz The type string is not a built-in type, technically it’s a class¾ What must you do to use strings in your programs?¾ What alternatives are there if strings not supported?z There are many numerical types in C++. We’ll use two¾ int, represents integers: {…-3,-2,-1,0,1,2,3,…}• Conceptually there are an infinite number of integers, but the range is limited to [-231, 231-1](on


View Full Document

Duke CPS 006 - Programs that Respond to Input

Download Programs that Respond to Input
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Programs that Respond to Input and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Programs that Respond to Input 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?