I encountered a problem where gets() is not working sometimes without any error compiling. In other words, gets() will not return any value but no warning or error explanation. Here is the code where it's not returning value
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
class student
{
private:
int admno;
char sname[20];
public:
void Takedata()
{
cout<<"Enter admission number ";
cin>> admno;
cout<<"Enter student name " ;
gets(sname);
}
void Showdata()
{
cout<<"Admission number "<<admno<<"\nStudent name "<<sname;
}
};
int main ()
{
student obj ;
obj.Takedata();
obj.Showdata();
getch();
return 0;
}
And in contrast here is the code where it's returning value to "sname"
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
class student
{
private:
int admno;
char sname[20];
public:
void Takedata()
{
cout<<"Enter student name " ;
gets(sname);
}
void Showdata()
{
cout<<"\nStudent name "<<sname;
}
};
int main ()
{
student obj ;
obj.Takedata();
obj.Showdata();
getch();
return 0;
}
If anything is unclear don't hesitate to ask me! I'm glad to accept ant answer/solution/advise!