0% found this document useful (0 votes)
102 views2 pages

Riphah International University Faculty of Computing Programming Fundamentals Lab 3: Taking Input From Users

This document discusses taking input from users in C++ programs. It explains that the cin stream object is used to access keyboard input and the extraction (>>) operator is used to extract input into variables. For example, cin >> variable; waits for user input and stores it in the variable. If invalid input is entered, extraction fails silently. The document provides an example program that gets integer input and outputs the double. It notes chained extractions can request multiple inputs at once. Finally, it lists 4 lab tasks involving getting numeric input and performing calculations.

Uploaded by

Ayaan Editor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views2 pages

Riphah International University Faculty of Computing Programming Fundamentals Lab 3: Taking Input From Users

This document discusses taking input from users in C++ programs. It explains that the cin stream object is used to access keyboard input and the extraction (>>) operator is used to extract input into variables. For example, cin >> variable; waits for user input and stores it in the variable. If invalid input is entered, extraction fails silently. The document provides an example program that gets integer input and outputs the double. It notes chained extractions can request multiple inputs at once. Finally, it lists 4 lab tasks involving getting numeric input and performing calculations.

Uploaded by

Ayaan Editor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Riphah International University Faculty of Computing

Programming Fundamentals Lab 3: Taking Input from users


Objective:
Learn basic data types in C++.
Learn how to get the input from the user.

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 int age;
2 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.

1 // i/o example Please enter an integer value: 702


2 The value you entered is 702 and its
3 #include <iostream> double is 1404.
4 using namespace std;
5
6 int main ()
7 {
8 int i;
9 cout << "Please enter an integer
10 value: ";
11 cin >> i;
12 cout << "The value you entered is " <<
13 i;
14 cout << " and its double is " << i*2
< ".\n";
return 0;
}

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.
Programming Fundamentals Riphah International University
Lab 3: Inputs from users Faculty of Computing
2

Extractions on cin can also be chained to request more than one datum in a single statement:

cin >> a >> b;

This is equivalent to:

1 cin >> a;
2 cin >> b;

In both cases, the user is expected to introduce two values, one for variable a, and another for variable b.
Any kind of space is used to separate two consecutive input operations; this may either be a space, a tab,
or a new-line character.

Lab Tasks:
1. Write a program which asks the user to enter the length and width of the
rectangle. It calculates the rectangle`s area and display the result on the screen.

2. Write a program which asks the user to enter the numerator and denominator of
a fraction and it displays the decimal values.

3. Write a program that asks the user to enter three values the program then
calculates the average of the numbers and display it on the screen.

4. There are three seating categories in the stadium. For a softball game, Class A
seats cost Rs. 15, class B seats cost Rs. 12 and class C seats cost Rs. 9. Write a
program that asks the user that how many tickets for each class of seats were
sold, then displays the amount of income generated from each ticket sales.

You might also like