CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.

Slides:



Advertisements
Similar presentations
Chapter 3: Expressions and Interactivity. Outline cin object Mathematical expressions Type Conversion and Some coding styles.
Advertisements

CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
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/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 3 Expressions.
CS150 Introduction to Computer Science 1
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 3- 1.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Basic Elements of C++ Chapter 2.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions and Interactivity.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
C++ Programming: Basic Elements of C++.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 3 Expressions and Interactivity.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Expressions and Interactivity. 3.1 The cin Object.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Formatting Output.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
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.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Chapter Expressions and Interactivity 3. The cin Object 3.1.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
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.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3: Expressions and Interactivity.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
Lecture 3 Expressions, Type Conversion, Math and String
TK1913 C++ Programming Basic Elements of C++.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2 Introduction to C++ Programming
Chapter 3 Assignment and Interactive Input.
Programming Fundamentals
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
Basic Elements of C++ Chapter 2.
Expressions and Interactivity
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Primitive Types and Expressions
C++ for Engineers and Scientists Second Edition
Lecture 3 Expressions, Type Conversion, and string
Getting Started With Coding
Presentation transcript:

CMPSC 121- Spring 2015 Lecture 6 January 23, 2015

Input  C++ utilizes the cin object and extraction operator to read data from keyboard and store in memory locations. cin>> variablename;  Variable must be declared before it can be used.  Input stops when white space is encountered.  Can enter multiple values by separated by extraction operators. cin>>x>>y; //data is stored in order given  Usually users are prompted by a cout statement to enter data.

Examples cout > radius; // must be declared prior cout > length >>width; // must be declared prior ----or---- cin>>length; cin >>width;

Caution  cin does not confirm data type.  Given the following code segment int alpha; cout >alpha; What would happen if the user entered a floating point number or a character? (See also pg 87 in book)

Input for char Variable  Remember that a char variable only stores one character (letter, digit, or special character).  Using cin will allow the extraction of 1 character from the input stream.  White spaces are skipped.  Extra characters remain in the input stream.

Input of C-strings  Remember a C-string is an array of characters and may be declared as char varname[n]; char last[12];  You can use cin to store characters in this C- string. cin>>last;  Input will stop is a white space is encountered.  There is no check for number of characters.

Input of String Objects  Remember to declare a variable of the class string, you must include the string library file. # include string first;  You can use cin to store characters in this string object cin>>first;  Input will stop is a white space is encountered.  String objects do not have a preset length.

Questions ???

Data Overflow and Underflow   C++ does not check for data being within range of data type.   How the data is stored may be compiler dependent.   In many compilers, the number will “wrap”. For example given the following statements short x = 32767; x = x + 5; cout<< x<<endl; will usually output

Data Type Coercion   Automatic conversion of a data type for mathematical operations or assignment. int alpha = 10.5; // 10.5 is coerced to 10 double beta = 4; // 4 is coerced to 4.0 alpha = alpha/beta; //two coercions occur //10 is coerced to 10.0 for the division //the value 2.5 is truncated to 2 for assignment. beta = alpha * 4.4; //what happens to alpha, beta?

Type Casting   Code specifies the conversion of the data type for the purposes of an operation.   Three methods: static_cast (variable or expression) data type (variable or expression) (data type) variable or expression The latter two are considered to be C-style   Type casting a variable will not change what is stored in a variable

Examples   Consider the following statements: int a = 6, b = 9; double c; c = static_cast (b)/a; // the value stored in b, 9, is converted to 9.0 // before the division, but the integer value 9 is // still stored in b; cout<<a<<“ ”<< b <<“ ”<<c<<endl; //what is output?   Would c = static_cast (b/a); produce the same results.

Remember   There are only 2 methods to change what is stored in a variable.   Assignment statement   Input new value

Named Constants   Named, or symbolic, constants allow the user to assign a value that will not change to a memory location   Has the format of const data type identifier = value; const double PI = ;   Like variables, named constants must be declared before they are used (usually before main).   A value must be assigned at time of declaration.

Named Constants   Utilizes space in main memory, however the value in memory space cannot be changed unless code is changed. PI = 4.234; // would be a syntax error   Makes updates to the program easier if a value is to be used over and over again

Define   Utilizes a preprocessor directive to associate a value with a word.   Has the format #define identifier value #define tax_rate 4.5 the data type is not needed because no memory space is allocated.   No memory space is used. During compilation, the word is changed to the value.

Questions

Compound Operators  Provide a shortcut for assignment statements.  Combines the = operator with another arithmetic operator a + = 5.67; is equivalent to a = a ; a + = 5.67; is equivalent to a = a ; a -= 6.0; is equivalent to a = a – 6.0; a *= 3.4; is equivalent to a = a * 3.4; a /= 2.1; is equivalent to a = a /2.1; a %= 3; is equivalent to a = a % 3;  Reminder---the % operator only works for integer data types.

Examples  Suppose we had the following declarations: double alpha = 6.0, beta = 7.5; int gamma = 8;  What would be the result of the following?  alpha += 4;  gamma *= 4;  beta -= 4.5;  gamma /= 6.0;  beta %= 2;

Questions