In: Computer Science
C++
Write the C++ code for a void function that prompts the user to enter a name, and then stores the user's response in the string variable whose address is passed to the function. Name the function getName.
Function’s Code Screenshots:
Function’s Code To Copy:
//Define the
//function getName().
void getName(string *name)
{
//Prompt the user to
//enter the name.
cout << "Enter a name: " << endl;
//Read and store
//the name.
cin >> *name;
}
Below is the code to test the working of the function.
Code Screenshots:
Sample Output:
Code To Copy:
//Include the
//required header files.
#include <iostream>
#include <string>
//Use the standard
//namespace.
using namespace std;
//Define the
//function getName().
void getName(string *name)
{
//Prompt the user to
//enter the name.
cout << "Enter a name: " << endl;
//Read and store
//the name.
cin >> *name;
}
//Define the
//main() function.
int main()
{
//Declare a string.
string name;
//Call the
//function getName().
getName(&name);
//Display the name
//entered by the user.
cout << "The name entered "
<<"by the user is: "
<< name << endl;
//Return from the
//main() function.
return 0;
}