10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam
Standard Input and Output The process of giving something to computer from the keyboard is known as standard input. Program need instruction from the user and perform operations on that accordingly. On the other hand, the process of taking some thing from computer to the monitor is known as standard output. C++ uses a convenient abstraction called streams to perform input and output operations in sequential media such as the screen or the keyboard. A stream is an object where a program can either insert or extract characters to/from it.
Standard Input and Output The standard C++ library includes the header file iostream, where the standard input and output stream objects are declared. Therefore, this header file iostream.h must be included in all c++ programs that uses input and output statements. Program uses stream object to insert and extract data.. 10/20/2016
Standard Output (cout) By default, the standard output of a program is the screen, and the C++ stream object defined to access it is cout. cout is used in conjunction with the insertion operator, which is written as << (two "less than" signs). The << operator inserts the data that follows it into the stream preceding it. Whenever we want to use constant strings of characters we must enclose them between double quotes (") so that they can be clearly distinguished from variable names. For example, these two sentences have very different results: The insertion operator (<<) may be used more than once in a single statement:
Standard Output (cout) The utility of repeating the insertion operator (<<) is demonstrated when we want to print out a combination of variables and constants or more than one variable: It is important to notice that cout does not add a line break after its output unless we explicitly indicate it, either by : \n operator or by endl manipulator: 10/20/2016
Standard Input (cin) The standard input device is usually the keyboard. Handling the standard input in C++ is done by applying the overloaded operator of extraction (>>) on the cin stream. The operator must be followed by the variable that will store the data that is going to be extracted from the stream. For example: cin can only process the input from the keyboard once the RETURN key has been pressed. You must always consider the type of the variable that you are using as a container with cin extractions. If you request an integer you will get an integer, if you request a character you will get a character and if you request a string of characters you will get a string of characters. 10/20/2016
Standard Input (cin) // i/o example #include using namespace std; int main () { int i; cout << "Please enter an integer value: "; cin >> i; cout << "The value you entered is " << i; cout << " and its double is " << i*2 << ".\n"; return 0; } 10/20/2016
Standard Input and Output //program that display a message, integer and character variables //output.cpp #include void main() { clrscr(); int n=10; char ch='*'; cout<<"this is output.."<<endl; cout<<n<<endl; cout<<ch; getch(); }
Standard Input and Output //program to add two floating point numbers and shows the sum on screen. //add-num.cpp #include void main() { clrscr(); float var1, var2, res; var1=24.27; var2=41.50; res= var1+var2; cout<<var1<<" + "<<var2<<" = "<<res; getch(); }
Standard Input and Output //program to calculate and print the area of square with given height and width //area-square.cpp #include void main() { clrscr(); int height, width, area; height=10; width=5; area= height*width; cout<<"area of square= "<<area; getch(); }
Definitions Extraction Definition with initialization Area.cpp #include using namespace std; int main() { // Extract length and width cout << "Rectangle dimensions: "; float Length; float Width; cin >> Length >> Width; // Compute and insert the area float Area = Length * Width; cout << "Area = " << Area << " = Length " << Length << " * Width " << Width << endl; return 0; }
C++ Manipulators Manipulators are instructions to the output stream that modify the output in various ways. or manipulators are operators used with the insertion operator << to modify—or manipulate—the way data is displayed. include header file “iomanip.h” in the program when using these operators. Several types of manipulators are available in C++ such as: endl, setw(), setprecision(), fixed(), showpoint() and setfill(). We’ve already seen the endl manipulator; now we’ll look at another one: setw(), which changes the field width of output. 10/20/2016Reference source OOP by Lafore
Setw() Manipulator //Program without setw manipulator // setw-need.CPP #include int main() { long pop1= , pop2=47, pop3=9761; cout << "LOCATION " << "POP." << endl << "Portcity " << pop1 << endl << "Hightown " << pop2 << endl << "Lowville " << pop3 << endl; getch(); return 0; } 10/20/2016 // demonstrates setw manipulator //setw.CPP #include #include // heater file for setw #include int main() { long pop1= , pop2=47, pop3=9761; cout << setw(8) << "LOCATION" << setw(18) << "POPULATION" << endl << setw(8) << "Portcity" << setw(18) << pop1 << endl << setw(8) << "Hightown" << setw(18) << pop2 << endl << setw(8) << "Lowville" << setw(18) << pop3 << endl; getch(); return 0; }
Setw() Manipulator The setw manipulator causes the number (or string) that follows it in the stream to be printed within a field n characters wide, where n is the argument to setw(n). The value is right-justified within the field. 10/20/2016
setprecision() Manipulator The precision determines the maximum number of digits that shall be output on insertion operations to express floating-point values, counting both the digits before and after the decimal point. Sets the decimal precision to be used to format floating-point values on output operations. //setpre.cpp #include // Declarations of cin, cout,... #include // Manipulator setw() #include int main() { string label; double price; cout << "\nPlease enter an article label: "; cin >> setw(16); cin >> label; cout << "\n Enter the price of the article: "; cin >> price; // Input the price 10/20/2016 // Controlling output: cout << setprecision(2) << "\nArticle:" << "\n Label: " << label << "\n Price: " << price << endl; //... The program to be continued getch(); return 0; }