Question #181622

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


Expert's answer

//for user friendly we can enter strings of letter this shows program taking sequence of characters and //showing every letter seperately.

#include<iostream>


using namespace std; 


int main() 


{ 


string letter = "A"; 


while (letter != "x") 


{ cout << "Please enter a letter" << endl;

 cin >> letter; 


cout << "The letter you entered is " << letter << endl; 


} 


return 0;


} 

//Using do-while loop

#include<iostream>


using namespace std; 


int main() 


{ 


string letter = "A"; 

do{

     cout << "Please enter a letter" << endl;

 cin >> letter; 


cout << "The letter you entered is " << letter << endl; 


}


while (letter != "x") ;



return 0;


} 


LATEST TUTORIALS
APPROVED BY CLIENTS