-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter-3
148 lines (110 loc) · 4.16 KB
/
chapter-3
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
121
122
123
124
125
126
127
128
129
130
131
132
133
Source code for first exercise:
#include "std_lib_facilities.h"
void MakeLetter() {
string letter;
cout << "Enter the recipient's name.\n";
string recipient_name = " ";
cin >> recipient_name;
letter += "Dear " + recipient_name +
",\n How are you? I am fine. I miss you.\n I am studying C++, it's great!\n";
cout << "Enter the name of a friend.\n";
string friend_name = " ";
cin >> friend_name;
cout << "Enter the gender of that friend.\n";
string friend_gender;
cin >> friend_gender;
if (friend_gender != "male" && friend_gender != "female")
simple_error("That's not a gender, you have been cancelled");
else if (friend_gender == "male")
letter += " If you see " + friend_name + " please ask him to call me.\n";
else
letter += " If you see " + friend_name + " please ask her to call me.\n";
cout << "Enter recipient's age.\n";
int age;
cin >> age;
letter += " I hear you just had your birthday and are " + to_string(age) + " years old.\n";
if (age <= 0 || age >= 110)
simple_error("You're kidding!");
else if (age < 12)
letter += " Next year you will be " + to_string(age + 1) + ".\n";
else if (age == 17)
letter += " Next year you will be able to vote.\n";
else if (age >= 70)
letter += " I hope you are enjoying retirement.\n";
letter += " Yours sincerely,\n\nMikey Musch";
cout << letter;
}
int main()
{
MakeLetter();
}
Review:
1. what is meant by the term prompt
A prompt is a function that pauses for user input and continues after receiving input
2. You read into a variable using the input opperator >>
3. two lines of code to read user input into an int variable called number:
int number = 0;
cin >> number;
4. "\n" is an escape character \ and n which together mean new line
5. a space terminates input into a string
6. any character that's not an integer terminates input into an int.
7. cout << "Hello, " << first_name << "!\n"
8. An object is an area of memory that can store a value
9. a literal is a raw value such as: "mikey", 'c' 123.4
10. There are string, int, char, pointer, float literals
11. a variable is a name object
12. a char is typically 1 byte, an int 2 or 4 bytes, and double 8 bytes
13. bytes
14. = is assignment, and == is the "equal to" comparison operator
15. a definiton is the stating of an object's name, values, and methods
16. initialization is the declaration of an object, it differs from assignment because you can initialize an object without assigning it a value
17. string concatenation is the joining of strings, it looks like "some string " + ", some other string"
18. not legal names:
correct?
the_$12_method
latest thing
this_1_is fine
2_for_1_special
19. 5 examples of names you could use but shouldn't
xy7a, lnt, String, Int
20. good rules for choosing names, make it clear and meaningful, short, not easily confusable character-wise, for example O0 I l
21. type safety is the certainty that the type you're dealing with is what you expect it to be, it's important because without type safety you can attempt to
run methods on incompatible types
22. since doubles can be larger in binary than ints, a conversion from double to int causes narrowing, which loses the extra 4 bytes a double can potentially have
23. list notation can be used to help decide if a conversion from one type to another is safe or unsafe,
if the type types are of differing sizes then narrowing will occur
Exercise 6/7:
#include "std_lib_facilities.h"
int main()
{
string val1, val2, val3;
cout << "Enter three strings\n";
cin >> val1 >> val2 >> val3;
string smallest, middle, largest;
if (val1 <= val2)
{
smallest = val1;
largest = val2;
}
else
{
smallest = val2;
largest = val1;
}
if (val3 <= smallest)
{
middle = smallest;
smallest = val3;
}
else if (val3 >= smallest && val3 <= largest)
{
middle = val3;
}
else
{
middle = largest;
largest = val3;
}
cout << smallest << ", " << middle << ", " << largest << "\n";
return 0;
}