char不希望在课堂上工作 [英] char not want work in classes

查看:80
本文介绍了char不希望在课堂上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!
也许这个问题是第五十个(对不起)并且可能会重复,但是当我找到有关我的问题的信息或示例时,我还不明白.在我的初学者学习中,关于字符型主题的主题是地狱.在简单的main中,char类型可以正常工作,但是在类中它将出错.这是我的示例,我试图证明这是不正确的.

Hello smartens!
Maybe this question is fiftieth (SORRY) and it may repeat, but when i found an information or examples about my problem, I have not understand. Topic about char type topic are hell in my beginner learning. In simple main, char type works fine, but in classes it is going to error. Here is my example and I try to show that is works incorrect.

// ppl.h
class cPeople
{
	private:
			char hisName[33];
	public:
			char getName() const { return *hisName; }
			void setName(char name) { hisName[33] = name; }
};


// main.cpp
#include <iostream>
#include "ppl.h"

int main()
{
	cPeople man;

	char myName;
        std::cout << "Please enter your name: ";
	std::cin >> myName;

	man.setName(myName);
	std::cout << "The name is: " << man.getName() << std::endl;

	return 0;
}



我输入了名字约翰".你知道吗,我在输出屏幕上得到什么?我笑了,simbol,哈哈.
有解决方案吗?



I entered name "John". Do you know, what i get in output screen? I got a smile simbol, lol.
Any solution?
Thanks for the patience.

推荐答案

您似乎对char实际上是什么感到困惑:char是单个字符. ``A''或``B''或``C''或...
hisName是一个由33个单字符对象组成的数组,因此"John"将占据这些槽"中的4个(实际上是5,因为结尾处有一个空字符来表示结尾.因此:*他的名字是单个字符,不是名称.如果您的代码有效,则getName将返回"J".
但事实并非如此.因为setName既不起作用,因为hisName [33]也是单个字符,更糟糕的是,该字符超出了分配给数组的值的范围,该数组的索引在0到32之间

更糟糕的是,您的cin始终设置为读取单个字符:"J",因为myName被声明为单个字符.

认真地讲,您需要立即回到基础知识,并从头开始阅读课程.有太多您尚未理解的概念.
You seem to be confused as to what a char actually is: char is a single character. ''A'' or ''B'' or ''C'' or ...
hisName is an array of 33 single character objects, so "John" would occupy 4 of those "slots" (in fact 5, as there is a null character on the end to signify the end. So: *hisname is a single character, not a name. If your code had worked, getName would return ''J''.
But it doesn''t. Because setName doesn''t work either as hisName[33] is also a single character, and worse, one that is outside the range of values assigned to the array in the firstplace, which will have indexes between 0 and 32

To make matters worse, your cin is set to read a single character anyway: ''J'' because myName is declared as a single character.

Seriously, you need to go right back to basics, and start reading your course from the very beginning. There are just too many concepts you haven''t understood yet.


这是一个初学者的问题.但是每个人都曾经是初学者.在C和C ++中,指针是必不可少的.而您的问题与指针有关.您应该为初学者阅读一本好书,或者为初学者寻找在线课程.

我将向您展示您的错误:
It is a beginner question. But everbody was a beginner once. In C and C++ pointers are essential. And your problem has to do with pointers. You should read a good book for beginners or look for an online lesson for beginners.

I will show you your errors:
// This is a single char variable.
// You would probably want a buffer that can hold multiple characters.
char myName;

// This will return the first character of hisName
char getName() const { return *hisName; }

// This will assign the character name to the 34th character of hisName
//  which is not allowed:
// The size of hisName is 33 chars. Accessing the 34th element (index 33)
// will corrupt your memory.
// Your intention here is to copy a string. 
// See the functions strcpy() and strncpy().
void setName(char name) { hisName[33] = name; }


这里很难提出正确的方法",因为基本上有两种方法可以解决问题.

第一个是纠正您所做的char,char *和char []之间的混淆.结果是一个不是C ++的程序,只用+ iostream类破坏了C.
我不知道为什么在2011年仍然是大多数教师使用C ++的方式...

It is difficult here to propose the "right way" since there are essentially two ways to solve the problem.

The first is correct the confusion between char, char* and char[] you did. The result is a program that''s not C++, bust just C with classes +iostream.
An I wonder why in 2011 this is still the way the most of teachers tech C++ ...

// ppl.h
#ifndef PPL_H_INCLUDED
#define PPL_H_INCLUDED
#include <cstring>

class cPeople
{
public:
    static const unsigned max_namelength = 32; //magic number defined only once
    const char* getName() const 
    { return hisName; } //< array decay into pointer
    void setName(const char* name)
    {
        //this copies all 'name' into hisName up to max_length and...
        std::strncpy(hisName,name,max_namelength);
        //...ensure the proper null-termination
        hisName[max_namelength] = '\0';
    }
private:
    char hisName[max_namelength+1]; //+1 for the terminating null char 
};

#endif // PPL_H_INCLUDED


// main.cpp
#include <iostream>
#include <iomanip>
#include "ppl.h"

int main()
{
    static const unsigned max_read_buffer = 40; //< max read chars
    cPeople man;

    char myName[max_read_buffer+1]; // +1 or the terminating null char
    std::cout << "Please enter your name: ";
    //read up to (but no more than) the buffer capacity
    std::cin >> std::setw(max_read_buffer) >> myName;

    man.setName(myName);
    std::cout << "The name is: " << man.getName() << std::endl;

    return 0;
}




另一个解决方案更一致地使用C ++工具:




The other solution uses C++ facilities more congruently:

// ppl.h
#ifndef PPL_H_INCLUDED
#define PPL_H_INCLUDED

#include <string>
#include <utility>

class cPeople
{
public:
    //will not copy on return
    const std::string& getName() const
    { return hisName; }

    //use move semantics where it can
    void setName(std::string name)
    { hisName = std::move(name); }

private:
    std::string hisName;
};

#endif // PPL_H_INCLUDED


// main.cpp
#include <iostream>
#include <string>
#include "ppl.h"

int main()
{
    cPeople man;

    std::string myName;
    std::cout << "Please enter your name: ";
    std::cin >> myName;

    man.setName(myName);
    std::cout << "The name is: " << man.getName() << std::endl;

    return 0;
}



注意,不再需要注意缓冲区的长度.内存管理和可变长度对象(如字符串)留在标准库中.



Note that there is anymore the need to take care of buffer lengths. memory management and variable length objects (like strings) are left to the standard library.


这篇关于char不希望在课堂上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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