Download as pdf or txt
Download as pdf or txt
You are on page 1of 69

Mahmoud Abou El-Mag Soliman

Department of Computer Science


Faculty of Computers and Information
Sohag University
cin and the Extraction Operator >>
Using Predefined Functions in a Program
Example 3-4: -
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
const double PI = 3.1416;
int main()
{
double sphereRadius; //Line 1
double sphereVolume; //Line 2
double point1X, point1Y; //Line 3
double point2X, point2Y; //Line 4
double distance; //Line 5
string str; //Line 6
cout << "Line 7: Enter the radius of the sphere: "; //Line 7
cin >> sphereRadius; //Line 8
cout << endl; //Line 9
sphereVolume = (4.0 / 3) * PI * pow(sphereRadius, 3); //Line 10
cout << "Line 11: The volume of the sphere is: "
<< sphereVolume << endl << endl; //Line 11
cout << "Line 12: Enter the coordinates of two "
<< "points in the X-Y plane: "; //Line 12
cin >> point1X >> point1Y >> point2X >> point2Y; //Line 13
cout << endl; //Line 14
distance = sqrt(pow(point2X - point1X, 2)
+ pow(point2Y - point1Y, 2)); //Line 15
cout << "Line 16: The distance between the points "
<< "(" << point1X << ", " << point1Y << ") and "
<< "(" << point2X << ", " << point2Y << ") is: "
<< distance << endl << endl; //Line 16
str = "Programming with C++"; //Line 17
cout << "Line 18: The number of characters, "
<< "including blanks, in \"" << str << "\" is: "
<< str.length() << endl; //Line 18
return 0; //Line 19
}
cin and the get Function
cin and the get Function

cin.get(varChar);
cin and the get Function

cin.get(ch1);
cin.get(ch2);
cin >> num;
cin and the ignore Function

cin.ignore(intExp, chExp);

cin.ignore(100, '\n');
cin and the ignore Function
cin.ignore(100, '\n');

cin.ignore(100, 'A');
cin and the ignore Function
The putback and peek Functions
istreamVar.putback(ch);

ch = istreamVar.peek();
Example: Functions peek and putback
#include <iostream>
using namespace std;
int main()
{
char ch;
cout << "Line 1: Enter a string: "; //Line 1
cin.get(ch); //Line 2
cout << endl; //Line 3
cout << "Line 4: After first cin.get(ch); "
<< "ch = " << ch << endl; //Line 4
cin.get(ch); //Line 5
cout << "Line 6: After second cin.get(ch); "
<< "ch = " << ch << endl; //Line 6
Example: Functions peek and putback

cin.putback(ch); //Line 7
cin.get(ch); //Line 8
cout << "Line 9: After putback and then "
<< "cin.get(ch); ch = " << ch << endl; //Line 9
ch = cin.peek(); //Line 10
cout << "Line 11: After cin.peek(); ch = "
<< ch << endl; //Line 11
cin.get(ch); //Line 12
cout << "Line 13: After cin.get(ch); ch = "
<< ch << endl; //Line 13
return 0;
}
Sample Run:
Line 1: Enter a string: abcd
Line 4: After first cin.get(ch); ch = a
Line 6: After second cin.get(ch); ch = b
Line 9: After putback and then cin.get(ch); ch = b
Line 11: After cin.peek(); ch = c
Line 13: After cin.get(ch); ch = c
The Dot Notation between I/O Stream Variables and
I/O Functions
Input Failure
int a, b, c;
double x;
If the input is:
W 54
then the statement:
cin >> a >> b;
would result in an input failure, because you are trying to
input the character 'W' into the int variable a. If the input
were:
35 67.93 48

then the input statement:


cin >> a >> x > >b;
would result in storing 35 in a, 67.93 in x, and 48 in b.
Now consider the following read statement with the
previous input (the input with three values):
cin >> a >> b >> c;
Example
//Input Failure program
#include <iostream>
#include <string>
using namespace std;

int main()
{
string name; //Line 1
int age = 0; //Line 2
int weight = 0; //Line 3
double height = 0.0; //Line 4
cout << "Line 5: Enter name, age, weight, and "
<< "height: "; //Line 5
cin >> name >> age >> weight >> height; //Line 6
cout << endl; //Line 7
cout << "Line 8: Name: " << name << endl; //Line 8
cout << "Line 9: Age: " << age << endl; //Line 9
cout << "Line 10: Weight: " << weight << endl; //Line 10
cout << "Line 11: Height: " << height << endl; //Line 11
return 0; //Line 12
}
Sample Runs: In these sample runs, the user input is shaded.
Sample Run 1
Line 5: Enter name, age, weight, and height: Sam 35 q56 6.2
Line 8: Name: Sam
Line 9: Age: 35
Line 10: Weight: 0
Line 11: Height: 0
Next Run
Sample Run 2
Line 5: Enter name, age, weight, and height: Sam 35.0 156 6.2
Line 8: Name: Sam
Line 9: Age: 35
Line 10: Weight: 0
Line 11: Height: 0
The clear Function
Example
//Input failure and the clear function
#include <iostream>
#include <string>
using namespace std;

int main()
{
string name; //Line 1
int age = 0; //Line 2
int weight = 0; //Line 3
double height = 0.0; //Line 4
cout << "Line 5: Enter name, age, weight, and "
<< "height: "; //Line 5
cin >> name >> age >> weight >> height; //Line 6
cout << endl; //Line 7
cout << "Line 8: Name: " << name << endl; //Line 8
cout << "Line 9: Age: " << age << endl; //Line 9
cout << "Line 10: Weight: " << weight << endl; //Line 10
cout << "Line 11: Height: " << height << endl; //Line 11
cin.clear(); //Restore input stream; Line 12
cin.ignore(200,'\n'); //Clear the buffer; Line 13
cout << "\nLine 14: Enter name, age, weight, "
<< "and height: "; //Line 14
cin >> name >> age >> weight >> height; //Line 15
cout << endl; //Line 16
cout << "Line 17: Name: " << name << endl; //Line 17
cout << "Line 18: Age: " << age << endl; //Line 18
cout << "Line 19: Weight: " << weight << endl; //Line 19
cout << "Line 20: Height: " << height << endl; //Line 20
return 0; //Line 21
}

Sample Run: In this sample run, the user input is shaded.


Line 5: Enter name, age, weight, and height: Sam 35 q56 6.2
Line 8: Name: Sam
Line 9: Age: 35
Line 10: Weight: 0
Line 11: Height: 0
Line 14: Enter name, age, weight, and height: Sam 35 156 6.2
Line 17: Name: Sam
Line 18: Age: 35
Line 19: Weight: 156
Line 20: Height: 6.2
Output and Formatting Output
setprecision Manipulator
setprecision(n)

cout << setprecision(2);


fixed Manipulator

cout << fixed;

cout.unsetf(ios::fixed);
//Example: scientific and fixed
#include <iostream>
Example
using namespace std;

int main()
{
double hours = 35.45;
double rate = 15.00;
double tolerance = 0.01000;
cout << "hours = " << hours << ", rate = " << rate
<< ", pay = " << hours * rate
<< ", tolerance = " << tolerance << endl << endl;
cout << scientific;
cout << "Scientific notation: " << endl;
cout << "hours = " << hours << ", rate = " << rate
<< ", pay = " << hours * rate
<< ", tolerance = " << tolerance << endl << endl;
cout << fixed;
cout << "Fixed decimal notation: " << endl;
cout << "hours = " << hours << ", rate = " << rate
<< ", pay = " << hours * rate
<< ", tolerance = " << tolerance << endl << endl;
return 0;
}
Sample Run:
hours = 35.45, rate = 15, pay = 531.75, tolerance = 0.01
Scientific notation:
hours = 3.545000e+001, rate = 1.500000e+001, pay =
5.317500e+002, tolerance = 1 .000000e-002
Fixed decimal notation:
hours = 35.450000, rate = 15.000000, pay = 531.750000,
tolerance = 0.010000
showpoint Manipulator

cout << showpoint;

cout << fixed << showpoint;


Example
//Example: setprecision, fixed, showpoint
#include <iostream> //Line 1
#include <iomanip> //Line 2
using namespace std; //Line 3
const double PI = 3.14159265; //Line 4

int main() //Line 5


{ //Line 6
double radius = 12.67; //Line 7
double height = 12.00; //Line 8
cout << fixed << showpoint; //Line 9
cout << setprecision(2)
<< "Line 10: setprecision(2)" << endl; //Line 10
cout << "Line 11: radius = " << radius << endl; //Line 11
cout << "Line 12: height = " << height << endl; //Line 12
cout << "Line 13: volume = "
<< PI * radius * radius * height << endl; //Line 13
cout << "Line 14: PI = " << PI << endl << endl; //Line 14
cout << setprecision(3)
<< "Line 15: setprecision(3)" << endl; //Line 15
cout << "Line 16: radius = " << radius << endl; //Line 16
cout << "Line 17: height = " << height << endl; //Line 17
cout << "Line 18: volume = "
<< PI * radius * radius * height << endl; //Line 18
cout << "Line 19: PI = " << PI << endl << endl; //Line 19
cout << setprecision(4)
<< "Line 20: setprecision(4)" << endl; //Line 20
cout << "Line 21: radius = " << radius << endl; //Line 21
cout << "Line 22: height = " << height << endl; //Line 22
cout << "Line 23: volume = "
<< PI * radius * radius * height << endl; //Line 23
cout << "Line 24: PI = " << PI << endl << endl; //Line 24
cout << "Line 25: "
<< setprecision(3) << radius << ", "
<< setprecision(2) << height << ", "
<< setprecision(5) << PI << endl; //Line 25
return 0; //Line 26
}
Sample Run
Line 10: setprecision(2)
Line 11: radius = 12.67
Line 12: height = 12.00
Line 13: volume = 6051.80
Line 14: PI = 3.14
Line 15: setprecision(3)
Line 16: radius = 12.670
Line 17: height = 12.000
Line 18: volume = 6051.797
Line 19: PI = 3.142
Line 20: setprecision(4)
Line 21: radius = 12.6700
Line 22: height = 12.0000
Line 23: volume = 6051.7969
Line 24: PI = 3.1416
Line 25: 12.670, 12.00, 3.14159
setw

cout << setw(5) << x << endl;


Example
//Example: This example illustrates how the function
//setw works
#include <iostream> //Line 1
#include <iomanip> //Line 2
using namespace std; //Line 3

int main() //Line 4


{ //Line 5
int miles = 245; //Line 6
int speed = 55; //Line 7
double hours = 35.45; //Line 8
double error = 3.7564; //Line 9
cout << fixed << showpoint; //Line 10
cout << "123456789012345678901234567890" << endl; //Line 11
cout << setw(5) << miles << endl; //Line 12
cout << setprecision(2); //Line 13
cout << setw(5) << miles << setw(5) << speed
<< setw(6) << hours
<< setw(7) << error << endl << endl; //Line 14
cout << setw(5) << speed << setw(5) << miles
<< setw(4) << hours
<< setw(7) << error << endl << endl; //Line 15
cout << setw(2) << miles << setw(6) << hours
<< setw(7) << error << endl << endl; //Line 16
cout << setw(2) << miles
<< setw(7) << "error"
<< error << endl; //Line 17
return 0; //Line 18
} //Line 19

Sample Run:
123456789012345678901234567890
245
245 55 35.45 3.76
55 24535.45 3.76
245 35.45 3.76
245 error3.76
setfill Manipulator

ostreamVar << setfill(ch);

cout << setfill('#');


//Example: This program illustrates how the function
//setfill works.
#include <iostream> //Line 1
#include <string> //Line 2
#include <iomanip> //Line 3
using namespace std; //Line 4

int main() //Line 5


{ //Line 6
string name = "Jessica"; //Line 7
double gpa = 3.75; //Line 8
int scholarship = 7850; //Line 9
cout << "123456789012345678901234567890" << endl; //Line 10
cout << fixed << showpoint << setprecision(2); //Line 11
cout << setw(10) << name << setw(7) << gpa
<< setw(8) << scholarship << endl; //Line 12
cout << setfill('*'); //Line 13
cout << setw(10) << name << setw(7) << gpa
<< setw(8) << scholarship << endl; //Line 14
cout << setw(10) << name << setfill('#')
<< setw(7) << gpa
<< setw(8) << scholarship << endl; //Line 15
cout << setw(10) << setfill('@') << name
<< setw(7) << setfill('#') << gpa
<< setw(8) << setfill('^') << scholarship
<< endl; //Line 16
cout << setfill(' '); //Line 17
cout << setw(10) << name << setw(7) << gpa
<< setw(8) << scholarship << endl; //Line 18
return 0; //Line 19
} //Line 20
Sample Run:
123456789012345678901234567890
Jessica 3.75 7850
***Jessica***3.75****7850
***Jessica###3.75####7850
@@@Jessica###3.75^^^^7850
Jessica 3.75 7850
left and right Manipulators

ostreamVar << left;

cout << left;

ostreamVar.unsetf(ios::left);

cout << right;


Input/Output and the string Type

Alice Wonderland

cin >> name;


getline(istreamVar, strVar);

string myString;

bbbbHello there. How are you?

getline(cin, myString);

myString = " Hello there. How are you?"


Debugging: Understanding Logic Errors
and Debugging with cout Statements
#include <iostream> //Line 1
using namespace std; //Line 2

int main() //Line 3


{ //Line 4
int fahrenheit; //Line 5
int celsius; //Line 6
cout << "Enter temperature in Fahrenheit: "; //Line 7
cin >> fahrenheit; //Line 8
cout << endl; //Line 9
celsius = 5 / 9 * (fahrenheit - 32); //Line 10
cout << fahrenheit << " degree F = "
<< celsius << " degree C. " << endl; //Line 11
return 0; //Line 12
} //Line 13

Sample Run 1: In this sample run, the user input is shaded.


Enter temperature in Fahrenheit: 32
32 degree F = 0 degree C.
Sample Run 2: In this sample run, the user input is shaded.
Enter temperature in Fahrenheit: 110
110 degree F = 0 degree C.
celsius = static_cast<int> (5.0 / 9 * (fahrenheit - 32) + 0.5);
#include <iostream> //Line 1
using namespace std; //Line 2

int main() //Line 3


{ //Line 4
int fahrenheit; //Line 5
int celsius; //Line 6
cout << "Enter temperature in Fahrenheit: "; //Line 7
cin >> fahrenheit; //Line 8
cout << endl; //Line 9
celsius = static_cast<int>
(5.0 / 9 * (fahrenheit - 32) + 0.5); //Line 10
cout << fahrenheit << " degree F = "
<< celsius << " degree C. " << endl; //Line 11
return 0; //Line 12
} //Line 13

Sample Run: In this sample run, the user input is shaded.


Enter temperature in Fahrenheit: 110
110 degree F = 43 degree C.
File Input/Output
#include <fstream>

ifstream inData;
ofstream outData;
fileStreamVariable.open(sourceName);

inData.open("prog.dat"); //open the input file; Line 1


outData.open("prog.out"); //open the output file; Line 2

inData.open("h:\\prog.dat");
inData >> payRate;
inData.close();
outData.close();
outData.open("firstProg.out", ios::app);

You might also like