Welcome to Lesson 14!
Learning Objectives By the end of today's class, you should know...
- What is a do-while loop?
- How does the syntax of a do-while loop compare to that of a while loop?
- When do you use a do-while loop instead of a while loop?
- When a user enters a string rather than a number, how do you handle the user error?
- What is the difference between cin.good() and cin.fail()?
- What are cin.clear() and cin.ignore(1000,'\n');?
- What is a string index?
- How to iterate through a string using indexing.
- How to use getline() to take in string input.
- What is cin >> ws and when is it needed?
Announcements - Quiz 5 today
- Midterm one week from today
- around 15 multiple choice, short answer, true/false
- one program from your homework or in-class activities
- Study old quizzes, review activities and assignments
Review Activity With a partner, complete the following:
- Write the following while loop as the equivalent for loop (output should be identical):
int age = 13; while (age <20) { cout << "Still a teen. Age: " << age << endl; age++;
}
Do-While Loops
Flowchart of do-while loop:

Image source.
- One common use of a do-while loop is to validate user input (i.e. check for errors in the user input)
- The following code shows an example where we force the user to enter a positive value
Example do-while Loop Used to Validate Input1
2
3
4
5
6
7
8
9
10
11
12
13
| #include <iostream>
using namespace std;
int main() {
double input;
do {
cout << "Enter a positive number: ";
cin >> input;
} while (input <= 0.0); // test condition at end
cout << "You entered: " << input << endl;
return 0;
}
|
When to Use do-while Statements- Use the do-while loop when you want to force a minimum of one iteration
- Note that you can accomplish the same control flow with a while loop
- However, in some cases we can save a statement by using a
do-while loop
Group Activity
With a partner, answer the following questions: - How is the syntax of a do-while loop different from a while loop?
- When would you use choose a do-while loop over a while loop?
- Change the following while loop into a do-while loop:
int sum = 0; cout << "Enter a score or 0 to quit: "; int score; cin >> score;
while (score != 0) { sum += score; cout << "Enter a score or 0 to quit: "; cin >> score; }
cout << "The sum is: " << sum << endl;
Using Loops with Strings Using Loops to Check for cin Failure cout << "The input failed" << endl;
//handle the failure
}- As
we saw when discussing uses of loops, It is good practice to use a loop
in cases like these as the user might enter invalid input more than
once.
while (cin.fail()) { cout << "The input failed" << endl;
//handle the failure
} //clear the invalid input cin.clear(); cin.ignore(1000, '\n');
// keep prompting user to enter a valid input until they get it right cout << "Please enter a number not a string: "; cin >> input; } - Note that we could also use a do-while loop as in the next example.
- Why would a do-while loop be useful here?
Example Program with Input Validation1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| #include <iostream>
using namespace std;
int main() {
double input = 0.0;
do {
cout << "Enter a positive number: ";
cin >> input;
if (cin.fail()) {
cout << "You must enter digits, not words\n";
cin.clear();
cin.ignore(1000, '\n');
input = -1; // set loop test to fail
} else if (input <= 0.0) {
cout << "You must enter a positive number\n";
}
} while (input <= 0.0);
cout << "You entered: " << input << endl;
return 0;
}
| More information on cin functions
Activity 14.1: Scores (10 pts) - In this exercise we use indefinite loops to process user input and to ensure correct user input.
- Find a partner for pair programming.
- Copy the following program into CodeBlocks, save it as scores.cpp, and then compile and run the starter program to make sure you copied it correctly.
#include <iostream>
#include <climits>
using namespace std;
int main() {
// Enter your code here
return 0;
}
- Declare two variables of type
double named sumScores and nextScore and initialize the variables to 0 . In addition, declare an integer variable named count and initialize it to 0 . The following is the psuedocode for these steps:set sumScores to 0
set nextScore to 0
set count to 1
Compile your code to make sure you declared the variables correctly. - Now
we want to use a loop to enter a series of scores. Since we do not know
how many scores to enter, we use an indefinite loop like the following:
- In addition, add a statement to display
sumScores after the loop.Compile your code to make sure you added the loop correctly. To exit the loop you will need to enter a negative number. - The loop includes statements to collect the sum of the scores in the variable named
sumScores . Add a statement after the loop to print sumScores to the console. When you run the program after adding this code, the output should look like:Enter Score#1: 38
Enter Score#2: 39
Enter Score#3: -1
Sum of scores: 77
The loop uses the sumScores variable to accumulate scores during each repetition of the loop.
- We could write our indefinite loop using a
do -while loop instead. Replace your current loop with the following:
Note
that the statements inside the loop did not change, only the loop
statement itself. To make sure you made the changes correctly, compile
and run your code and check to see if it works the same. The difference
between a while and do-while loop is that a do-while ensures the body of the loop is executed at least once.
- One problem with our program is the user can still enter letters instead of digits. We can prevent this error by checking
cin.fail() and looping until the user enters a correct value. Replace the current if statement with the following:
Strings Continued Review: Strings Versus Characters - Remember that a string is a series of characters enclosed in double quotes such as:
"Hello" "b" "3.14159" "$3.95" "My address is 378 Eastbrook Dr" - We can store text in a variable of type
string , like:string firstName; // declaration
firstName = "Jennifer"; // assignment
string lastName = "Parrish"; // declaration + assignment
string fullName = firstName + " " + lastName; // concatenation (+) of 2 stings
- On the other hand, a character is a single letter, number or special symbol
- We enclose characters in a single quote, rather than a double quote, like:
'a' 'b' 'Z' '3' 'q' '$' '*' - Also, we can store a a single character using a variable of type
char , such as:char letterA = 'A';
char letterB = 'B';
- Each character is stored as a number, using its ASCII Table value
- By declaring a
char variable or using single quotes, C++ knows to treat the number as a character - Thus, when we print a character, we see a letter rather than a number:
char letter = 'A';
cout << letter << 'B' << endl;
- As we can see, a string is made up of characters and characters are numerical codes
- We can use this information to work with characters and strings
Indexing a String- Strings have a built-in indexing system with each stored in a character sequence starting at 0 (zero)
- We can access any individual character of a
string variable using square brackets [ ] - The general syntax is:
stringVariable[index];
- What will the following print?
#include <iostream>
using namespace std;
int main() {
string sport = "Basketball";
cout << "The league: " << sport[1] << sport[7] << endl;
cout << "Abbreviation for this sport: " << sport[0] << sport[6] << endl;
return 0;
}
Using a For Loop to Iterate Strings
Group Activity: Iterating Strings
- Copy the following program into a text editor, save it as
test.cpp , and then compile and run the starter program to make sure you copied it correctly.#include <iostream>
using namespace std;
int main() {
// Enter your code here
return 0;
}
- Add the following
for -loop code to the main() function.string msg = "Hello, world!";
for (int i = 0; i < msg.length(); i++) {
cout << i << ": " << msg[i] << endl;
}
- Compile and run your code. What do you see when you compile?
String Input With Spaces
Input Using getline()- To read an entire line we use function
getline() - Syntax:
getline(cin, stringVariable);
- Where:
- stringVariable: the name of the string variable
- For example:
string line;
cout << "Enter a line of input:\n";
getline(cin, line);
cout << line << "END OF OUTPUT\n";
- Note that
getline() stops reading when it encounters a '\n'
Activity 14.2: How many words in your sentence? (10 pts)- Let's write a program that counts the number of words in a sentence input by the user.
- Find a partner and open up a new C++ file called numWords.cpp.
- Copy and paste the following starter code into your file:
#include <iostream>
using namespace std;
int main() {
//Your statements go here
return 0;
}
- Add the appropriate block comments with your name and section information.
- Next declare a string variable at the top of your program named sentence:
string sentence;
- Welcome the user to the program with the following message:
Give me a sentence, and I will count the number of words.
- Prompt the user to input a sentence and store the user input as the sentence variable.
Please enter your sentence:_
- Don't forget to use getline() here.
- How can we determine how many words are in a sentence?
- We need to look at the whitespace.
- Next, we will use a for loop to scroll through the sentence looking for blank spaces.
- Each time we encounter a new blank space, we will add one to our total for the number of words in the sentence.
- Create
a new variable at the top of main to store our counter for the number
of words in the sentence. Assign it a value of 1. Why do we want to give
it an initial value of 1 not 0?
int numWords = 1;
- Now, create a for loop to iterate through the sentence. Don't forget to use the length() function here.
for (int i = 0; i < sentence.length(); i++) {
cout << sentence[i] << endl;
}
- Now, run your program and verify that you get the following output.
- However, this is not the purpose of our program.
- We want to count the number of words.
- Remove the cout statement from your for loop.
- Repalce the cout with an if statement to check the value of the the s[i] variable to determine if it is a blank space.
if (sentence[i] == ' ')
- When we encounter a blank space, we need to add one to the numWords variable.
- Your for loop should now look like this:
for (int i = 0; i < sentence.length(); i++) { if (sentence[i] == ' ') {
numWords++; }
}
- Finally, outside of your for loop, add a statement to print out the number of words.
cout << "There are " << numWords << " words in \"" << sentence << "\"" << endl;
- Run your program to verify it works correctly. Then, upload to Catalyst.
- Your program should now look like this:
- With your partner, answer the questions from today's learning objectives
Upcoming Assignments - Assignment 14 due Tuesday at 3:20pm on Catalyst
- Midterm 2 one week from today
- No Lab on Friday - Veteran's Day Holiday!
~Have a Great Weekend!~
|