这是我的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;
}
}
问题如下图所示:
如何使用初始化变量单独打印它们?
答案 0 :(得分:1)
在cin >> a
之后,\n
将保留在getline(cin,x)
中使用的输入缓冲区中。
在再次读取输入之前重置并刷新cin
缓冲区
How do I flush the cin buffer?