我有一些大学工作,我注意到 get() 不起作用,但我不知道为什么。
我尝试将 getch() 和 getchar() 放在 gets() 之前,但还有其他问题。
当我在do-while(标记-----> 3)之前编写实现gets()的代码时,它可以工作!!!
有人可以帮助我吗?
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
class student
{
int rollNo;
char department[20];
int year;
int semester;
public:
student()
{
rollNo=0;
year=0;
semester=0;
}
void getData();
void promote();
void changeDepartment();
void display();
};
void student::changeDepartment()
{
if(rollNo!=0)
{
cout<<"\nEnter the new Department\n";
gets(department); -------------->1
}
else
{
cout<<"\nStudent not confirmed\n";
}
}
void student::getData()
{
cout<<"\nEnter the roll no\n";
cin>>rollNo;
cout<<"\nEnter the year\n";
cin>>year;
cout<<"\nEnter the semester\n";
cin>>semester;
cout<<"\nEnter the department\n";
gets(department); ----------------> 2
}
void student::promote()
{
if(rollNo!=0)
{
semester+=1;
if(semester%2==1)
{
year+=1;
}
}
else
{
cout<<"\nStudent not confirmed\n";
}
}
void student::display()
{
if(rollNo!=0)
{
cout<<"\nRoll No : "<<rollNo;
cout<<"\nYear : "<<year;
cout<<"\nSemester : "<<semester;
cout<<"\nDepartment : "<<department;
}
else
{
cout<<"\nStudent not confirmed";
}
}
int main()
{
student s;
int ch;
char choice;
----------------> 3
do
{
cout<<"\nMain Menu";
cout<<"\n1. Enter student details";
cout<<"\n2. Change department of student ";
cout<<"\n3. Promote student ";
cout<<"\n4. Display student details ";
cout<<"\nEnter your choice ";
cin>>ch;
switch(ch)
{
case 1 : s.getData();
s.display();
break;
case 2 : s.changeDepartment();
s.display();
break;
case 3 : s.promote();
s.display();
break;
case 4 : s.display();
break;
}
cout<<"\nDo you want to continue? (Y/n)\n";
cin>>choice;
}while((choice=='y')||(choice=='Y'));
return(0);
}