对这项作业有什么建议吗? [英] any suggestion for this homework ?

查看:64
本文介绍了对这项作业有什么建议吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,这里我有一个简单的代码,它是c ++类的功课,我需要一个有关如何为ex编写代码的建议.当计算机要求我输入名称时,如果我按Enter键,则计算机必须在下一个问题时通过我,但是在信息将要打印的情况下,我按Enter键时,它必须表明它没有名称或任何问题我通过了,请帮助我,我非常需要此代码:D:D

hello everybody, here I have a simple code that is a homework from classes in c++, I need an advice how to write a code for ex. when the computer ask me for the name and if I push enter the computer must pass me at the next question but when the information are going to print that question that I pushed enter It must show that it doesn''t have name or any question that I passed, plz help me I need this code so much :D:D

#include <iostream>
#include <string>
using namespace std;
class person
{
private:
    string name;
    string surname;
    int age;

public:
    void fill(string,string,int);
    void printinfo();

};
void person::fill(string n,string s,int a)
{
	name=n;
    surname=s;
    age=a;
}
void person::printinfo()
{
    cout<<"\n\nName of student : "<<name
        <<"\nSurname : "<<surname
        <<"\nAge : "<<age;
}
class student:public person
{
public:
    int ID;
    int year;
    void setkv(int,int);
    void printkv();
};
void student::setkv(int i,int y)
{
    ID=i;
    year=y;
}
void student::printkv()
{
    cout<<"\nStudent ID is: "<<ID;
    cout<<"\nStudent year is: "<<year;
}
int main()
{
    person p;
    student s;
    string name,surname;
    int i,y,age;
	cout<<"\n\tPlease type needed information !";
    cout<<"\n\nName of student: ";
	cin>>name;
	
    cout<<"Surname: ";
    cin>>surname;
    cout<<"Age: ";
    cin>>age;

    p.fill(name,surname,age);

    cout<<"The ID of student: ";
    cin>>i;
    cout<<"And Year of study: ";
    cin>>y;
     
    s.setkv(i,y);
	cout<<"\n\n\tThe typed information are : ";
    p.printinfo();
    s.printkv();
cin.get();cin.get();
return 0;

}

推荐答案

cout<<"\n\nName of student: ";
	cin>>name;


如果您仅在此处按Enter,会发生什么?变量name将不包含任何内容.您应该检查这种情况并采取一些措施.想想如果您是计算机会做什么.


What happens if you just push enter here? The variable name will not contain anything; matbe you should check for that situation and take some action. Think what you would do if you were the computer.


转到此处 [ ^ ]

您的老师没有教您完成功课分配所需的内容吗?我上大学时是我的.
Go here [^]

Didn''t your instructor teach you what was necessary to complete the homework assigment? When I went to college, mine did.


这篇关于对这项作业有什么建议吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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