






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







College of Engineering and Architecture ITE 295 Computer Programming Laboratory Manual Activity No. 4 Basic Input/ Output ● Objectives : 1. Recognize the basic input/output statement;
College of Engineering and Architecture environment setups, they actually do the exact same thing: they print on screen, although they can also be individually redirected. Standard output (cout) On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. For formatted output operations, cout is used together with the insertion operator , which is written as << (i.e., two "less than" signs). 1 2 3 cout << "Output sentence"; // prints Output sentence on screen cout << 120; // prints number 120 on screen cout << x; // prints the value of x on screen The << operator inserts the data that follows it into the stream that precedes it. In the examples above, it inserted the literal string Output sentence, the number 120, and the value of variable x into the standard output stream cout. Notice that the sentence in the first statement is enclosed in double quotes (") because it is a string literal, while in the last one, x is not. The double quoting is what makes the difference; when the text is enclosed between them, the text is printed literally; when they are not, the text is interpreted as the identifier of a variable, and its value is printed instead. For example, these two sentences have very different results: 1 2 cout << "Hello"; // prints Hello cout << Hello; // prints the content of variable Hello Multiple insertion operations (<<) may be chained in a single statement: cout << "This " << " is a " << "single C++ statement"; This last statement would print the text This is a single C++ statement. Chaining insertions is especially useful to mix literals and variables in a single statement: cout << "I am " << age << " years old and my zipcode is " << zipcode; Assuming the age variable contains the value 24 and the zipcode variable
College of Engineering and Architecture flushed, which means that the output is requested to be physically written to the device, if it wasn't already. This affects mainly fully buffered streams, and cout is (generally) not a fully buffered stream. Still, it is generally a good idea to use endl only when flushing the stream would be a feature and '\n' when it would not. Bear in mind that a flushing operation incurs a certain overhead, and on some devices it may produce a delay. Standard input (cin) In most program environments, the standard input by default is the keyboard, and the C++ stream object defined to access it is cin. For formatted input operations, cin is used together with the extraction operator, which is written as >> (i.e., two "greater than" signs). This operator is then followed by the variable where the extracted data is stored. For example: 1 2 int age; cin >> age; The first statement declares a variable of type int called age, and the second extracts from cin a value to be stored in it. This operation makes the program wait for input from cin; generally, this means that the program will wait for the user to enter some sequence with the keyboard. In this case, note that the characters introduced using the keyboard are only transmitted to the program when the ENTER (or RETURN) key is pressed. Once the statement with the extraction operation on cin is reached, the program will wait for as long as needed until some input is introduced. The extraction operation on cin uses the type of the variable after the >> operator to determine how it interprets the characters read from the input; if it is an integer, the format expected is a series of digits, if a string a sequence of characters, etc. ● Procedure :
College of Engineering and Architecture
As you can see, extracting from cin seems to make the task of getting input from the standard input pretty simple and straightforward. But this method also has a big drawback. What happens in the example above if the user enters something else that cannot be interpreted as an integer? Well, in this case, the extraction operation fails. And this, by default, lets the program continue without setting a value for variable i, producing undetermined results if the value of i is used later. This is very poor program behavior. Most programs are expected to behave in an expected manner no matter what the user types, handling invalid values appropriately. Only very simple programs should rely on values extracted directly from cin without further checking. A little later we will see how stringstreams can be used to have better control over user input. Extractions on cin can also be chained to request more than one datum in a single statement: cin >> a >> b;
College of Engineering and Architecture
stringstream The standard header
College of Engineering and Architecture Then, the third line uses this variable to extract from a stringstream constructed from the string. This piece of code stores the numerical value 1204 in the variable called myint.
**College of Engineering and Architecture
● Rubric** 1 2 3 4 Adequate Time Spent on Activity Student spent too much time and/or too little time on entire computer lab activity. Student spent too much time and/or too little time on parts of computer lab activity. Student spent an adequate amount of time on computer lab activity to ensure good results. Student spent an adequate amount of time on computer lab activity to ensure the best results. Effort Student put little to no effort towards computer lab activity Student put little effort towards computer lab activity. Student put a good amount of effort towards computer lab activity. Student put a great deal of effort towards computer lab activity. Completion of Task Student completed less than 1/2 of the computer lab activity by the due date. Student completed about 1/2 of the computer lab activity by the due date. Student completed about 80% of the computer lab activity by the due date. Student completed all of the computer lab activity by the due date. Reasonable Response and Information Responses and information given are entirely unreasonable throughout the activity. Responses and information given are unreasonable in some areas of the activity. Responses and information given are reasonable throughout most of the activity. Responses and information given are very reasonable throughout all of the activity. Neatness, Readability, and Legibility Responses and information given are entirely unreadable and illegible throughout the activity. Responses and information given are unreadable and illegible throughout most of the activity. Responses and information given are neat, readable, and legible throughout most of the activity. Responses and information given are very neat, readable, and legible throughout all of the activity. Instructor: (^) Percentage (^) Total Name: Subject Code
College of Engineering and Architecture