0% found this document useful (0 votes)
19 views

CPP Lecture 04

The document discusses basic input and output in C++ programming using streams. It covers standard input stream cin which is used to accept user input, standard output stream cout which is used to print to the screen, and standard error stream cerr and standard logging stream clog. It provides examples of using cin and cout to accept values from the user and display output at runtime.

Uploaded by

jiamah2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

CPP Lecture 04

The document discusses basic input and output in C++ programming using streams. It covers standard input stream cin which is used to accept user input, standard output stream cout which is used to print to the screen, and standard error stream cerr and standard logging stream clog. It provides examples of using cin and cout to accept values from the user and display output at runtime.

Uploaded by

jiamah2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Programming Fundamentals

(Using C++)
Huma Israr
Department Of Information Engineering Technology
National Skills University, Islamabad.
[email protected]
Fall 2023, BS-CS
2

Week 03

Lecture – 01
Lecture – 02
Building Programs: Basic input/output

Semester Calendar
3

Class Schedule

Lab Hours (Practice)


Monday (11:00 to 1:00)
Basic Input/Output
 The example programs of the previous sections printed simple
values on screen, but the standard library provides many
additional ways to interact with the user via its input/output
features.
 C++ uses a convenient abstraction called streams to perform
input and output operations
 C++ comes with libraries that provide us with many ways for
performing input and output.
Basic Input/Output:
 Input Stream: If the direction of flow of bytes is from the device (for
example, Keyboard) to the main memory then this process is called input.
 Output Stream: If the direction of flow of bytes is opposite, i.e. from main
memory to device( display screen ) then this process is called output.
 cin, cout, cerr, and clog are streams that handle standard inputs and
standard outputs. These are stream objects defined in iostream header file.
 stream description
 cin standard input stream
 cout standard output stream
 cerr standard error (output) stream
 clog standard logging (output) stream
Standard output (cout)
 On most program environments, the standard output by default is the
screen, and the C++ stream object defined to access it is cout
cout << "Output sentence"; // prints Output sentence on screen
cout << 120; // prints number 120 on screen
cout << x; // prints the value of x on screen

 Multiple insertion operations (<<) may be chained in a single statement:


cout << "This " << " is a " << "single C++ statement";

 Chaining insertions is especially useful to mix literals and variables in a


single statement:
cout << "I am " << age << " years old and my zipcode is " << zipcode;
Standard input (cin):

int age;
cin >> age;
 The first statement declares a variable of type int called age,
 the second statement extracts from cin a value to be stored in it.
 This operation makes the program wait for input from cin;
generally, this means that the program will wait for the user to enter
some sequence with the keyboard
Program to accept values at Run Time ( Using cin>> )

//cin>> Example
#include <iostream>
using namespace std;
int main ()
{
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
return 0; }
Cin>>
cin >> a;
cin >> b;
 Extractions on cin can also be chained to request more than
one datum in a single statement:
cin >> a >> b;
Using Static variables Values at runtime (using cin>>)

// using Static Variables // using cin>>


#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() int main()
{ {
int a,b,c; int a,b;
a=3; b=4; cin>>a; cin>>b;
int c=a+b ; int c=a+b ;
cout << "Ans is “<<c; cout << "Ans is “<<c;
return 0; } return 0; }
using cerr & clog
Program 1 Program 2

// using Cerr // using Clog


#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() int main()
{ {
cerr << "An error occured"; clog << "An error occured";
return 0; return 0;
} }
Any Question

You might also like