cs 1400 pick ups from chapters 2 and 3. #include directive this pre-processing directive causes the...
Post on 20-Dec-2015
213 views
TRANSCRIPT
#include directive
• This pre-processing directive causes the textual contents of a named file to be inserted into a program file at this point.
• Include files typically contain information necessary to use externally-defined utilities (such as I/O, math, etc.)
Character variables
• A variable of type char can hold one character;
char sex;
• A character constant is delimited by single quotes;
sex = ‘f’;
if (sex == ‘m’)
cout << “male”;
char vs. literals
• A literal is different from a character! char c;
cout << “hello world”;
c = “hello world”; // ERROR!
c = ‘h’; // CORRECT
c = “h”; // ERROR!
variable types (Visual C++)
type Size Rangebool 1 byte true or false
char 1 byte any single character
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
int 4 bytes -2,147,483,648 to 2,147,483,647
unsigned int 4 bytes 0 to 4,294,967,295
long 4 bytes same as int
unsigned long 4 bytes same as unsigned int
float 4 bytes +/- 3.4E-38 to +/- 3.4E38
double 8 bytes +/- 1.7E-308 to +/- 1.7E308
*FYI
Determining the size of variables
• The library function sizeof() can be used to determine the size of variables or types on other systems:
cout << sizeof (float);
*FYI
Constants
• C++ allows the programmer to control the type and base of a constantcout << 32; // defaults to int
cout << 32L; // becomes long
cout << 3.14; // defaults to double
cout << 3.14F; // becomes float
cout << 032; // becomes base-8 or 26
cout << 3.14e-3; // becomes .00314
*FYI
Named constants
• Constants may be given symbolic names to represent them in a program;– Form: const type name = value;– Example:
const float PI = 3.14159;
const int SIZE = 24;
– Alternative Form:#define name value#define PI 3.14159
#define SIZE 24
Defines should be place above main()
cin I/O rules
• Only digit characters (and sign) can be entered for an int variable.
• Only digit, decimal, and possibly E-notation characters (and sign) can be entered for a float variable.
• Only printable characters can be entered for a char variable.
Strings or char arrays
• A message or string of characters can be stored in a special array variable; char word[80]; // holds <80 characters
cin >> word;
cout << word << endl;
• Only printable characters can be input using cin (no blanks, tabs, etc.)
Initially limit use of string arrays…
• For now, only input and output will be done on arrays;
cin >> word; // OK
cout << word; // OK
word = “hello world”; // ERROR!
if (word < “goodbye”) // ERROR!
word1 = word2; // ERROR!
Combining assignments
• Multiple assignment statements may be combined– Example:
a = b = c = 25;
• Shorthand notation for combining arithmetic operations and assignment– Example:
a = a * 2; same as a *= 2;b = b – 5; same as b -= 5;c = c / 7; same as c /= 7;
Additional shorthand notation…
• incrementing a variablevalue++ same as value = value + 1
• decrementing a variablevalue-- same as value = value - 1
Formatting output
• Requires: #include <iomanip>• cout manipulators:
setw(n)• (set the minimum spaces for the next value output)
setprecision(n)• (set the precision or rounding for a float value)
fixed• (force output of float values in fixed-point notation)
showpoint• (show a decimal point with trailing zeros)
Controlling input
• Requires #include <iomanip>• cin manipulators
setw(n)• do not input more than n-1 characters for the next
variable
textbook error, pg 128
char word[5];
cin >> setw(5) >> word; // user enters “Eureka”
cout << word; // program outputs “Eure”
Special input examples…char sentence[20], ch;
cin.getline(sentence, 20);– reads all characters on a line (up to 19 in count) from
the keyboard and stores them in array sentencecin.get(ch);
– reads the next input character (including white spaces)
cin >> ch;– reads the next printable character (ignoring white
spaces)cin.ignore ();
– ignore (discard) the next input buffer character
More math library functions…More math library functions…
Requires: #include <cmath>functions examples
abs y = abs(x);
sqrt y = sqrt(a*a + b*b);
log y = log(x);
sin y = sin(x+z);
cos y = cos(x);
tan y = tan(x);etc…
File OutputRequires: #include <fstream>
ofstream fout;fout.open (“report.txt”);fout << “hello from a file!\n”;fout.close();
To write to report.txt on the a: drive;fout.open (“a:\\report.txt”);fout.open (“a:report.txt”);fout.open (“a:\report.txt”); // ERROR!
File Input
Requires: #include <fstream>
int a, b;
ifstream fin;
fin.open (“report.txt”);
fin >> a >> b;
cout << “sum is: “ << a+b << endl;
fin.close();
Example…
• Write a program to generate a monthly report of concert hall business for a touring show. Up to 20 concerts are given.– assume: reserved seat tickets $80
stadium seat tickets $50
standing tickets $35
production company gets 80%