I have some college work and as i noticed that the gets() is not working but i can't figure out why.
I tried putting getch() and getchar() before gets() but there is something else wrong.
When i write a code implementing gets() before do-while (labeled -----> 3) it works!!!
Can somebody help me?
#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);
}