1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.

Slides:



Advertisements
Similar presentations
Chapter 3. Expressions and Interactivity Csc 125 Introduction to C++
Advertisements

Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions.
CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
Variables: Named Storage Locations Variables must be defined or declared before they can be used so that appropriate memory storage can be allocated for.
1 Objectives Understand streamed input and output.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
C++ Typecasting. Math Library Functions.. Operator / Operands A = x + y.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
CS150 Introduction to Computer Science 1
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 3- 1.
CS Sept 2006 Pick ups from chapters 2 and 3.
CS 1400 Chapter 3: sections Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example: int.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
CS Jan 2007 Chapter 3: sections Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example:
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions and Interactivity.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for CMPS 1043 Computer Science I at MSU.
LOOP & Type Conversion. do – while Loop In the while loop, the test expression is evaluated at the beginning of the loop. If the test condition is false.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 3 Expressions and Interactivity.
Expressions and Interactivity. 3.1 The cin Object.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Formatting Output.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 3: Expressions and Interactivity.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Last Time…. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Chapter Expressions and Interactivity 3. The cin Object 3.1.
Chapter 4: Introduction To C++ (Part 3). The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Information.
Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3: Expressions and Interactivity.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Lecture 3 Expressions, Type Conversion, Math and String
Completing the Problem-Solving Process
Chapter 3. Expressions and Interactivity
Chapter 3: Expressions and Interactivity.
Type Conversion, Constants, and the String Object
getline() function with companion ignore()
Expressions and Interactivity
Lecture 3 Expressions Richard Gesick.
Starting Out with C++: From Control Structures through Objects
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
Expressions and Interactivity
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Standard Version of Starting Out with C++, 4th Edition
Arithmetic Operations
Lecture 3 Expressions, Type Conversion, and string
Data Types and Expressions
getline() function with companion ignore()
Data Types and Expressions
Presentation transcript:

1 9/13/06CS150 Introduction to Computer Science 1 Type Casting

2 9/13/06CS150 Introduction to Computer Science Practice  Write a C++ program that allows the user the ability to enter their name and the number of nickels and pennies they have. You are then to print the number of dollars and change that corresponds to. The change should be in the form of nickels and pennies

3 9/13/06CS150 Introduction to Computer Science 1 Implicit Type Conversion (3.3)  What happens when we mix the data types of operands during mathematical operations o What happens when we save a double as an int? o What happens when an int is multiplied by a float?  Data types are ranked  A data type outranks another if it can hold a larger number

4 9/13/06CS150 Introduction to Computer Science 1 long double double float unsigned long long unsigned int int Highest Lowest

5 9/13/06CS150 Introduction to Computer Science 1 Rules for Type Conversion  When a value is converted to a higher data type, it is being promoted  When a value is converted to a lower data type, it is being demoted  Rule 1: char, short, and unsigned short are automatically promoted to int  Rule 2: When an operator works with values of different types, the lower ranking value is promoted to the higher ranking  Rule 3: When the value of an expression is assigned to a variable, it is converted to the data type of that variable

6 9/13/06CS150 Introduction to Computer Science Practice  Assume the following variable definitions int a = 5, b = 12; double x = 3.4;  What are the values of the following expressions: a. b / x b. x * a

7 9/13/06CS150 Introduction to Computer Science 1 Explicit Type Conversion (3.4)  A type cast expression let’s you manually change the data type of a value  The syntax for type casting is o static_cast (Value) o Value is a variable or literal value o DataType is the data type that you are converting Value into

8 9/13/06CS150 Introduction to Computer Science Example of Type Casting double number = 3.7; int val; val = static_cast (number);  What is saved into val?

9 9/13/06CS150 Introduction to Computer Science 1 Uses of Type Casting  Preventing integer division int books = 30, months = 7; double booksPerMonth; booksPerMonth = static_cast (books) / months; o What about this statement? booksPerMonth = static_cast (books / months);  Displaying a char from its ASCII value int number = 65; cout (number)

10 9/13/06CS150 Introduction to Computer Science Practice  What is the value of each of the variables while this expression is being executed? int total; double gradeCounter, average; total = 310; gradeCounter = 4; average = static_cast (total) / gradeCounter;

11 9/13/06CS150 Introduction to Computer Science 1 Overflow and Underflow (3.5)  What happens when a variable is assigned a value that is too large or too small in range for that variable’s data type? short testVar = 32767; cout << testVar << endl; testVar = testVar + 1; cout << testVar << endl; testVar = testVar - 1; cout << testVar << endl;

12 9/13/06CS150 Introduction to Computer Science 1 Multiple Assignments (3.7)  C++ allows statements such as: a = b = c = d = 45;  Why do you think that is?  What is the associativity of the assignment operator?

13 9/13/06CS150 Introduction to Computer Science 1 Combined Assignments  The same variable can be used on the left hand side of the assignment and on the right hand side notes = notes / 20; note = notes % 20;  These are common in programming, so the two operators can be combined as follows: notes /= 20; note %= 20;

14 9/13/06CS150 Introduction to Computer Science 1 Examples of Combined Assignments OperatorExample UsageEquivalent To +=x += 5;x = x + 5; -=y -= 2;y = y – 2; *=z *= 10;z = z * 10; /=a /= b;a = a / b; %=c %= 3;c = c % 3;

15 9/13/06CS150 Introduction to Computer Science Combined Assignments  Combined assignments can be combined with arithmetic operators y -= a * 2; a /= b + c; C %= d – 3;  What is the long form of these statements?

16 9/13/06CS150 Introduction to Computer Science What is the Output? int unus, duo, tres; unus = duo = tres = 5; unus += 4; duo *= 2; tres -= 4; unus /= 3; duo += tres; cout << unus << endl; cout << duo << endl; cout << tres << endl;

17 9/13/06CS150 Introduction to Computer Science 1 getline  What happens when the user types their first and last name for the following code segment? string name; cout << “Enter your name: “; cin >> name;

18 9/13/06CS150 Introduction to Computer Science 1 getline  cin passes over and ignores leading whitespaces, but will stop reading once it gets to the first whitespace character after the string  Solution? o Use getline function

19 9/13/06CS150 Introduction to Computer Science 1 getline string name; cout << “Enter your name: “; getline(cin, name);

20 9/13/06CS150 Introduction to Computer Science 1 getline  Syntax for getline getline(cin, inputLine);  Where o cin is the input stream o inputLine is the variable where the string will be stored

21 9/13/06CS150 Introduction to Computer Science 1 cin.get()  Used to read one character from the keyboard at a time  Also reads new lines, spaces, and tabs as a character o ‘\n’: new line o ‘\t’: tab o ‘ ‘: space

22 9/13/06CS150 Introduction to Computer Science 1 Example char ch; cout << “This program has paused.”; cout << “Press Enter to continue.”; cin.get(ch); cout << “Thank you!” << endl;