鱼C论坛

 找回密码
 立即注册
查看: 532|回复: 3

[已解决]c++文件输入小问题 新手求助!

[复制链接]
发表于 2020-5-26 21:09:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 光着屁股的犀牛 于 2020-5-26 21:11 编辑

为啥写入文件我要输入的数据,自己写到下一行去了,然后下一行的数据不见了,望高手指答。

输入主要部分,可能错在这,我猜的
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>

  4. using namespace std;

  5. class record
  6. {
  7. public:
  8.        
  9.         record (void);
  10.         ~record (void);
  11.         void nam ();
  12.         void word ();
  13.         void write ();


  14. private:
  15.         ofstream outfile;
  16.         string na;
  17.         string wo;


  18. };

  19. record::record(void)
  20. {
  21.         outfile.open("a.dat",ios::out);
  22. }

  23. record::~record(void)
  24. {
  25.         outfile.close();
  26. }

  27. void record::nam()
  28. {
  29.         cout << "please enter the well-know saying" <<endl;
  30.         getline (cin,na);
  31. }

  32. void record::word()
  33. {
  34.         cout << "please enter the famous person" <<endl;
  35.         getline (cin,wo);
  36. }

  37. void record::write()
  38. {
  39.         if(!outfile)             //testing the opening;
  40.         {
  41.                 cout << "open error" <<endl;
  42.                 exit (1);
  43.         }

  44.         outfile << "well-know saying :"<< "\t"<<wo<<"\n";   //输入主要部分,可能错在这,我猜的
  45.         outfile <<"famous person :" <<na;
  46. }

  47. int main ()
  48. {
  49.        
  50.         record text1;
  51.         text1.nam();
  52.         text1.word();
  53.         text1.write();

  54.         return 0;
  55. }
复制代码



                               
登录/注册后可看大图


                               
登录/注册后可看大图
最佳答案
2020-5-27 09:12:04
outfile << "well-know saying :"<< "\t"<<wo<<"\n";   //输入主要部分,可能错在这,我猜的
        outfile <<"famous person :" <<na;
中把wo和na的位置换下就好了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-5-27 09:12:04 | 显示全部楼层    本楼为最佳答案   
outfile << "well-know saying :"<< "\t"<<wo<<"\n";   //输入主要部分,可能错在这,我猜的
        outfile <<"famous person :" <<na;
中把wo和na的位置换下就好了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-29 15:43:52 | 显示全部楼层
自己琢磨出来点东西,就是那个getline 函数 要敲两下回车
,搞得,改一下别的函数就行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-29 15:44:57 | 显示全部楼层

还是好像不行,自己琢磨了一下,发现好像是getline,要敲两下回车才能输入导致的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-3 04:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表