Download presentation
Presentation is loading. Please wait.
1
CS 1400 15 Sept 2006 Pick ups from chapters 2 and 3
2
Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example: int sum = 0; float interest_rate = 0.56;
3
Boolean constants Example: bool flag; flag = true; flag = false; What would this do? while (true) {cout << “hello! “; }
4
Type conversions Rankings: –long double(highest) –double –float –unsigned long –long –unsigned int –int(lowest)
5
Rules Rule #1 –char, short, unsigned short are all considered as int Rule #2 –When a mixed-type arithmetic operation is encountered, the lower ranked value is promoted to the higher ranking value. Rule #3 –When a value is assigned to a variable, it is converted to that variable type.
6
Overflow / Underflow Assigning a too large or too small value to a variable results in incorrect values. An error message may not be generated!
7
Manual type conversions… Type casting allows you to manually force a value to be converted to a type –Form: static_cast (value ) –Example: int sum, count; float average; cin >> sum >> count; average = static_cast (sum) / count; cout >> “average is: “ << average << endl;
8
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()
9
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 asa *= 2; b = b – 5;same asb -= 5; c = c / 7;same asc /= 7;
10
Formatting output Requires: #include 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)
11
Controlling input Requires #include 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”
12
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 sentence cin.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
13
More math library functions… Requires: #include functionsexamples absy = abs(x); sqrty = sqrt(a*a + b*b); logy = log(x); siny = sin(x+z); cosy = cos(x); tany = tan(x); etc…
14
File Output Requires:#include 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”);
15
File Input Requires:#include int a, b; ofstream fin; fin.open (“report.txt”); fin >> a >> b; cout << “sum is: “ << a+b << endl; fin.close();
16
Example… Write a program to generate a report of concert hall business for a touring show. –assume:reserved seat tickets$80 stadium seat tickets$50 standing tickets$35 production company gets 80%
17
Input… line 1:tour name line 2:number of shows line 3 (first show)reserved stadium standing line 4 (second show)reserved stadium standing etc…
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.