Chapter 3
Chapter 3
1
2
3
4
FORMATTED INPUT / OUTPUT{
5
6
[Chapter 3]
7
8
9 ‘Objectives,
10 ▪ To understand and use various member functions for C++ formatted
11 I/O.
▪ To understand and use various stream manipulators for C++
12
13 } formatted I/O.
14
Programming Language
Engineering
1
2
3.1 iostream Library
▪ In this Module we will discuss how this formatted I/O implemented in C++ by using member functions and stream
3 manipulators.
▪ Discuss the formatted I/O here, for file I/O and some of the member functions mentioned in this Module, will be
4 presented in another Module.
5 ▪ The header files used for formatted I/O in C++ are:
6
7
8
9
10
11
12
13
14
Programming Language
Engineering
1 ▪ The compilers that fully comply with the C++ standard that use the template based header files won’t need
2 the .h extension. Please refer to Namespaces for more information.
▪ The iostream class hierarchy is shown below. From the base class ios, we have a derived class:
3
4
5
6
7
8 ▪ So, iostream support both stream input and output. The class hierarchy is shown below.
9
10
11
12
13
14 }
Programming Language
Engineering
Programming Language
Engineering
1 ▪ For file processing C++ uses (will be discussed in another Module) the following classes:
2
3
4
5
6
7 ▪ Stream output program example:
// string output using <<
8 #include <iostream>
9 using namespace std;
int main()
10 {
cout<<"Welcome to C++ I/O module!!!"<<endl;
11 cout<<"Welcome to ";
cout<<"C++ module 18"<<endl;
12 // endl is end line stream manipulator
13 // issue a new line character and flushes the output buffer
}
// output buffer may be flushed by cout<<flush; command
14 return 0;
}
Programming Language
Engineering
2
// concatenating <<
3 #include <iostream>
using namespace std;
4 int main()
{
5 int p = 3, q = 10;
6
cout << "Concatenating using << operator.\n"
7 <<"--------------------------------"<<endl;
cout << "70 minus 20 is "<<(70 - 20)<<endl;
8 cout << "55 plus 4 is "<<(55 + 4)<<endl;
cout <<p<<" + "<<q<<" = "<<(p+q)<<endl;
9 return 0;
10 }
11
12
13
14 }
Programming Language
Engineering
2
#include <iostream>
3 using namespace std;
4 int main()
{
5 int p, q, r;
6 cout << "Enter 3 integers separated by space: \n";
cin>>p>>q>>r;}
7
// the >> operator skips whitespace characters such as tabs,
8 // blank space and newline.
cout<<"Sum of the "<<p<<","<<q<<" and "<<r<<" is = "<<(p+q+r)<<endl;
9 return 0;
10 }
11
12
13
14 }
Programming Language
Engineering
Programming Language
Engineering
1 3. get() with three arguments, a character array, a size limit and a delimiter (default value ‘\n’). It reads characters
from the input stream, up to one less than the specified maximum number of characters and terminates or terminates
2 as soon as the delimiter is read. For example:
3 char namevar[30];
...
4 cin.get(namevar, 30);
5 // get up to 29 characters and inserts null
6 // at the end of the string stored in variable
// namevar. If a delimiter is found,
7 // the read terminates. The delimiter
// is left in the stream, not stored in the array.
8
9 4. getline() operates like the third get() and insert a null character after the
line in the character array. It removes the delimiter from the stream, but does not
10 store it in the character array.
▪ A program examples:
11
12
13
14
Programming Language
Engineering
// get() version
1 #include <iostream>
2 using namespace std;
4 int main()
{
5 char bufferOne[SIZE], bufferTwo[SIZE];
cout <<"Enter a line of text:"<<endl;
6 cin>>bufferOne;
// store the string in array bufferOne
7 // just the first word in the array string, then the
// first whitespace encountered
8 cout<<"\nThe line of text read by cin>> was:"<<endl;
t<<bufferOne<<endl;
9 cin.get(bufferTwo, SIZE);
// the rest of the string
10 cout<<"The line of text read by cin.get(bufferTwo,SIZE) was:"<<endl;
cout<<bufferTwo<<endl;
11 return 0;
}
12
13
14 }
Programming Language
Engineering
// getline() example
1 #include <iostream>
2 using namespace std;
const SIZE = 100;
3
int main()
4 {
char buffer[SIZE];
5
cout<<"Read by cin.getline(buffer, SIZE)\n"
6 <<"--------------------------------\n"<<"Enter a line of text:"<<endl;
cin.getline(buffer, SIZE);
7 cout<<"The line of text entered is: "<<endl;
cout<<buffer<<endl;
8 return 0;
}
9
10
11
12
13
14 }
Programming Language
Engineering
▪ ignore() member function skips over a designated number of characters (default is one character) or terminates
1 upon encountering a designated delimiter (default is EOF). For example:
2 cin.ignore(); // gets and discards 1 character.
cin.ignore(5); // gets and discards 5 characters.
3
4 cin.ignore(20,’\n’);
// gets and discards up to 20 characters or until
5 // newline character whichever comes first.
10 ▪ peek() member function returns the next character from an input stream, but does
not remove the character from the stream. For example:
11 char chs;
12 ...
chs = cin.peek();
13 // peek at character
14
Programming Language
Engineering
1 ▪ Unformatted I/O performed with read() and write() member functions. They simply input or output as raw byte.
▪ The read() member function extracts a given number of characters into an array and the write() member function
2 inserts n characters (nulls included). For example:
3 char texts[100];
...
4 cin.read(texts, 100);
5 // read 100 characters from input stream and don’t append ‘\0’
6
7
8
9
10
11
12
13
14
Programming Language
Engineering
11
12
13
14 }
Programming Language
Engineering
▪ Program example:
1
// using hex, oct, dec and setbase stream manipulator
2 ##include <iostream>
#include <iomanip>
3 using namespace std;
4
int main()
5 {
int p;
6
cout<<"Enter a decimal number:"<<endl;
7 cin>>p;
8 cout<<p<< " in hexadecimal is: "
<<hex<<p<<'\n’
9 <<dec<<p<<" in octal is: "
<<oct<<p<<'\n’
10 <<setbase(10) <<p<<" in decimal is: "
11 <<p<<endl;
cout<<endl;
12 return 0;
}
13
14 }
Programming Language
Engineering
}
cout<<setprecision(poinplace)<<theroot<<endl;
14 return 0;
}return 0;
}
Programming Language
{ THANK YOU
Any questions?
}
Engineering
1
2
3
4
5
6 John 3:16 – For God so loved the world, that He
7
8
gave His only begotten Son, that whoever
9 believes in Him, shall not perish but have
10 everlasting life.
11
12
13
14 }
Programming Language