using string with void function

simple program to enter name and display name. what am i doing wrong?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//Ch10ConE03.cpp
//Displays the name entered by the user
//Created/revised by <your name> on <current date>

#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

void getName (string);

int main()
{	
  	string name = " ";
	getName(name);
	cout << "Your name is " << name << "." << endl;
	
    return 0;
}   //end of main function

//*****function definitions*****
void getName(string inputName)
{
	cout << "Enter your name: ";
	getline(cin, inputName);
}   //end of getName function 
You need to pass inputName by reference:
1
2
3
4
void getName(string &inputName)
{
   //...
}

Topic archived. No new replies allowed.