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 0

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:

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. For more information see lesson: 6.1.6: do-while Statements.

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:

Note that the second if statement is the same as the current if statement. Compile and run your program to make sure you added the changes correctly. Try entering letters instead of digits and verify you see an error message. For more information see section: 6.1.7: Checking for cin Failure.

Save your final program source code to submit to Canvas as part of assignment 6.

In this exercise we write code to find words in a text file. Compile and test after each step to verify your work.

Specifications

Copy the following program into a text editor, save it to the home folder of Cygwin or your Terminal window as findword.cpp, and then compile and run the starter program to make sure you copied it correctly.