Post on 02-Jan-2016
transcript
LECTURE 5:INPUT & OUTPUT FUNCTIONS
CSC 107 – Programming For Science
Announcements
Memorization is not important, but… … you will all still be responsible for
information Instead use your resources: notes, books,
slides, etc. Figured out problem using Eclipse in
labs Easy to work around problem via several
options Bringing thumbdrive easiest, but is not
required During the day, tutors available in WTC
206/208 Weekly assignment #2 posted to Angel
Variables, Constants, & More
General Cases Examples
Variable DeclarationdataType name;dataType name = value;dataType name(value);dataType name, anotherName;dataType name = value, anotherName;
int count;bool monkey = true;float avg(10.0);char help,letter;char a=‘a’,letter;
Constant Declarationconst dataType name = value; const double PI=3.1;
Symbolic Constant #define NAME value #define AGE 34
Data Types
Each variable also has data type How program treats variable’s value
defined by this Single true or false value held by bool C/C++ defines 7 numeric data types
Integer types: short, int, long, long long Decimal types: float, double, long double
char data type can hold a character
Data Types
Each numeric data type can be one of 2 types Signed (both positive & negative) version is
default If data must be non-negative use unsigned _____ Upper range of variable is doubled
Warned when using signed & unsigned together
Only certain assignments allowed in system Can assign an integer to a decimal variable Error assigning decimal to integer variable,
since its hard
Program Outline
Once upon a time… … some stuff happens… … and they all lived happily ever after
Program Outline
Once upon a time… All programs must begin somewhere Defines what is worked upon during rest of
program For non-trivial programs, requires receiving
input
When starting program, first steps always same:1. What is the input?2. What will the input look like?3. How will the data be entered?
Reading From The Keyboard
Easiest to get input from the keyboard Reading from files possible; discussed later
in term C++ lacks standard, so writing GUI much
harder
C++ defines cin to get user’s input As easy to use as delivering food to
Granny’s house When cin hit, program waits until it has
input User must press enter for line to be able to
be read Editing not seen by program; only receives
final line
Programming Using cin
Used to read one or more values at once:
cin >> variable;cin >> variable1 >> variable2;
Reads where last cin stopped reading input Automatically skips past whitespace
Data type of variable determines what is read Stops reading at first non-usable value in
input If input is not usable, will set variable equal
to 0
cin Example
>>
>>
>>
cin Example
>>
>>
>>
cin Example
>>
>>
>>
Program Outline
Once upon a time… Get the input using cin from the keyboard
… some stuff happens… … and they all lived happily ever after
Some Stuff Happens
This really depends on specific project details Focus of most of term, we will skip this
today
Program Outline
… and they all lived happily ever after All good processing comes to end & report
results Shows program worked and provides
feedback Results takes many forms, focus on printing
today
When starting program, second steps ask:1. What must be output?2. How can output be presented best?3. Will it be pretty?
Using cout to Print
Already seen how to print text using cout
cout << “Hello World” << endl; Prints out whatever is placed between
quotes endl goes to next line and prints out
immediately
Use escape sequences for fancier text output\n newline (move to start of next line)\t tab (go to next column that is multiple of 8)\\ \ (backslash character)\” “ (quotation mark)
Can Print Out Multiple Items cout can also print out value of
variablesint bob = 2;double j = 4.5;char var = ‘a’;cout << “Hello ”;cout << bob << endl;cout << j << endl;cout << var << endl;cout << j << “ is not ” << bob << endl;cout << bob << “ equals bob” << endl;cout << var << bob << j << endl;
But Can It Be Used?
cout built to provide basics needed to work Prints out as many digits as needed No extra spaces or tabs used Scientific notation cannot be used
Often want to format results Significant digits can matter Tables make reading faster
Real World Strikes Again
Troll Princess
First Way To Format Output
#include <iostream>using namespace std;
int main() { int k = 4; cout << k << endl; cout.width(4); cout << k << endl; cout << k << endl; cout.setf(ios::showpos); cout << k << endl; cout.width(3); cout.setf(ios::left); cout << k << k << endl;}
Second Way To Format Output
#include <iostream>#include <iomanip>using namespace std;
int main() { int k = 4; cout << k << endl; cout << setw(4) << k << endl; cout << k << endl; cout.setf(ios::showpos); cout << k << endl; cout.setf(ios::left); cout << setw(3) << k << k << endl;}
Your Turn
Get in groups of 3 & work on following activity
For Next Lecture
Read sections 6.1 – 6.7 for Friday How can we use the variables? What operations exist for us to use? What do we mean by order of operations?
Week #2 weekly assignment due Tuesday Problems available on Angel If problem takes more than 10 minutes,
TALK TO ME!