类的问题 [英] A problem with a classes

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

问题描述

我需要任何可以告诉我一个简单问题的人的帮助.这里有代码:

I need some help from anyone who can tell me about a simple problem.Here I have the code :

#include<iostream>
#include<string>
using namespace std;
class Person
{
private:
    string name;
public:
    int ID;
    int AGE;
    void setname(string);
    string getname();
};
void Person::setname(string thename)
{
    (name=thename);
}
string Person::getname()
{
    return name;
}
int main()
{
Person p;
string s;
cout<<"\nEnter the student name: ";
cin>>s;
p.setname(s);
cout<<"Enter the ID: ";
cin>>p.ID;
cout<<"Enter the age: ";
cin>>p.AGE;
cout<<"\nThe stduent name: "<<p.getname();
cout<<"\nThe stduent id: "<<p.ID;
cout<<"\nThe stduent age: "<<p.AGE<<endl;
system("PAUSE");
return 0;
}


因此,我想知道如何编写代码,例如.当我在打印信息中未键入ID或Name的任何数据时,我想告诉我不存在的数据并且不崩溃我的整个程序,请帮帮我,我当然需要:D


So I want to know how to write the code, ex. when I don''t type any data for ID or Name in the print information I want to show me that doesn''t exist and not to crash my entire programm, help me I need certainly :D

推荐答案

如果要检查字段是否存在,则必须编写一些检查代码.例如,当您要求输入内容时,您需要确保用户实际输入了有效的回复.不要接受直接进入类变量的项目,而是将它们读入临时区域并进行验证(它们是否必须是数字或字符,必须在特定范围内,等等),并且仅保存有效条目.永远不要以为用户会a)理解您的问题并b)以预期的方式做出回应.
If you want to check that a field exists or not then you have to write some checking code. For example when you ask for some input you need to make sure the user actually enters a valid response. Don''t accept items direct into the class variables but read them into a temporary area and validate them (do they need to be numbers or characters, must they be within a specific range, etc), and only save valid entries. Never assume that the user will a) understand your question and b) respond in the way that''s expected.


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

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