L E S S O N S E T 5 Loops and Files Working with the while Loop Bring in program while.cpp from the Lab 5 folder. (This is Sample Program 5.2 from the Pre-lab Reading Assignment). The code is shown below: // PLACE YOUR NAME HERE #include using namespace std; int main() { char letter = 'a'; while (letter != 'x') { cout << "Please enter a letter" << endl; cin >> letter; cout << "The letter you entered is " << letter << endl; } return 0; } Exercise 1: This program is not user friendly. Run it a few times and explain why. Exercise 2: Add to the code so that the program is more user friendly. Exercise 3: How would this code affect the execution of the program if the while loop is replaced by a do-while loop? Try it and see.
L E S S O N S E T
5 Loops and Files
Working with the while Loop
Bring in
from the Pre-lab Reading Assignment). The code is shown below:
// PLACE YOUR NAME HERE
#include <iostream>
using namespace std;
int main()
{
char letter = 'a';
while (letter != 'x')
{
cout << "Please enter a letter" << endl;
cin >> letter;
cout << "The letter you entered is " << letter << endl;
}
return 0;
}
Exercise 1: This program is not user friendly. Run it a few times and explain
why.
Exercise 2: Add to the code so that the program is more user friendly.
Exercise 3: How would this code affect the execution of the program if the
while loop is replaced by a do-while loop? Try it and see.
Step by step
Solved in 3 steps with 5 images