String Arrays

Need a little help with understanding arrays and functions. Say I have a simple function that uses a string array to allow a user to enter his name. My issue is how do I call up that function in main. Right now im just trying to run through the function, then just display what the 1st user entered for his name. Here is what I have.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

void UserInput(string EmployeeName);


int main(){

	UserInput(EmployeeName);  //This is where my error is
	cout << EmployeeName[0];
}

void UserInput(string EmployeeName){

	int x = 0;
	string EmployeeName[4];

	for (x = 0; x < 5; x++){
		cout << "Please Enter Employee First And Last Name.  ";
		cin >> EmployeeName[x];
		cout << endl;
	}
}
the compiler doesn't know about EmployeeName at the point you use it as a variable in main(). You'd need to :

1
2
3
4
5
int main(){
  string EmployeeName;
  UserInput(EmployeeName);
  cout << EmployeeName;
}


Also you'll want to change UserInput to:
 
void UserInput(string &EmployeeName){


this will allow you to populate EmployeeName and have it return to main(). read up about local variables and scope of variables.

Last edited on
Ah ok that makes sense, I knew it was just something easy I was missing. Thanks alot
Topic archived. No new replies allowed.