我遇到了一個問題,其中gets()不工作有時沒有任何錯誤編譯。換句話說,gets()不會返回任何值,但不會有警告或錯誤說明。 這裏是它沒有返回值錯誤gets(),不編譯錯誤,但無法接收輸入
#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;
}
而在相反的代碼這裏是代碼,其中它的返回值「的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;
}
如果有不清楚的地方請不要猶豫,問我!我很高興接受螞蟻回答/解決方案/建議!
請注意,在C11和C++ 14中'gets'已被刪除。在C++中,你應該使用'std :: string'和'getline'來處理字符串。 – NathanOliver
[永遠不要使用'gets'](http://stackoverflow.com/q/1694036/10077)。 –
謝謝你的回覆,我把gets(sname)改爲getline(cin,sname),char sname [20]改爲string sname,同樣的問題仍然存在。 –