06 Input and Output Stud
06 Input and Output Stud
Programming Technique I
(SCSJ1013)
Formatting Output
Introduction to Output Formatting
Can control how output displays for numeric and string
data:
size
position
number of digits
#include <iostream>
#include <iomanip>
int main()
{
cout << "*" << -17 << "*" << endl;
cout << "*" << setw(6) << -17 << "*" << endl << endl;
cout << "*" << "Hi there!" << "*" << endl;
cout << "*" << setw(20) << "Hi there!" << "*" << endl;
cout << "*" << setw(3) << "Hi there!" << "*" << endl;
return 0;
}
Example 1: left and right
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int x = 15;
int y = 7634;
cout << left;
cout << setw(5) << x << setw(7) << y << setw(8) << "Warm"
<< endl;
int main()
{
cout << "*" << -17 << "*" << endl;
cout << "*" << setw(6) << -17 << "*" << endl;
cout << left;
cout << "*" << setw(6) << -17 << "*" << endl << endl;
cout << "*" << "Hi there!" << "*" << endl;
cout << "*" << setw(20) << "Hi there!" << "*" << endl;
cout << right;
cout << "*" << setw(20) << "Hi there!" << "*" << endl;
return 0;
}
Example 1: fixed
#include <iostream>
using namespace std;
int main()
{
double x = 15.674;
double y = 235.73;
double z = 9525.9874;
return 0;
}
Example 2: fixed
#include <iostream>
using namespace std;
int main()
{
float small = 3.1415926535897932384626;
float large = 6.0234567e17;
float whole = 2.000000000;
cout << "Some values in general format" << endl;
cout << "small: " << small << endl;
cout << "large: " << large << endl;
cout << "whole: " << whole << endl << endl;
#include <iostream>
using namespace std;
int main()
{
double x = 15.674;
double y = 235.73;
double z = 9525.9874;
int main()
{
float lots = 3.1415926535, little1 = 2.25;
float little2 = 1.5, whole = 4.00000;
cout << "Some values with noshowpoint (the default)" << endl;
cout << "lots: " << lots << endl;
cout << "little1: " << little1 << endl;
cout << "little2: " << little2 << endl;
cout << "whole: " << whole << endl << endl;
int main()
{
double x = 15.674;
double y = 235.73;
double z = 9525.9874;
return 0;
}
setprecision() Manipulator
To control the number of significant digits (or precision) of
the output, i.e., the total number of digits before and after
the decimal point.
int main()
{
double x = 15.674;
double y = 235.73;
double z = 9525.9874;
int main()
{
double x = 156.74, y = 235.765, z = 9525.9874;
return 0;
}
In-Class Exercise
What is the output of the following program:
#include <iostream>
#include <iomanip>
using namespace std;
int main( )
{ double val = 10.345;
cout << setprecision(5) << val << endl; //(a)
cout << setprecision(4) << val << endl; //(b)
cout << setprecision(3) << val << endl; //(c)
cout << setprecision(2) << val << endl; //(d)
cout << setprecision(1) << val << endl; //(e)
cout << "Apa Khabar \n Semua /n" << endl; //(f)
cout << static_cast<int>(val)/2 << endl; //(g)
cout << setw(6) << val*5 << endl; //(h)
cout << showpoint << fixed << setw(8) << val << endl;//(i)
return 0;
}
Formatted Input
Introduction to Input Formatting
Can format field width for use with cin.
cin reads one less character than specified with the setw()
manipulator.
Example: Input Formatting
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int SIZE = 10;
char firstName[SIZE];
return 0;
}
Example: Problem using cin
#include <iostream>
using namespace std;
int main()
{
string name;
cout << "Enter your name: ";
cin >> name;
cout << name << endl;
return 0;
}
Input Formatting: getline()
To read an entire line of input, use getline().
#include <iostream>
using namespace std;
int main()
{
const int SIZE = 20;
char firstName[SIZE];
return 0;
}
Example 2: getline()
#include <iostream>
using namespace std;
int main()
{
string name;
cout << "Enter your name: ";
getline (cin, name);
cout << name << endl;
return 0;
}
In-Class Exercise
Write C++ program to solve the flow chart:
START
Read name
Read address1
Read address2
END
Input Formatting: get()
To read a single character, use cin.
char ch;
cout << "Strike any key to continue";
cin >> ch;
Problem: will skip over blanks, tabs, <ENTER>
START
Read name
Read address1
Read address2
END
Input Formatting: ignore()
Mixing cin >> and cin.get() in the same program can cause
input errors that are hard to detect.
int main()
{
int id;
char code;
cout << "Enter an integer id: ";
cin >> id;
cout << "Enter a code: ";
cin.get(code);
cout << "Output\n" << id << "\t" << code;
return 0;
}
Introduction to Files
File Input and Output
Can use files instead of keyboard and monitor screen for
program input and output.
Steps: (1) Open the file (2) Use the file (read from, write to,
or both) (3) Close the file.
File Operations
Requires fstream header file:
use ifstream data type for input files.
use ofstream data type for output files.
use fstream data type for both input, output files.
ifstream:
Open for input only and file cannot be written to.
Open fails if file does not exist.
ofstream:
Open for output only and file cannot be read from.
File created if no file exists.
File contents erased if file exists.
File Operations (cont.)
Can use input file object and >> to copy data from file to
variables:
infile >> partNum;
infile >> qtyInStock >> qtyOnOrder;
Can use eof() member function to test for end of input file.
Closing Files
Use the close() member function:
infile.close();
outfile.close();
int main()
{
fstream infile("input.txt", ios::in); // open the files
fstream outfile("output.txt", ios::out);
int num;
if (!input)
{
cout << "While opening a file an error is encountered" << endl;
return 0;
}
else
cout << "File is successfully opened" << endl;
while(!input.eof())
{
input.getline(str, 80);
cout << str << endl;
}
input.close();
return 0;
}
Example 5: File Operations
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
int num;
ifstream inp("input.txt"); // open the input file
ofstream out("output.txt"); // open the output file
if (!inp.is_open()) // check for successful opening
{
cout << "Input file could not be opened! Terminating!\n;
return 0;
}
while (inp >> num)
out << num * 2 << endl;
inp.close();
out.close();
cout << "Done!" << endl;
return 0;
}