getline的错误在哪?
//有3个问题,都打了注释了~~~~~找了很多资料,还是不知道为什么~~~
#include <iostream.h>
#include <fstream.h>
class Student
{
private:
int stuID[11];
char stuName[10];
char stuSex[3];
public:
void getData()
{
cout <<"\n请输入学生的学号:";
cin >>stuID; //这里,想请问有没有办法控制输入的长度呢?比如学号定为10个字节?
cout <<"\n请输入学生的姓名:";
cin.getline(stuName,10); //程序通过编译,但运行时,会跳过这里,直接进入下一行,为什么呢?
cout <<"\n请输入学生的性别:";
cin.getline(stuSex,3); //有什么办法使得性别的输入只有“男”“女”两种选择?
}//end of getData()
void dispData()
{
//^^^^^^略
}//end of dispData()
};
int main()
{
Student stuObj;
cout <<"欢迎使用学生信息系统!\n";
int choice;
while ( true )
{
cout <<"\n0.退出系统";
cout <<"\n1.添加学生信息";
cout <<"\n2.查看学生信息";
cin >>choice;
if (choice==1)
{
ofstream outFile("LINPROJECT_student.dat");
char reply='Y';
while (reply=='Y' || reply=='y')
{
cout <<"\n输入详细资料:\n\n";
stuObj.getData();
outFile.write((char*)&stuObj, sizeof(stuObj));
cout <<"是否继续?(Y/N)"<<endl;
cin >>reply;
}
outFile.close();
}//end of if
if (choice==2)
{
//^^^^^^略
}//end of if
if (choice==0)
break;
}//end of while
return 0;
}