c++文件输入小问题 新手求助!
本帖最后由 光着屁股的犀牛 于 2020-5-26 21:11 编辑为啥写入文件我要输入的数据,自己写到下一行去了,然后下一行的数据不见了,望高手指答。
输入主要部分,可能错在这,我猜的
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class record
{
public:
record (void);
~record (void);
void nam ();
void word ();
void write ();
private:
ofstream outfile;
string na;
string wo;
};
record::record(void)
{
outfile.open("a.dat",ios::out);
}
record::~record(void)
{
outfile.close();
}
void record::nam()
{
cout << "please enter the well-know saying" <<endl;
getline (cin,na);
}
void record::word()
{
cout << "please enter the famous person" <<endl;
getline (cin,wo);
}
void record::write()
{
if(!outfile) //testing the opening;
{
cout << "open error" <<endl;
exit (1);
}
outfile << "well-know saying :"<< "\t"<<wo<<"\n"; //输入主要部分,可能错在这,我猜的
outfile <<"famous person :" <<na;
}
int main ()
{
record text1;
text1.nam();
text1.word();
text1.write();
return 0;
}
https://imgchr.com/i/tFUy5t
https://imgchr.com/i/tFamdA outfile << "well-know saying :"<< "\t"<<wo<<"\n"; //输入主要部分,可能错在这,我猜的
outfile <<"famous person :" <<na;
中把wo和na的位置换下就好了
{:5_109:} 自己琢磨出来点东西,就是那个getline 函数 要敲两下回车
,搞得,改一下别的函数就行 热气球 发表于 2020-5-27 09:12
outfile
还是好像不行,自己琢磨了一下,发现好像是getline,要敲两下回车才能输入导致的
页:
[1]