overview of c++
DESCRIPTION
Overview of C++. By Dr. Awad Khalil Computer Science Department. C++ Language Elements. Comments make a program easier to understand // Used to signify a comment on a single line /* Text text */ use if comments on multi lines Don’t embed comments within /* */ comments. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/1.jpg)
Overview of C++
By By
Dr. Awad KhalilDr. Awad Khalil
Computer Science DepartmentComputer Science Department
![Page 2: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/2.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
2
C++ Language Elements Comments make a program easier to Comments make a program easier to
understandunderstand // // Used to signify a comment on a single Used to signify a comment on a single
lineline /* Text text *//* Text text */ use if commentsuse if comments on multion multi
lineslines Don’t embed comments within Don’t embed comments within /* *//* */
commentscomments
![Page 3: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/3.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
3
Compiler Directives #include#include
Compiler directiveCompiler directive Processed at compilation timeProcessed at compilation time Instructs compiler on what you want in the Instructs compiler on what you want in the
programprogram #include <iostream>#include <iostream>
Adds library files to programAdds library files to program Used with Used with < >< > Also Also “ ““ “ user defined user defined
![Page 4: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/4.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
4
Compiler Directives
Stream data typeStream data type Object that is a stream of charactersObject that is a stream of characters Defined inDefined in iostreamiostream Entered on the keyboardEntered on the keyboard (cin)(cin) Displayed on monitorDisplayed on monitor (cout)(cout)
![Page 5: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/5.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
5
Declarations
Direct compiler on requirementsDirect compiler on requirements Based on data needs (data identifiers)Based on data needs (data identifiers) Each identifier needed must be declaredEach identifier needed must be declared Comma used to separate identifiersComma used to separate identifiers cin and cout are undeclared identifierscin and cout are undeclared identifiers
Special elements called streamsSpecial elements called streams cin - input stream , cout - output streamcin - input stream , cout - output stream Included with the iostream not declaredIncluded with the iostream not declared
![Page 6: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/6.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
6
Executable Statements
cout get outputcout get outputcout << “Enter the fabric size in square meters: ”;
cin get inputcin get inputcin >> sizeInSqmeters;
AssignmentAssignmentsizeInSqyards = metersToYards * izeInSqmeters;
![Page 7: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/7.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
7
Reserved Words and Identifiers Reserved words have special meaningsReserved words have special meanings
Can NOT be used for other purposes (const, Can NOT be used for other purposes (const, float and void are some examples)float and void are some examples)
Identifiers (variables)Identifiers (variables) Used to store data by the program (user Used to store data by the program (user
defined)defined) Valid identifiers - letter, letter1, _letterValid identifiers - letter, letter1, _letter Invalid identifiers - 1letter, const, hell oInvalid identifiers - 1letter, const, hell o
Special symbolsSpecial symbols C++ has rules for special symbolsC++ has rules for special symbols= * ; { } ( ) // << >>= * ; { } ( ) // << >>
![Page 8: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/8.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
8
Upper and Lower Case
C++ case sensitiveC++ case sensitive Compiler differentiates upper & lower Compiler differentiates upper & lower
casecase Identifiers can be either Identifiers can be either Be careful though (Be careful though (cost != Cost))
Blank spacesBlank spaces Use space to make program readableUse space to make program readable Use care in placing spacesUse care in placing spaces
![Page 9: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/9.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
9
User-Defined Identifier An Identifier must always begin with a letter or An Identifier must always begin with a letter or
underscore symbol (not recommended)underscore symbol (not recommended)
An identifier must consist of letters, digits, or An identifier must consist of letters, digits, or underscore only.underscore only.
You cannot use a C++ reserved word as an You cannot use a C++ reserved word as an identifier.identifier.
![Page 10: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/10.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
10
User-Defined Identifier
Invalid Identifiers1Letter1Letter
FloatFloat
ConstConst
Two*FourTwo*Four
Joe’sJoe’s
Two-dimensionalTwo-dimensional
![Page 11: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/11.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
11
Data Types and Declarations Predefined data typesPredefined data types
intint (integers)(integers) Positive or negative whole numbersPositive or negative whole numbers 1000 1000 1212 199199 100000100000 INT_MAXINT_MAX - largest int allowed by compiler- largest int allowed by compiler
floatfloat (real numbers)(real numbers) Positive or negative decimal numbersPositive or negative decimal numbers 10.510.5 1.21.2 100.02100.02 99.8899.88
boolbool (boolean)(boolean) truetrue falsefalse
charchar (Characters)(Characters) Represent charactersRepresent characters
![Page 12: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/12.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
12
Data Type: int The basic integer type is The basic integer type is intint
The size of an int depends on the machine and The size of an int depends on the machine and the compilerthe compiler
On pc’s it is normally 16 or 32 bitsOn pc’s it is normally 16 or 32 bits Other integers typesOther integers types
short: typically uses less bitsshort: typically uses less bits long: typically uses more bitslong: typically uses more bits
Different types allow programmers to use Different types allow programmers to use resources more efficientlyresources more efficiently
Standard arithmetic and relational operations are Standard arithmetic and relational operations are available for these typesavailable for these types
![Page 13: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/13.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
13
Data Type: float Floating-point types represent real numbersFloating-point types represent real numbers
Integer partInteger part Fractional partFractional part
The numberThe number 108.1517108.1517 breaks down into the following breaks down into the following partsparts 108 - integer part108 - integer part 1517 - fractional part1517 - fractional part
C++ provides three floating-point typesC++ provides three floating-point types floatfloat doubledouble long doublelong double
![Page 14: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/14.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
14
Data Type: char
charchar (characters)(characters)Individual character value (letter or Individual character value (letter or
number)number)Character literal enclosed in single Character literal enclosed in single
quotes ‘A’quotes ‘A’ Character type Character type charchar is is related to the related to the
integer typesinteger types Characters are encoded using a scheme Characters are encoded using a scheme
where an integer represents a particular where an integer represents a particular charactercharacter
![Page 15: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/15.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
15
Character Encoding Schemes
ASCII is the dominant encoding schemeASCII is the dominant encoding scheme ExamplesExamples
' ' encoded as 32' ' encoded as 32 '+' encoded as 43'+' encoded as 43 'A' encoded as 65'A' encoded as 65 'Z' encoded as 90'Z' encoded as 90 ’ ’a' encoded as 97 a' encoded as 97 ’ ’z' encoded as 122z' encoded as 122
![Page 16: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/16.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
16
Character Encoding Schemes
UNICODEUNICODE
![Page 17: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/17.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
17
string Class
String object data typeString object data type A literal string constant is a sequence of A literal string constant is a sequence of
zero or more characters enclosed in zero or more characters enclosed in double quotesdouble quotes
"Are you aware?\n""Are you aware?\n" Individual characters of string are stored Individual characters of string are stored
in consecutive memory locationsin consecutive memory locations The null character (The null character ('\0''\0') is appended to ) is appended to
strings so that the compiler knows where strings so that the compiler knows where in memory strings endsin memory strings ends
![Page 18: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/18.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
18
string Class
String literalString literal ““A”A” ““1234”1234” ““Enter the distance”Enter the distance”
Additional data types included in libraryAdditional data types included in library
#include <string>#include <string> Various operations on stringsVarious operations on strings
![Page 19: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/19.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
19
Declarations
Identifiers should be Identifiers should be Short enough to be Short enough to be
reasonable to type (single reasonable to type (single word is norm)word is norm)
Standard abbreviations Standard abbreviations are fine (but only are fine (but only standard abbreviations)standard abbreviations)
Long enough to be Long enough to be understandableunderstandable
When using multiple When using multiple word identifiers word identifiers capitalize the first letter capitalize the first letter of each wordof each word
ExamplesExamples char response;char response; int minelement;int minelement; float score;float score; float float
temperature;temperature; int i;int i; int n;int n; char c;char c; float x;float x;
![Page 20: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/20.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
20
Constant Declarations
Types of constantsTypes of constants integer integer floatfloat charchar boolbool string objectsstring objects
Associate meaningful termsAssociate meaningful terms constconst floatfloat PAYRATE = 10.25;PAYRATE = 10.25;
![Page 21: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/21.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
21
Hello.cpp// FILE: Hello.cpp// FILE: Hello.cpp// DISPLAYS A USER'S NAME// DISPLAYS A USER'S NAME#include <iostream>#include <iostream>#include <string>#include <string>
using namespace std;using namespace std;
int main ()int main (){{ char letter1, letter2; char letter1, letter2; string lastName;string lastName;
// Enter letters and print message.// Enter letters and print message. cout << "Enter 2 initials and last name: ";cout << "Enter 2 initials and last name: "; cin >> letter1 >> letter2 >> lastName;cin >> letter1 >> letter2 >> lastName; cout << "Hello " << letter1 << ". " << cout << "Hello " << letter1 << ". " <<
letter2 << ". " << lastName << "! ";letter2 << ". " << lastName << "! "; cout << "We hope you enjoy studying C++." << endl;cout << "We hope you enjoy studying C++." << endl; return 0;return 0;}}
![Page 22: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/22.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
22
Executable Statements Memory statusMemory status
Before and afterBefore and after AssignmentsAssignments
Form: result = expression;Form: result = expression;
sizeInSqyards = metersToYards * sizeInMeters;sizeInSqyards = metersToYards * sizeInMeters;
sum = sum + item;sum = sum + item;
![Page 23: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/23.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
23
Arithmetic Operators
++ AdditionAddition - - SubtractionSubtraction ** MultiplicationMultiplication // DivisionDivision % % ModulusModulus
![Page 24: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/24.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
24
Input / Output Operations
InputInput
#include <iostream> #include <iostream> librarylibrary
cin >> sizeInSqmeters;cin >> sizeInSqmeters;
OutputOutput
#include <iostream> #include <iostream> librarylibrary
cout << squareArea;cout << squareArea;
![Page 25: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/25.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
25
Program Input: cinForm: Form: cin >> dataVariable;cin >> dataVariable;cin >> age >> firstInitial;cin >> age >> firstInitial;
Extracted from cin (input stream)Extracted from cin (input stream) >> Directs input to variable>> Directs input to variable cin associated with keyboard input (stdin)cin associated with keyboard input (stdin) Used with int, float, char, bool and stringsUsed with int, float, char, bool and strings Leading blanks ignored (floats, int, char,bool and strings)Leading blanks ignored (floats, int, char,bool and strings) Char read 1 at a time (1 non blank)Char read 1 at a time (1 non blank) Case issuesCase issues int or float will read until spaceint or float will read until space Stings same as int and floatStings same as int and float
![Page 26: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/26.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
26
Program Output: cout
Form:Form: cout << dataVariable;cout << dataVariable;
cout << squareArea;cout << squareArea;
Output stream coutOutput stream cout << Output operator (insertion operator)<< Output operator (insertion operator)
cout << “my height in inches is: “ << height;cout << “my height in inches is: “ << height; Blank linesBlank lines
endl; or “\n”;endl; or “\n”;
![Page 27: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/27.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
27
General Form of a C++ Program
General program formGeneral program form Function basic unit (collection of related Function basic unit (collection of related
statements)statements) A C++ program must contain a main functionA C++ program must contain a main function
void main ()void main () int - function returns integer valueint - function returns integer value main - lower case with ()main - lower case with () { } - Braces define the function body{ } - Braces define the function body
![Page 28: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/28.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
28
General Form of a C++ Program General form of function body partsGeneral form of function body parts
Declaration statementsDeclaration statementsVariables and constantsVariables and constants
Executable statementsExecutable statementsC++ statementsC++ statements
![Page 29: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/29.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
29
General Form of a C++ Program
General formGeneral form// File: filename// File: filename// Program description:// Program description:#include directives#include directivesint main()int main(){{
Declarations sectionDeclarations sectionExecutable statements sectionExecutable statements section
}}
![Page 30: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/30.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
30
Arithmetic Expressions int data typeint data type
+ - * /, Assignment, input and output on int+ - * /, Assignment, input and output on int % Only used with int% Only used with int
Examples of integer divisionExamples of integer division15 / 3 = 515 / 3 = 5
15 / 2 = 715 / 2 = 7
0 / 15 = 00 / 15 = 0
15 / 0 15 / 0 undefinedundefined
![Page 31: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/31.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
31
Modulus and Integer Used only with integer and yields remainderUsed only with integer and yields remainder Examples of integer modulusExamples of integer modulus
7 % 2 = 17 % 2 = 1
299 % 100 = 99299 % 100 = 99
49 % 5 = 449 % 5 = 4
15 % 0 undefined15 % 0 undefined
![Page 32: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/32.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
32
Mixed-type Assignments
Expression evaluated Expression evaluated Result stored in the variable on the left sideResult stored in the variable on the left side C++ can mix typesC++ can mix types
float a, b, x;float a, b, x;
int m, n;int m, n;
a=10;a=10;
b=5;b=5;
x = m / n;x = m / n;
![Page 33: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/33.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
33
Expressions With Multiple Operators Operator precedence tells how to evaluate Operator precedence tells how to evaluate
expressionsexpressions Standard precedence orderStandard precedence order
()() Evaluated first, if nested innermostEvaluated first, if nested innermostdone firstdone first
* / %* / % Evaluated second. If there are Evaluated second. If there are several,several,
then evaluate from left-to-rightthen evaluate from left-to-right + -+ - Evaluate third. If there are several,Evaluate third. If there are several,
then evaluate from left-to-rightthen evaluate from left-to-right
![Page 34: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/34.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
34
Mathematical Formulas in C++
a = bc not valid C++ syntaxa = bc not valid C++ syntax * Operator* Operator a = b * c;a = b * c;
m = m = y - b y - b x - ax - a
( ) And /( ) And / m = (y - b) / (x - a);m = (y - b) / (x - a);
![Page 35: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/35.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
35
Example 1: Milesbatch.cpp// File: milesBatch.cpp// File: milesBatch.cpp
// Converts distance in miles to // Converts distance in miles to kilometers.kilometers.
#include <iostream>#include <iostream>
using namespace std;using namespace std;
int main() // start of main functionint main() // start of main function
{{
const float KM_PER_MILE = 1.609; const float KM_PER_MILE = 1.609;
float miles, float miles,
kms; kms;
![Page 36: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/36.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
36
Milesbatch.cpp
// Get the distance in miles.// Get the distance in miles.
cin >> miles;cin >> miles;
cout << "The distance in miles is " << cout << "The distance in miles is " <<
miles << endl;miles << endl;
// Convert the distance to kilometers.// Convert the distance to kilometers.
kms = KM_PER_MILE * miles;kms = KM_PER_MILE * miles;
// Display the distance in kilometers.// Display the distance in kilometers.
cout << "The distance in kilometers is " <<cout << "The distance in kilometers is " <<
kms << kms << endl;endl;
return 0;return 0;
}}
![Page 37: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/37.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
37
Milesbatch.cpp
Program outputProgram output
The distance in miles is 10The distance in miles is 10
The distance in kilometers is 16.09The distance in kilometers is 16.09
![Page 38: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/38.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
38
Example 2: Coin Collection Case Study
Problem statementProblem statement Saving nickels and pennies and want to Saving nickels and pennies and want to
exchange these coins at the bank so need exchange these coins at the bank so need to know the value of coins in dollars and to know the value of coins in dollars and cents.cents.
AnalysisAnalysis Count of nickels and pennies in totalCount of nickels and pennies in total Determine total valueDetermine total value Use integer division to get dollar valueUse integer division to get dollar value
/ 100/ 100
![Page 39: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/39.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
39
Coin Collection Case Study Analysis (cont)Analysis (cont)
Use modulus % to get cents valueUse modulus % to get cents value % 100% 100
DesignDesign Prompt for namePrompt for name Get count of nickels and penniesGet count of nickels and pennies Compute total valueCompute total value Calculate dollars and centsCalculate dollars and cents Display resultsDisplay results
![Page 40: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/40.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
40
Coin Collection Case Study
ImplementationImplementation Write C++ code of designWrite C++ code of design Verify correct data types neededVerify correct data types needed Mixed mode types and promotionMixed mode types and promotion
TestingTesting Test results using various input combinationsTest results using various input combinations
![Page 41: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/41.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
41
Coins.cpp// File: coins.cpp// File: coins.cpp// Determines the value of a coin collection// Determines the value of a coin collection
#include <iostream>#include <iostream>#include <string>#include <string>
using namespace std;using namespace std;
int main()int main(){{// Local data ...// Local data ... string name;string name; int pennies; int pennies; int nickels; int nickels; int dollars; int dollars; int change; int change; int totalCents; int totalCents;
// Prompt sister for name.// Prompt sister for name. cout << "Enter your first name: ";cout << "Enter your first name: "; cin >> name;cin >> name;
![Page 42: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/42.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
42
Coins.cpp // Read in the count of nickels and pennies.// Read in the count of nickels and pennies. cout << "Enter the number of nickels: ";cout << "Enter the number of nickels: "; cin >> nickels;cin >> nickels; cout << "Enter the number of pennies: ";cout << "Enter the number of pennies: "; cin >> pennies;cin >> pennies;
// Compute the total value in cents.// Compute the total value in cents. totalCents = 5 * nickels + pennies;totalCents = 5 * nickels + pennies;
// Find the value in dollars and change.// Find the value in dollars and change. dollars = totalCents / 100;dollars = totalCents / 100; change = totalCents % 100;change = totalCents % 100; // Display the value in dollars and change.// Display the value in dollars and change. cout << "Good work " << name << '!' << endl;cout << "Good work " << name << '!' << endl; cout << "Your collection is worth " << cout << "Your collection is worth " <<
dollars << " dollars and " << dollars << " dollars and " << change << " cents." << endl;change << " cents." << endl;
return 0;return 0;}}
![Page 43: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/43.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
43
Coins.cpp
Program outputProgram output
Enter your first name and press return: SallyEnter your first name and press return: Sally
Enter number of nickels and press return: 30Enter number of nickels and press return: 30
Enter number of pennies and press return: 77Enter number of pennies and press return: 77
Good work sally!Good work sally!
Your collection is worth 2 dollars and 27 Your collection is worth 2 dollars and 27 cents.cents.
![Page 44: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/44.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
44
Interactive Mode, Batch and Data Files Two modes interactive or batchTwo modes interactive or batch
Keyboard input interactiveKeyboard input interactive Batch mode data provided prior to startBatch mode data provided prior to start
File as inputFile as input Input / output redirectionInput / output redirection
Direct input to program use ‘<‘ symbolDirect input to program use ‘<‘ symbol Direct output to a file use ‘>‘ symbolDirect output to a file use ‘>‘ symbol
![Page 45: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/45.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
45
Input / Output Redirection Program name < datafileProgram name < datafile
Metric < mydataMetric < mydata Program name > outFileProgram name > outFile
Metric > outFileMetric > outFile
Input and output redirectionInput and output redirection
Metric < inFile > outFileMetric < inFile > outFile
![Page 46: Overview of C++](https://reader030.vdocuments.us/reader030/viewer/2022012908/568151e0550346895dc0199f/html5/thumbnails/46.jpg)
CSCI 106, Overview of C++, by Dr. Awad Khalil
46
Common Programming Errors SyntaxSyntax
Programs rarely compilePrograms rarely compile Something always goes wrongSomething always goes wrong Systematic solutionsSystematic solutions
Compiler not descriptiveCompiler not descriptive Look at line number before and after errorLook at line number before and after error Watch missing ; and }Watch missing ; and }
Run-time errorsRun-time errors Illegal operation (divide by 0)Illegal operation (divide by 0)
Logic errorsLogic errors Program functions differently than you expectProgram functions differently than you expect