Programming Fundamental-1

Slides:



Advertisements
Similar presentations
Numeric Types, Expressions, and Output ROBERT REAVES.
Advertisements

CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 2: Basic Elements of C++
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
Chapter 2: Basic Elements of C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
Chapter 2: Basic Elements of C++
Chapter 3: Input/Output
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
CSI 2372: Advanced Programming Concepts with C++
Due Dates Quiz 1, 2 : Friday September 11 th Quiz 3 : Friday September 18 th Quiz 4 : Monday September 28 th Lab 1, 2 and 3 : Friday Sep 11 th Project.
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
CHAPTER 3 INPUT/OUTPUT. In this chapter, you will:  Learn what a stream is and examine input and output streams  Explore how to read data from the standard.
Numeric Types, Expressions, and Output 1. Chapter 3 Topics Constants of Type int and float Evaluating Arithmetic Expressions Implicit Type Coercion and.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 3: Input/Output.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
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.
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:
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
Lecture #5: iostream functions دوال الإدخال والإخراج Dr. Hmood Al-Dossari King Saud University Department of Computer Science 4 March 2012.
Chapter 3 Arithmetic Expressions, Function Calls, and Output
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
Chapter 3: Input/Output
Expressions and Interactivity. 3.1 The cin Object.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Input/Output. Objectives In this chapter you will: Learn what a stream is and examine input and output streams Explore how to use the input stream functions.
Chapter 2: Basic Elements of C++. Introduction Computer program – Sequence of statements whose objective is to accomplish a task Programming – Process.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
1 A Simple “Hello World” Example #include // input-output library using namespace std; int main() // function main { cout
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
CSCI 125 & 161 / ENGR 144 Lecture 6 Martin van Bommel.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
Chapter 3 Numeric Types, Expressions, and Output
Lecture 3 Expressions, Type Conversion, Math and String
TK1913 C++ Programming Basic Elements of C++.
Chapter 2: Basic Elements of C++
Topic 2 Input/Output.
C++ Basic Input and Output (I/O)
Arithmetic Expressions Function Calls Output
Programming Fundamentals
EGR 2261 Unit 3 Input/Output Read Malik, Chapter 3.
Chapter 2: Basic Elements of C++
Chapter 2 part #3 C++ Input / Output
Input and Output Chapter 3.
Expressions and Interactivity
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
Chapter 3: Input/Output
Chapter 3 Input output.
Chapter 3: Expressions and Interactivity
Formatted Input, Output & File Input, Output
Chapter 2 part #3 C++ Input / Output
C++ Programming Basics
C++ for Engineers and Scientists Second Edition
Presentation transcript:

Programming Fundamental-1 Instructor Name: Muhammad Safyan Lecture-4

Lecture outline Type conversion Inuput stream function Output stream function

Implicit Type-Conversion vs. Explicit Type-Casting Converting a value from one type to another type is called type casting (or type conversion). Two kinds of type casting: 1. Implicit type-conversion performed by the compiler automatically, and 2. Explicit type-casting via an unary type-casting operator in the form of (new-type)operand or new-type(operand).

Implicite Type-Conversion Implicit (Automatic) Type Conversion C++ automatically converts the value to the receiving type, if the two types are compatible. If you assign an int value to a double variable, the compiler automatically casts the int value to a double double (e.g., from 1 to 1.0) and assigns it to the double variable. if you assign a double value of to an int variable, the compiler automatically casts the double value to an int value (e.g., from 1.2 to 1) and assigns it to the int variable. The fractional part would be truncated and lost.

Explicit Type-Casting You can explicitly perform type-casting via the so-called unary type-casting operator in the form of (new-type)operand or new-type(operand). The type-casting operator takes one operand in the particular type, and returns an equivalent value in the new type. Take note that it is an operation that yields a resultant value, similar to an addition operation although addition involves two operands. For example,

Type Conversion (Casting)

I/O Streams I/O is a sequence of bytes, called a stream, from the source to the destination. I/O is a sequence of Character, called a stream, from the source to the destination (if it take Input from key board only) For I/O , header file iostream is necessary There are two data types in iostream, istream (input stream) and ostream (output stream).

I/O Streams These variable declarations are similar to the following C++ statements: istream cin; ostream cout; The variable cin has access to operators and functions that can be used to extract data from the standard input device

cin and the Extraction Operator >> Suppose payRate is double variable cin >> payRate; The extraction operator >> is binary and thus takes two operands. The left-side operand must be an input stream variable, such as cin. And right hand side is a variable to store the data. cin >> payRate >> hoursWorked; Equivalent to cin >> hoursWorked; White space or carriage return are equivalent to segregate the two inputs

cin and the Extraction Operator >>

cin and the Extraction Operator >>

cin and the Extraction Operator >>

cin and the Extraction Operator >>

Using Predefined Functions in a Program A function, also called a subprogram, is a set of instructions. A function executes, it accomplishes something. e.g. main( ) C++ comes with a wealth of functions, called predefined functions, that are already written. Organized as a collection of libraries, called header files. You can call the functions placed in liberary , after including it in program Pow (2,3) is a power function in math library Length(“gcu”) dtermine the length of string placed in string liberary

cin and the get Function Consider below code along with input char ch1, ch2; int num; cin >> ch1 >> ch2 >> num; and the input: A 25 The variable cin can access the stream function get, which is used to read character data. The get function inputs the very next character, including whitespace characters. The syntax of cin, together with the get function to read a character, follows: cin.get(varChar);

cin and the get Function cin.get(ch1); cin.get(ch2); cin >> num; Note: get function can only accommodate char type data.

Input Failure A program that is syntactically correct might produce incorrect results. What about an attempt to read invalid data? For example, what would happen if you tried to input a letter into an int variable? If the input data did not match the corresponding variables, the program would run into problems. int a,b,c; cin>>a>>b>>c; Input is X 6 7

The clear Function istreamVar.clear(); Istreamvar.ignore() int a,b,c; cin>>a; cin.clear(); cin.ignore(); cin>>b; cin>>c; cout<<a<<"\n"<<b<<"\n"<<c;

Output and Formatting Output Header file for output formatting: #include <iomanip> cout << expression or manipulator << expression or manipulator...; setprecision Manipulator: control the output of floating-point numbers setprecision(n) e.g. main() { double f =3.14159; cout<<setprecision(5) << f << '\n'; cout << setprecision(9) << f << '\n'; }

Output and Formatting Output fixed Manipulator: fixed decimal format, cout << fixed; main() { double f =3.14159; cout << setprecision(5) << f << '\n'; cout << setprecision(9) << f << '\n'; cout<<"\n"; system("pause"); }

Output and Formatting Output Setw manipulator used to output the value of an expression in a specific number of columns. outputs the value of the next expression in n columns. The output is right-justified. Specify the number of columns to be 8, for example, and setw(n) cout << setw(5) << x << endl;

Output and Formatting Output main() { double f =3.14159; cout<<setw(20)<<f; cout<<'\n'; system("pause"); }

Output and Formatting Output setfill Manipulator Setw ( )is right justified and remaining column are unfilled/whitespace. cout << setfill('#'); e.g. double f =3.14159; cout << setfill('*'); cout<<setw(20)<<f; Left and right Manipulators Setw() is right justified by defualt You can do left justified or right justified explicitly cout << left;