Welcome to Lesson 15!By the end of today's class, you should know...
Announcements
Review Activity With a partner, answer the following review questions:
string sport = "FOOTBALL!"; cout << sport[1] << sport[sport.length()-1] << sport[2];
Wrapping Up StringsString Input With Spaces
Input Using getline()
Activity 15.1: How many words in your sentence? (10 pts)
#include <iostream> using namespace std; int main() { //Your statements go here return 0; }
string sentence;
Give me a sentence, and I will count the number of words.
Please enter your sentence:_
int numWords = 1;
for (int i = 0; i < sentence.length(); i++) { cout << sentence[i] << endl; }
if (sentence[i] == ' ')
for (int i = 0; i < sentence.length(); i++) { if (sentence[i] == ' ') { numWords++; } }
cout << "There are " << numWords << " words in \"" << sentence << "\"" << endl;
The Problem with Newlines
Example Using
|
#include <iostream> using namespace std; int main() { cout << "Enter your age: "; int age; cin >> age; cout << "Enter your full name: "; string name; |
Activity 15.2: Sentence Continued (10 pts)
- Let's
add to our program involving sentences from the last class. We will
also calculate the number of letters in the sentence and take in a user
guess for a number of letters.
- At the end of the program, we will confirm whether their guess was correct or not.
- Declare
a new variable beneath the other two at the top of the program. This
variable will be used to store our calculation for the number of
letters.
- Also, add a variable to store the user input for their guess.
- The variable declaration section of your program should now look like this:
string sentence;
int numWords = 1;
int numLetters = 0;
int guess;
- Now
alter your first cout statement to reflect the additional uses of this
program. Remove the original message in your cout statement and replace
it with the one below:
Think of a sentence in your mind.
Later I will tell you how many words and letters are in your sentence.
- Now,
ask the user to enter a guess for how many letters are in the sentence.
We want the user to guess without counting the number of letters.
Enter a guess for the number of letters in your sentence (don't count!):
- Store the user guess as the guess variable using cin.
- Next,
prompt the user to enter the sentence. Your prompt should remain the
same from the last exercise and, as before, you should use getline() to store the user input as the sentence variable.
Please enter your sentence:
- Verify that your code inside main looks like identical to the code below:
- Now, run your program. You should notice a problem.
- How can we fix this problem?
- Add a cin.ignore() statement (or you can use cin >> ws) above your getline(cin, sentence);
cin.ignore(); //or use cin >> ws
getline(cin, sentence);
- Compile and run your code again and verify that it is now working properly.
- Now let's alter the code inside the for loop to calculate how many letters are in the sentence.
- Since we don't want to count any blank spaces, we only want to increment the numLetters variable when we are NOT incrementing the numWords variable.
- Therefore, we need to add an else statement to our for loop. Make sure your if-else in the for loop looks like this:
if (sentence[i] == ' ') {
numWords++;
} else {
numLetters++;
}
- Now, let's add another cout statement below the for loop to print out the number of letters in the sentence,
cout << "And, " << numLetters << " letters.\n";
- Did our user guess the number of letters correctly? Now is the time to let him or her know. Add the following if-else block above the return 0; of main.
if (guess == numLetters) {
cout << "You guessed right!\n";
} else {
cout << "You guessed wrong!\n";
}
- Run
your program again and you should get the following output. Note: if
you don't get the output below, compare your program to the final
version at the end of this exercise. When you are finished, upload to
Canvas:
- Your final code should look identical to the following:
Activity 15.2: Sentence Continued (10 pts)
- Let's add to our program involving sentences from the last class. We will also calculate the number of letters in the sentence and take in a user guess for a number of letters.
- At the end of the program, we will confirm whether their guess was correct or not.
- Declare
a new variable beneath the other two at the top of the program. This
variable will be used to store our calculation for the number of
letters.
- Also, add a variable to store the user input for their guess.
- The variable declaration section of your program should now look like this:
string sentence;
int numWords = 1;
int numLetters = 0;
int guess;
- Now alter your first cout statement to reflect the additional uses of this program. Remove the original message in your cout statement and replace it with the one below:
Think of a sentence in your mind.
Later I will tell you how many words and letters are in your sentence.
- Now, ask the user to enter a guess for how many letters are in the sentence. We want the user to guess without counting the number of letters.
Enter a guess for the number of letters in your sentence (don't count!):
- Store the user guess as the guess variable using cin.
- Next,
prompt the user to enter the sentence. Your prompt should remain the
same from the last exercise and, as before, you should use getline() to store the user input as the sentence variable.
Please enter your sentence:
- Verify that your code inside main looks like identical to the code below:
- Now, run your program. You should notice a problem.
- How can we fix this problem?
- Add a cin.ignore() statement (or you can use cin >> ws) above your getline(cin, sentence);
cin.ignore(); //or use cin >> ws
getline(cin, sentence);
- Compile and run your code again and verify that it is now working properly.
- Now let's alter the code inside the for loop to calculate how many letters are in the sentence.
- Since we don't want to count any blank spaces, we only want to increment the numLetters variable when we are NOT incrementing the numWords variable.
- Therefore, we need to add an else statement to our for loop. Make sure your if-else in the for loop looks like this:
if (sentence[i] == ' ') {
numWords++;
} else {
numLetters++;
}
- Now, let's add another cout statement below the for loop to print out the number of letters in the sentence,
cout << "And, " << numLetters << " letters.\n";
- Did our user guess the number of letters correctly? Now is the time to let him or her know. Add the following if-else block above the return 0; of main.
if (guess == numLetters) {
cout << "You guessed right!\n";
} else {
cout << "You guessed wrong!\n";
}
- Run your program again and you should get the following output. Note: if you don't get the output below, compare your program to the final version at the end of this exercise. When you are finished, upload to Canvas:
- Your final code should look identical to the following:
Do-While Loops
- In some cases, it would be preferable to execute the body of a loop at least once before performing the loop test.
- In these cases, we can use a do-while loop.
- The structure of a do-while loop looks like this
do { //statements } while (test); // loop test
- Where:
- statements: the statements to execute the first time and repeatedly while the test remains true
- test: the test condition to evaluate
- The loop test is placed after the body and executes at the end of the loop
- The statements in the body of the loop always execute at least once
Flowchart of do-while loop:
- 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 Input
1
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
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;
int score = 0;
cout << "Enter a score or 0 to quit: ";
cin >> score;
while (score != 0)
{
sum += score;
cout << "Enter a score or 0 to quit: ";
cin >> score;
}
int score = 0;
cout << "Enter a score or 0 to quit: ";
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 for Error Checking
Using Loops to Check for cin Failure
- A problem arises if the user of a our programs enters a string when we expect a number
- For instance, in the following, we get incorrect results if the user enters "seven"
#include <iostream>
using namespace std;
int main() {
int guess;
cout << "Guessing Game Program! \nEnter a number between 1 and 10.\n";
cout << "Enter your guess: ";
cin >> guess;
cout << "You entered: " << guess << endl;
if(guess==7) {
cout << "***Correct!***\n";
} else {
cout << "***Incorrect!***\n";
}
return 0;
} - The problem is that cin cannot convert the word "seven" into the number 7
- When this happens cin fails, sets an error flag and skips the rest of the input operation.
- You have probably seen this when you have mistakenly entered the wrong content in your program.
- We can detect the failure condition using code like:
while (cin.fail()) {
// the stream failed... code to handle failure here
//Keep prompting the user to enter valid input
cout << "Please enter a number not a string: ";
cin >> input;
}
- 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.
- To clear the failed state you need to both clear the flags and the input stream, like:
cin.clear(); cin.ignore(1000, '\n');
- Using
cin.clear()
will reset the error flag but leaves the bad input in the input stream - Adding
cin.ignore(1000, '\n');
will clear all the input a user enters until the user presses an enter key - For example:
while (cin.fail()) {
//clear the invalid input
cin.clear(); //clear the error flag
cin.ignore(1000, '\n'); //ignore the last 1000 characters or until \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 Validation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <iostream> |
Activity 15.3: 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 an Eclipse project, save it as scores.cpp, and then compile and run the starter program to make sure you copied it correctly.
#include <iostream> #include <climits> //for INT_MAX variable (largest possible int) using namespace std; int main() { int sumScores = 0, nextScore = 0, count = 1; cout << "Enter a series of scores or -1 to quit\n\n"; }
- Make sure the code compiles and runs.
- 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 printsumScores
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
anddo-while
loop is that ado-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 currentif
statement with the following:
When finished, upload your scores.cpp file to Canvas.
Wrap Up
- Assignment 15 due Thursday at 9:20 on Canvas
- Lab 8 due Friday at midnight
- Quiz 6 next class
- Assignment 15 due Thursday at 9:20 on Canvas
- Lab 8 due Friday at midnight
- Quiz 6 next class