Docsity

C++ Programming: Basic Input/Output Lab Activity - Prof. Joey, Study notes of Computer Science

Toggle sidebar
bg1
PHINMA – Cagayan de Oro College
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;
2. Make a program using cin and cout.
●Materials: Computer & C/C++ programming language software tool
Basic Input/Output
The example programs of the previous sections provided little interaction with the
user, if any at all. They simply printed simple values on screen, but the standard
library provides many additional ways to interact with the user via its input/output
features. This section will present a short introduction to some of the most useful.
C++ uses a convenient abstraction calledstreamsto perform input and output
operations in sequential media such as the screen, the keyboard or a file.
Astreamis an entity where a program can either insert or extract characters
to/from. There is no need to know details about the media associated to the stream
or any of its internal specifications. All we need to know is that streams are a
source/destination of characters and that these characters are provided/accepted
sequentially (i.e., one after another).
The standard library defines a handful of stream objects that can be used to access
what are considered the standard sources and destinations of characters by the
environment where the program runs:
strea
mdescription
cin standard input stream
cout standard output stream
cerr standard error (output) stream
clog standard logging (output)
stream
We are going to see in more detail onlycoutandcin(the standard output and
input streams); cerrandclogare also output streams, so they essentially work
likecout, with the only difference being that they identify streams for specific
purposes: error messages and logging; which, in many cases, in most
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download C++ Programming: Basic Input/Output Lab Activity - Prof. Joey and more Study notes Computer Science in PDF only on Docsity!

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;

  1. Make a program using cin and cout. ● Materials : Computer & C/C++ programming language software tool Basic Input/Output The example programs of the previous sections provided little interaction with the user, if any at all. They simply printed simple values on screen, but the standard library provides many additional ways to interact with the user via its input/output features. This section will present a short introduction to some of the most useful. C++ uses a convenient abstraction called streams to perform input and output operations in sequential media such as the screen, the keyboard or a file. A stream is an entity where a program can either insert or extract characters to/from. There is no need to know details about the media associated to the stream or any of its internal specifications. All we need to know is that streams are a source/destination of characters and that these characters are provided/accepted sequentially (i.e., one after another). The standard library defines a handful of stream objects that can be used to access what are considered the standard sources and destinations of characters by the environment where the program runs: strea m description cin standard input stream cout standard output stream cerr standard error (output) stream clog standard logging (output) stream We are going to see in more detail only cout and cin (the standard output and input streams); cerr and clog are also output streams, so they essentially work like cout, with the only difference being that they identify streams for specific purposes: error messages and logging; which, in many cases, in most

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 :

  1. Let's have a look at the following code below
  2. Run the program. Assume i = 720. Place the output on the space provided output here

College of Engineering and Architecture

  1. Write your observations on the output or how and why you arrive that output.






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

  1. Write your observations on the output or how and why you arrive that output.






stringstream The standard header defines a type called stringstream that allows a string to be treated as a stream, and thus allowing extraction or insertion operations from/to strings in the same way as they are performed on cin and cout. This feature is most useful to convert strings to numerical values and vice versa. For example, in order to extract an integer from a string we can write: 1 2 3 string mystr ("1204"); int myint; stringstream(mystr) >> myint; This declares a string with initialized to a value of "1204", and a variable of type int.

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.

  1. Let’s try this example and place your output on the space provided Assume the following: Enter price: 22. Enter quantity: 8
  2. Write your observations on the output or how and why you arrive that output.




**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