Welcome to Lesson 15!


Learning Objectives

By the end of today's class, you should know...
  • What is a string index?
  • How do you "iterate" through the characters in a string using a for loop and string indexing?
  • How do you use getline() to take in string input.
  • What is cin.ignore() and when is it needed?
    • Alternately cin >> ws
  • What is a do-while loop and how does it differ from a while loop?
  • How do you write a do-while loop (what is unique about its syntax)?
  • How do you use loops to do error checking for the case when the user inputs a string instead of numerical input?
    • cin.fail()
    • cin.clear()
    • cin.ignore(1000, '\n')

Announcements

  • Last day to drop with a 'W' is this Friday
    • Please check your current grade on Canvas
    • Better to drop with a 'W' than to get an 'F' in most cases
    • Please feel free to talk to me
  • Return Quiz 5
  • Quiz 6 next class


Review Activity
With a partner, answer the following review questions:
  • What will the following print to the console?
string sport = "FOOTBALL!";
cout << sport[1] << sport[sport.length()-1] << sport[2];
  • Write a for loop to print out the above string to the console with each character on its own line.


Wrapping Up Strings

String Input With Spaces

  • We have been using the >> operator to enter data into a string variable:
    string something;
    cout << "Enter something: ";
    cin >> something;
    cout << "You entered: " << something << "END OF OUTPUT\n";
    
  • However, there are some complications
  • >> skips whitespace and stops on encountering more whitespace
  • Thus, we only get a single word for each input variable
  • If a user types in "Hello Mom!", we would only read "Hello" and not " Mom!"
  • This is because cin >> s1 works as follows:
    1. Skips whitespace
    2. Reads non-whitespace characters into the variable
    3. Stops reading when whitespace is found

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 15.1: 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 Canvas.
  • Your program should now look like this:




The Problem with Newlines

  • When you press the Enter key, a newline character ('\n') is inserted as part of the input
  • The newline character can cause problems when you mix cin >> with getline()
  • Recall that cin >> s1:
    1. Skips whitespace
    2. Reads non-whitespace characters into the variable
    3. Stops reading when whitespace is found
  • Since whitespace includes newline characters, using cin >> will leave a newline character in the input stream
  • However, getline() just stops reading when it first finds a newline character
  • This can lead to mysterious results in code like the following:
    cout << "Enter your age: ";
    int age;
    cin >> age;
    cout << "Enter your full name: ";
    string name;
    getline(cin, name);
    cout << "Your age: " << age << endl
         << "Your full name: " << name << endl; 
  • There are two options to resolve this problem:
  1. Use cin.ignore(); //ignore the '\n' in the input buffer
  2. Use cin >> ws; //clear whitespace from the input buffer

Example Using cin.ignore()


#include <iostream>
using namespace std;

int main() {
    cout << "Enter your age: ";
    int age;
    cin >> age;
    cout << "Enter your full name: ";
    string name;
    cin.ignore(); // clear whitespace from buffer
    //alternately use cin >> ws;
    getline(cin, name); cout << "Your age: " << age << endl << "Your full name: " << name << endl; }

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:

Flowchart of working of do...while loop in C programming.

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 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 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;
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
22
23
24
25
26
27
28

#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;

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 >> guess;
}

cout << "You entered: " << guess << endl;
if(guess==7) {
cout << "***Correct!***\n";
} else {
cout << "***Incorrect!***\n";
}

return 0;
}
More information on cin functions


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 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:
  • When finished, upload your scores.cpp file to Canvas.

Wrap Up

  • With a partner, answer the questions from today's learning objectives


Upcoming Assignments

  • Assignment 15 due Thursday at 9:20 on Canvas
  • Lab 8 due Friday at midnight
  • Quiz 6 next class