PowerPtChangeToWebPage

Download Report

Transcript PowerPtChangeToWebPage

Section 3.4 day 1
Using string type variables
The ASCII Character Set
0
1
2
3
4
5
6
7
8
9
10
11
12
0
NUL
LF
DC4
RS
(
2
<
F
P
Z
d
n
x
1
SOH
VT
NAK
US
)
3
=
G
Q
[
e
o
y
2
STX
FF
SYN
SP
*
4
>
H
R
\
f
p
z
3
ETX
CR
ETB
!
+
5
?
I
S
]
g
q
{
4
EOT
SO
CAN
"
,
6
@
J
T
^
h
r
|
5
ENQ
SI
EM
#
7
A
K
U
_
i
s
}
41 = ')'
6
ACK
DLE
SUB
$
.
8
B
L
V
'
j
t
~
7
BEL
DC1
ESC
%
/
9
C
M
W
a
k
u
DEL
8
BS
DC2
FS
&
0
:
D
N
X
b
l
v
9
HT
DC3
GS
`
1
;
E
O
Y
c
m
w
Using string: Input and Output
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter your name: ";
cin >> name;
cout << "Your name is " << name << endl;
return 0;
}>> reads a word up to a space or newline character
Type the above code into a new project. The next slide will
tell us what else we need to do before compilation.
Using string: getline
#include <iostream>
#include <string>
int main()
{
string name;
cout << "Enter your full name: ";
getline(cin, name);
cout << "Your full name is " << name << endl;
return 0;
}
getline reads an entire line, including blanks, up to
a newline character
Using >> Before getline
#include<iostream>
Do you see any problems
#include<string>
// program segment belowwith this segment of code?
string name;
int age;
Type and run a program
that includes this segment.
cout << "Enter your age: ";
cin >> age;
cout << "Enter your full name: ";
getline(cin, name);
cout << "Your age is " << age << endl;
cout << "Your full name is " << name << endl;
getline encounters the trailing newline after the age,
stores an empty string in name, and does not wait for user input
Reverse the order of the inputs, with
getline being called before >>.
apstring name;
int age;
cout<<"Enter your full name:";
getline(cin,name);
cout<<"Enter your age:";
cin>>age;
cout << name << “,you are ”<<age<<endl;
Or use cin.ignore(); before getline
#include <iostream>
#include <string>
string name;
int age;
cout << "Enter your age: ";
cin >> age;
cin.ignore();
// consume \n
cout << "Enter your full name: ";
getline(cin, name);
cout << "Your age is " << age << endl;
cout << "Your full name is " << name << endl;
Memory for String Variables
 When a string variable is declared, no
memory is initially provided.
 When a value is assigned to a string variable,
the computer automatically adjusts the
memory to accommodate the number of
characters to be stored.
apstring Member Functions
apstring Member Function
What It Does
int length()
Returns the number
of characters in the
string.
int find(<a string>)
Returns the starting
position of the first
occurrence of a string
or -1 if the string
does not exist.
int find(<a character>)
Returns the starting
position of the first
occurrence of a
character or -1 if the
character does not
exist.
apstring substr(<position>,
Returns a substring
<length>)
of length
Example Use
apstring s = "Hello there";
cout << s.length();
// Displays 11
apstring s = "Hello there";
cout << s.find("there");
// Displays 6
apstring s = "Hello there";
cout << s.find('H');
// Displays 0
apstring s = "Hello there";
cout << s.substr(3, 2);
// Displays "lo"
Group Activity
Write a program that will prompt the user for the name
of the school and save it into a single variable of the
string data type.
Your program will output the users school name and the
string length as you see below:
Your school name is NA Intermediate High School.
There are 27 characters in this name.
(including space characters)
Raise your hand when your program functions properly.