Answer to Question #181586 in C++ for Mridul

Question #181586

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


1
Expert's answer
2021-04-15T02:57:14-0400
#include <iostream>

using namespace std;

int main()

{

  string name;
  char exitOut;
  bool flag = true;
  do

  {
    cout << "Please enter your name: ";
    cin >> name;
    cout << "The name you entered is " << name << endl;
    cout << "Would you like to exit program?(write down Y or N)" << endl;
    cin >> exitOut;
    if(exitOut == 'Y')
    {
      flag = false;
    }
  }
  while(flag);
  return 0;
}


In case of using do-while loop, at least one time it will be executed(because it executes before checking the the condition


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS