0% found this document useful (0 votes)
54 views

Lab 4 Strings & Formatted Output: 4.1 Objectives

This document discusses strings and formatted output in C++. It covers: 1) String operations like length, concatenation, and substring extraction. It also discusses string input/output. 2) Arithmetic operators and expression evaluation order based on operator precedence. 3) The setw and setprecision manipulators for controlling floating point number formatting and field width when outputting values.

Uploaded by

hazyhazy9977
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)
54 views

Lab 4 Strings & Formatted Output: 4.1 Objectives

This document discusses strings and formatted output in C++. It covers: 1) String operations like length, concatenation, and substring extraction. It also discusses string input/output. 2) Arithmetic operators and expression evaluation order based on operator precedence. 3) The setw and setprecision manipulators for controlling floating point number formatting and field width when outputting values.

Uploaded by

hazyhazy9977
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/ 4

LAB 4

STRINGS & FORMATTED OUTPUT


4.1 OBJECTIVES

- To practice arithmetic operators and expression evaluation


- To get familiar with strings and exercise its basic functions
- To know the usage of setw and setprecision manipulators

4.2 PRE-LAB READING

4.2.1 ARITHMETIC OPERATORS & EXPRESSION EVALUATION

The following are some arithmetic operators used to perform different operations on
variables/constants. Most of them are easy to understand and use.

Operator Symbol Operation


+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
Integer Division

When we divide two integers, of course the result of this division will be an integer. This is because the
decimal part is truncated or “chopped” from the number. Thus 9/5 will give the answer 1 not 1.8. For
this reason there are two division operations for integer numbers. The modulus operator, (%) used only
with integers, gives the remainder of a division operation.
Therefore, 9 / 5 gives 1 while 9 % 5 gives 4 (the remainder of the division).
Activity:
Observe the output of following:

int numerator=9, denominator=7, Q,R;


Q = numerator/denominator;
R = numerator% denominator;
cout<<”I am dividing ”<<numerator<<” by ”<<denominator<<endl<<endl;
cout<<”Quotient of division is: ”<<Q<<endl;
cout<<”Remainder of division is: ”<<R<<endl;
C++ EXPRESSIONS
We know that an assignment statement consists of two parts: a variable on the left and a value to be
assigned on the right. The right side may have an expression as well. An expression in C++ is a
combination of variables, constants and various operators. For example:
circumference = 2*PI*radius ;
diff = 105 - 78 ;
ans = 8 * 4/2 + 9 - 4/2 + 6 * (4+3);

Expressions are evaluated (i.e. simplified and converted to one value) and the result is placed in the
memory location corresponding to the variable on the left. Let’s consider the last expression. Can you
tell in which order it would be evaluated?
The answer depends on the precedence of the operators used in this expression.

Precedence of Arithmetic Operators

Parenthesis
High to low

Unary operators (e.g increment/decrement operator)


Multiplication, division, modulus
Addition, subtraction

4.2.2 STRING: ANOTHER DATA TYPE


As char is used to store any single character, similarly there is another data type used to store sequence
of characters. You can define variables that hold strings.
string name = "Harry";
The string type is a part of the C++ standard. To use it, simply include the header file, <string>:
#include <string>
Unlike number variables, string variables are guaranteed to be initialized even if you do not supply an
initial value. By default, a string variable is set to an empty string: a string containing no characters. An
empty string literal is written as "". Both the following statements have same effect.
string response;
string response = "";

CONCATENATION
The “+” operator is used to concatenate two strings. For example,
string fname = "Harry";
string lname = "Morgan";
string name = fname + lname;
results in the string
"HarryMorgan"
What if you’d like the first and last name separated by a space? No problem:
string name = fname + " " + lname;
Now the result is
"Harry Morgan"

STRING INPUT

You can read a string from the console:

cout << "Please enter your name: ";


string name;
cin >> name;
When a string is read with >> (the extraction operator), only one word is placed into the string variable.
For example, suppose the user types:
“Harry Morgan”
as the response to the prompt. This input consists of two words. After the call cin >>name, the string
"Harry" is placed into the variable name. Use another input statement to read the second word.

Operation Description
name.length() Computes the length of string

name.substr(start,length) Extracts a sub string of given


length from mentioned position

name.substr(start) Extracts a sub string from the


mentioned position till the end of
string
Table 1 String Functions

Activity:
Store “Hello world!” in a string variable and perform the above mentioned operations on it.

4.2.3 FORMATTED OUTPUT:

When you print the result of a computation, you often want some control over its appearance. For
example, when you print an amount in dollars and cents, you usually want it to be rounded to two
significant digits. That is, you want the output to look like
Price per ounce: 0.04
instead of
Price per ounce: 0.0409722
The following command instructs cout to use two digits after the decimal point for all floating-point
numbers:
cout << fixed << setprecision(2);
This command does not produce any output; it just manipulates cout so that it will change the output
format. The values fixed and setprecision are called manipulators.

setw is another manipulator used to set the width of the next output field. The width is the total
number of characters used for showing the value, including digits, the decimal point, and spaces.
Controlling the width is important when you want columns of numbers to line up. For example, if you
want a number to be printed in a column that is eight characters wide, you use
cout << setw(8) << price_per_ounce;
This command prints the value price_per_ounce in a field of width 8, for example

Note: To use manipulators, you must include the <iomanip> header in your program:
#include <iomanip>

You might also like