插入学生的姓名及其ID并显示它们

时间:2015-12-27 15:20:07

标签: c++

这是我的C ++代码

student.h

#include <string>

using namespace std;

class student
{
public:
void setname ();
string getname ();

void setstudentid ();
int getstudentid ();

student ();


private:
string name;
int studentid;

};

student.cpp

#include "student.h"
#include <iostream>

using namespace std;

student::student ()
{
    setname ();

    setstudentid ();

cout << "Student's Name: " << getname () << endl;
cout << "Student's ID: " << getstudentid () << endl;
}

void student::setname ()
{
    string x;

    cout << "Enter student's name: ";
    getline (cin , x);


    name = x;
}

string student::getname ()
{
    return name;
}

void student::setstudentid ()
{
    int x;

    cout << "Enter student's code: ";
    cin >> x;

    studentid = x;
}

int student::getstudentid ()
{
    return studentid;
}

的main.cpp

#include <iostream>
#include <string>
#include "student.h"

using namespace std;

int main ()
{
    int a;

    cout << "1- stundent's info" << endl
        << "2- list of courses" << endl
        << "3- PickUp a course" << endl
        << "Please enter an option: ";
        cin >> a;

    if (a == 1)
    {
        student x;
    }
}

问题如下图所示:

enter image description here

如何使用初始化变量单独打印它们?

1 个答案:

答案 0 :(得分:1)

cin >> a之后,\n将保留在getline(cin,x)中使用的输入缓冲区中。
在再次读取输入之前重置并刷新cin缓冲区 How do I flush the cin buffer?

相关问题