Professional Documents
Culture Documents
Mahmoud Abou El-Mag Soliman: Department of Computer Science
Mahmoud Abou El-Mag Soliman: Department of Computer Science
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
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
}
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
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.unsetf(ios::left);
Alice Wonderland
string myString;
getline(cin, myString);
ifstream inData;
ofstream outData;
fileStreamVariable.open(sourceName);
inData.open("h:\\prog.dat");
inData >> payRate;
inData.close();
outData.close();
outData.open("firstProg.out", ios::app);