Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Bring in program while.cpp from the Lab 5 folder. (This is Sample Program 5.2 fr

ID: 3792876 • Letter: B

Question

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 <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.

Explanation / Answer

exercise 1)

1)the user don,t know how to exit from the program .the console output only show the enter the letter only.

2) the user might entered strings of letters also.this program taking sequences of characters and showing every letter separately in the output.actually it is good to see the user even though he entered multiple characters at a time also.

output

Please enter a letter
mark is a good boy
The letter you entered is m
Please enter a letter
The letter you entered is a
Please enter a letter
The letter you entered is r
Please enter a letter
The letter you entered is k
Please enter a letter
The letter you entered is i
Please enter a letter
The letter you entered is s
Please enter a letter
The letter you entered is a
Please enter a letter
The letter you entered is g
Please enter a letter
The letter you entered is o
Please enter a letter
The letter you entered is o
Please enter a letter
The letter you entered is d
Please enter a letter
The letter you entered is b
Please enter a letter
The letter you entered is o
Please enter a letter
The letter you entered is y
Please enter a letter

3)the program keep on running enter a letter and it is not flexibility to know exit the program also.the prompt of the program enter a letter .This prompting is also not sufficient and also not to enter more letters in the output.

4)every time need to display how to exit the program also.coding standards not followed.

5)need not to initialize the char letter='a' in the program.

6)exept small mistakes program running so fine.you did a good job.

2)

you did so good it is user friendly also .need not to do much more for being more user friendly

#include <iostream>
using namespace std;
int main()
{
char letter;
while (letter != 'x')
{
cout<<"to exit press character x "<<endl;  
cout << "Please enter a letter" << endl;
cin >> letter;
cout << "The letter you entered is " << letter << endl;
}
return 0;
}

3)when we use do-while instead of while definitely if program causes exception also do -while loop executed once.

if you type x also it runs once .

to exit press character x
Please enter a letter
f
The letter you entered is f
****************************************
to exit press character x
Please enter a letter
sfdg
The letter you entered is s
****************************************
to exit press character x
Please enter a letter
The letter you entered is f
****************************************
to exit press character x
Please enter a letter
The letter you entered is d
****************************************
to exit press character x
Please enter a letter
The letter you entered is g
****************************************
to exit press character x
Please enter a letter
c
The letter you entered is c
****************************************
to exit press character x
Please enter a letter
x
The letter you entered is x
****************************************

--------------------------------
Process exited after 37.2 seconds with return value 0
Press any key to continue . . .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote