范围的char数组问题 [英] char array issue with scope

查看:55
本文介绍了范围的char数组问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要编写一个程序来读取用户输入的(cin)行并将其存储在char数组中.容易吗?

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);


名称声明为数组.

很好...但是我需要一个单独的函数将数据返回到主函数.

这是5个字:

我讨厌范围.

谢谢.

我试过的:


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');	
}

推荐答案

如果您讨厌范围,为什么为什么要人为地创建超出您需要的范围呢?每次编写intchar时,都定义该类型的新变量,从而创建一个作用域.唯一需要类型的地方是声明:函数声明或变量声明.

您的问题是C/C ++允许您在某些条件下多次声明相同名称的变量,尽管编译器可能(并且应该)发出警告.但是,这些变量都是不同的,并且每个变量都有其各自的作用域!更重要的是,每次将类型放在变量名之前,都会发出新的声明并创建新的作用域.同时隐藏先前声明的同名变量.

提示:
1.不要忽略编译器警告!如果您没有看到任何警告,请确保将警告级别设置为最高值(通常为4),并着重调查每个警告并将其消除.

2.仅在代码中一次添加变量的类型:在变量的声明中

3.对本地(或全局)变量和函数参数使用不同的名称.虽然不是强制性的,但是使用相同的名称只是造成混乱的另一个原因,并且是潜在的错误来源.
(在这种情况下,main具有局部变量iname,它们与您在void Names(int i, char &name);的函数参数中使用的名称一致.请注意,这些变量/参数定义了不同的实体,因此应使用不同的名称! )

4.类型char&描述类型为对字符的引用,即i. e.一个字符.您的函数尝试将其用作char数组.如果您希望传递一个数组,执行此操作的一种方法是将指针传递给它的开始及其长度(例如e). g.:void Names(char* name_string, size_t name_length)
(请注意,我使用类型size_t而不是int来描述长度:该类型是专门为与(数组)大小或几乎所有与内存偏移有关的使用而设计的.虽然可以使用int,相反,如果您尝试将其与某些系统功能一起使用,则可能会引起警告,例如 size_t strlen(const char*) -参见上面的项目1.)
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.)


您需要自己先做些努力.这意味着您需要发布您首先编写的代码,我们将帮助您正确编写代码.我们不会为您完成工作.
家庭作业,以及为什么我们不这样做. [
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)


问题:您需要键入多少次您的代码中的char?提示,您有太多.

问题:您需要在代码中键入int多少次?提示,您在某处还有一个.

问题:子例程Names中的变量i有什么用?您根本不使用它.

问题:您在哪里想出了魔术数字256?似乎与代码中的任何内容都不相关.

苏格拉底会很高兴.
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.


这篇关于范围的char数组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆