Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Fundamental-1

Similar presentations


Presentation on theme: "Programming Fundamental-1"— Presentation transcript:

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

2 Lecture outline Type conversion Inuput stream function
Output stream function

3 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).

4 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.

5 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,

6 Type Conversion (Casting)

7

8 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).

9 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

10 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

11 cin and the Extraction Operator >>

12 cin and the Extraction Operator >>

13 cin and the Extraction Operator >>

14 cin and the Extraction Operator >>

15 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

16 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);

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

18 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

19 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;

20 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 = ; cout<<setprecision(5) << f << '\n'; cout << setprecision(9) << f << '\n'; }

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

22 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;

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

24 Output and Formatting Output
setfill Manipulator Setw ( )is right justified and remaining column are unfilled/whitespace. cout << setfill('#'); e.g. double f = ; 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;


Download ppt "Programming Fundamental-1"

Similar presentations


Ads by Google