Click here to Skip to main content
15,901,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys,

I need to make a program that reads a user inputted (cin) line and stores it in a char array. easy?
cout << "Enter your name" << endl;
cin.getline(name, 50);

name's declared as an array.

This is fine... but I need it in a seperate function that returns the data to main...

5 words for this:

I hate scope.

Thank you.

what i tried:

void Names(int i, char &name);

void main(void)
char name[50];

int i=1;
while (i < 4)
		Names(int i, char &


void Names(int i, char &name)
	cout << "Please enter your full name" << endl;
	cin.getline(char name, 256, '\n');	
Updated 8-Nov-12 11:12am

If you hate scopes, why do you artificially create many more of them than you need? Every time you write int or char you define a new variable of that type, and thereby create a scope. The only places that require a type are declarations: function declarations or variable declarations.

Your problem is that C/C++ allows you to declare variables of the same name multiple times under certain conditions, although the compiler may (and should) issue a warning. However, these variables are all different, and each has it's individual scope! More to the point, each time you put a type in front of a variable name you issue a new declaration and create a new scope. At the same time you hide the previously declared variable(s) of the same name.

1. Do not ignore compiler warnings! If you didn't see any, make sure to set the warning level to the highest value (usually 4), and make a point to investigate each warning and eliminate it.

2. Add the type of a variable only once in your code: in its declaration

3. Use different names for local (or global) variables and function parameters. While it is not mandatory to do so, using the same names is just another source of confusion, and a potential source of errors.
(in this case, main has the local variables i and name, which coincide with the names you use for the function arguments of void Names(int i, char &name);. Note that these variables/arguments define different entities and therefore should use different names!)

4. The type char& describes a variable of type reference to character, i. e. a single character. Your function tries to use it as a char array though. If you wish to pass an array, one way to do it is to pass a pointer to its start, and its length, e. g.: void Names(char* name_string, size_t name_length)
(note that I used the type size_t, not int, to describe the length: this type is specifically designed for the use with (array) sizes, or just about anything related to offsets in memory. While you can use int instead, that will likely cause warnings if you try to use it with certain system functions, e. g. size_t strlen(const char*) - see item 1. above.)
Share this answer
You need to make some effort on your own first. That means you need to post code that you have written first, and we'll help you get it right. We won't do the work for you.
Homework, and why we don't do it.[^] (Compliments of JSOP)
Share this answer
Question: How many times do you need to type char in your code? Hint, you have too many of them.

Question: How many times do you need to type int in your code? Hint, you have an extra one in there somewhere.

Question: What good is the variable i in the subroutine Names? You don't use it at all.

Question: Where'd you come up with the magic number 256? Seems to be totally unrelated to anything in your code.

Socrates would be happy.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900