#include<iostream>
using namespace std;
class student
{
private:
int admno;
char sname[20];
float eng,math,science;
float total;
float ctotal()
{
return eng+math+science;
}
public:
void Takedata()
{
cout<<"Enter admission number ";
cin>> admno;
cout<<"Enter student name " ;
gets(sname);// here its giving the error
cout<< "Enter marks in english, math, science ";
cin>>eng>>math>>science;
total=ctotal();
};
void Showdata()
{
cout<<"Admission number "<<admno<<"\nStudent name "<<sname<<"\nEnglish "
<<eng<<"\nMath "<<math<<"\nScience "<<science<<"\nTotal "<<total;
}
};
int main ()
{
student obj ;
obj.Takedata();
obj.Showdata();
return 0;
}
Asked
Active
Viewed 70 times
-3

Hatted Rooster
- 35,759
- 6
- 62
- 122
-
why are you trying to use gets in a c++ program, thats what cin is for – pm100 May 03 '17 at 20:42
-
2`gets` is not declared in `iostream` (it is/was a C function) and is obsolete, as far as I know. Even if it's available in your library, you should never use it. Use C++ I/O in C++. – molbdnilo May 03 '17 at 20:47
-
and use std::string not char[20] – pm100 May 03 '17 at 20:49
-
plz modify the program for me someone. i reqquest – Amit Mishra May 03 '17 at 21:15
-
[tag:gets] was already deprecated. Did you hover or click your mouse into the tag and read its description? [Why is the gets function so dangerous that it should not be used?](http://stackoverflow.com/q/1694036/995714) – phuclv May 04 '17 at 10:14
1 Answers
1
use std::cin.getline
instead of gets
and std::string
instead of char []
.
As comments pointed out, gets
is not declared in iostream
. Furthermore, it has been deprecated in C++11 and removed in C++14, which means even if you include cstdio
or stdio.h
, it just do not compile in compilers supporting C++14.
But still, never use std::cin
for char []
. The reason is the same as gets
. Both will make your program under the danger of buffer overflow when your input is longer than buffer, which will cause your program has unexpected behaviour, like crash. And crackers may even hack your whole computer with it.

xris
- 65
- 1
- 7