-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec6.cpp
120 lines (91 loc) · 2.63 KB
/
lec6.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <cmath>
using namespace std;
#include <string>
int main(){
cout << "----------------" << endl;
int answer1;
cout << answer1 << endl;
// => 1
int answer2;
cout << answer2 << endl;
// => 195506213
// The output is unpredictable. It prints whatever value that happens to be
// in the memory location allocated for the variable.
// The program does compile. It runs and does not crash.
//=========
// Strings
//=========
string response;
// cout << response << endl;
// => '' emprt string
// string name = "Harry";
// cout << name << endl;
// => Harry
// how can we concatenate strings?
string fname = "Harry";
string lname = "Potter";
string name = fname + lname;
// cout << name << endl;
// => HarryPotter
// how can we add a space between first and last name?
// name = fname + " " + lname; //got a space
// cout << name << endl;
// => Harry Potter
// how can we take string inputs?
// cout << "Please enter your name: ";
// string fname, lname;
// cin >> fname >> lname;
// cout << "Hello! " << fname << " " << lname << endl;
// => Hello! Tom Yeh
// how can we use getline()?
// string myname;
// getline(cin, myname);
// => myname = Tom Yeh
// what is the length of a string?
// cout << myname.length() << endl;
// => 7
// how can we get a character from a string?
string myname = "Taylor Swift";
// cout << myname[0] << endl;
// => T
// cout << myname[1] << endl;
// => a
// cout << myname[20] << endl;
// => 0
// The offset 20 exceeds the length of the string. It prints a unpredictable value.
// cout << myname[10000] << endl;
// => segmentation fault!
// The offset 10000 far-exceeds the length of the string, as well as the program size. It causes a crash.
// how can we get a part of a string?
// cout << myname.substr(0, 2);
// => ? (Ta or Tay)
// how can we get 'Taylor'?
// cout << myname.substr(?, ?);
// how can we get 'Swift'?
// cout << myname.substr(?, ?);
//=========
// ASCII
//=========
char c;
c = 70;
// cout << c << endl;
// => ?
// How can we print 'A'?
// c = ?
// cout << c << endl;
// c = (70 + 256);
// cout << c << endl;
// => ?
//=========
// BOOLEAN
//=========
bool failed = false;
// cout << failed << endl;
// => ?
int x = -3;
bool isNegative = (x < 0);
// cout << isNegative << endl;
// => ?
cout << "----------------" << endl;
}