Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Homework NoEffort
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 &
		i++;
	}
 
	fout.close();
}
 
void Names(int i, char &name)
{
	cout << "Please enter your full name" << endl;
	cin.getline(char name, 256, '\n');	
}
Posted 8-Nov-12 9:17am
Edited 8-Nov-12 12:12pm
v6
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
 
Tips:
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.)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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

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

  Print Answers RSS
0 OriginalGriff 495
1 Maciej Los 340
2 Richard MacCutchan 265
3 BillWoodruff 225
4 Mathew Soji 200
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411028.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100