Welcome to Lesson 8!By the end of today's class, you should know...
Announcements
Review Activity With a partner, answer the following questions: 1. Write one line of code to declare a String variable (name of your choice) and assign it the value $. 2. Write one line of code to declare a char variable (name of your choice) and assign it the value of $. 3. What does the following output to the console: string info = "Why hello, I'm Emily!"; cout << info.length() << ": " << info.substr(1,1) << info.substr(11, 1) << info.substr(14,3); 4. Write an if statement to check if the value stored in the variable age is equal to 5. If so, print out the age. Using |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| #include <iostream>
using namespace std;
int main() {
int guess = 0;
cout << "I'm thinking of a number between"
<< " 1 and 10.\nCan you guess it?\n\n"
<< "Enter your guess: ";
cin >> guess;
if (7 == guess) {
cout << "*** Correct! ***\n";
} else {
cout << "Sorry, that is not correct.\n";
cout << "Try again.\n";
}
return 0;
}
|
- Open up your program from the prior activity: Is Your Number Even?
- Now add an else clause to display when the number is odd.
- When your program is able to display both options (even and odd), submit it to Canvas.
- Both partners need to submit for full credit.
Comparing Characters and Strings
- Character data can be evaluated using relational operators as well
- Comparing characters works because C++ stores characters as numbers using ASCII codes
- Note that letters nearer to the start of the alphabet have lower numerical values
- Thus a numerical comparison can decide the alphabetical order of characters
Example Program Comparing Characters
1
2
3
4
5
6
7
8
9
10
11
12
| #include<iostream>
using namespace std;
int main() {
cout << boolalpha; // output true or false
cout << "'A' < 'B': " << ('A' < 'B') << endl;
cout << "'A' > 'B': " << ('A' > 'B') << endl;
cout << "'A' <= 'Z': " << ('A' <= 'Z') << endl;
cout << "'X' >= 'Y': " << ('X' >= 'Y') << endl;
cout << "'X' == 'X': " << ('X' == 'X') << endl;
cout << "'X' != 'X': " << ('X' != 'X') << endl;
}
|
Comparing Strings
- We can compare strings using relational operators as well
- C++ compares two strings using lexicographical order (a.k.a. alphabetic order)
- For example, "car" is less than "cat":
c
a
r
c
a
t
- Also, "car" is less than "card"
c
a
r
c
a
r
d
Example Program Comparing Strings
1
2
3
4
5
6
7
8
9
10
11
| #include<iostream>
using namespace std;
int main() {
string s1, s2;
cout << "Enter two strings: ";
cin >> s1 >> s2;
cout << boolalpha; // output true or false
cout << s1 << " <= " << s2 << " : " << (s1 <= s2) << endl;
cout << s1 << " > " << s2<< " : " << (s1 > s2) << endl;
}
|
Activity 8.2: Let's Alphabetize! (10 pts)
- Open up Eclipse and create a new C++ project Alphabetize with a file called alphabetize.cpp.
- Our program will take in two string inputs from the user, compare then and the output the two strings in alphabetical order.
- At the top of your program, declare a string variables called word1.
string word1;
- Now declare a second string variable called word2.
- Next, write a cout statement welcoming your user to the program and letting them know that this program will alphabetize two words.
cout << "Welcome! Give me two words and I will return them to you in alphabetical order!\n";
- Run your program to make sure it is giving you the output you expected.
- Let's prompt the user for the first word and store the result as word1.
cout << "Please enter the first word: ";
cin >> word1;
cin >> word1;
- Do the same for the second word.
- Now, let's create an if-else statement to determine the ordering of the two words. And, then output the result to our user. The if-else statement will need to use string comparison as discussed above.
- The code that you have written could be a useful part of a larger program.
- Both partners should submit to Canvas when finished.
Wrap Up
- Find a partner and answer the questions from today's learning objectives
Upcoming Assignments
- Assignment 8 due Tuesday
- Lab 4 due Friday at midnight