CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C++ 语言

C++当中的几个小问题

楼主sms88(白板http://shop34112882.taobao.com)2003-10-01 19:34:44 在 C/C++ / C++ 语言 提问

第一题:  
  该程序为何运行后无任何输出?程序中打开的文档已经存在  
   
  //分别计算出一文档的单词,行数和空格的个数  
  #include<iostream>  
  #include<fstream>  
  #include<iomanip>  
  using   namespace   std;  
  int   main()  
  {  
  int   space=0;                     //定义空格的存储变量  
  int   line=0;                       //定义行数的存储变量  
  int   words=0;                     //定义单词数的存储变量  
  char   ch;    
  ifstream   infile;  
  ofstream   outfile;  
  infile.open("e:text1.txt");       //打开文档  
  outfile.open("e:text.txt");       //建立输出文档  
  if(!infile)                                     //判断文档是否打开  
  {  
  cout<<"can   not   open   the   file."<<endl;  
  };  
  cin.get(ch);                                 //读取一字符  
  if(ch=='\0')                                 //判断该第一个字符  
  {  
  cout<<"end.";  
  return   1;  
  };  
  while(infile)                             //逐个读取整个文档的字符  
  {  
  if(ch=='\0')  
  {  
  space++;     //空格数  
  words++;     //单词数  
  };  
  if(ch=='\n')  
  line++;       //行数  
  cin.get(ch);  
  };  
  outfile<<"words:"<<++words<<"lines:"  
                  <<line<<"spaces:"<<space<<  
  endl;                       //输出结果  
  infile.close();  
  outfile.close();  
  return   0;  
  }  
   
  ==================================================  
  第二题:  
  为何cout<<menuList.nemuItem;输出不了结果?  
  //定义了一个结构体  
  #include<iostream>  
  #include<string>  
  using   namespace   std;  
  struct   menuItemType  
  {  
  string   menuItem;  
  double   menuPrice;  
  }menuList;  
  int   main()  
  {            
          string   name;  
          ifstream   infile;  
          infile.open("e:about.txt");  
          infile>>name;  
          menuList.menuItem=name;  
          cout<<menuList.nemuItem;  
  }  
   
  ===================================================  
  第三题:  
   
  怎么运行一些程序时会弹出这样的对话框?  
  该对话框为:  
   
  弹出应用程序:   1-1.exe   -   应用程序错误:   "0x004269d0"   指令引用的   "0x00000000"   内存。  
  该内存不能为   "written"。  
   
  要终止程序,请单击“确定”。  
  要调试程序,请单击“取消”。    
   
  ============================================================  
  第四题:  
  该程序是练习"重载流插入<<"和"重载析取>>运算符"  
  怎么会有错呢?  
  #include<iostream>  
  using   namespace   std;  
  class   opoverclass     //定义类opoverclass  
  {  
  friend   ostream&   operator<<(ostream&,const   opoverclass&);  
  //声明重载流插入<<  
                  friend   istream&   operator>>(istream&,opoverclass&);  
                  //声明重载析取>>运算符  
  public:  
  opoverclass(int,int);  
  private:  
  int   a;  
  int   b;  
  };  
  opoverclass::opoverclass(int,int)  
  {  
  a=2;  
  b=3;  
  }  
  ostream&   operator<<(ostream&   osobject,const   opoverclass&   right)  
                  //定义重载流插入<<函数  
  {  
  osobject<<"("<<right.a<<","<right.b<<")";  
  return   osobject;  
  }  
  istream&   operator>>(istream&   isobject,opoverclass&   right)  
                  //定义重载析取>>运算符函数  
  {  
  isobject>>right.a>>right.b;  
  return   isobject;  
  }  
   
  int   main()     //主函数  
  {  
  opoverclass   one(1,23);//定义一个名为one的类  
  cout<<"enter   two   integers:";  
  cin>>one;     //调用重载流插入<<函数  
  cout<<one;   //调用定义重载析取>>运算符函数  
  }  
   
   
   
  问题点数:80、回复次数:7Top

1 楼ALNG(?)回复于 2003-10-01 20:58:29 得分 15

"e:text1.txt"  
  之类的全部改为  
  "e:\\text1.txt"  
  看看。  
   
   
   
  Top

2 楼ALNG(?)回复于 2003-10-01 21:07:47 得分 17

3:   访问了非法内存,比如未初始化或已delete的内存。  
  如:  
          int   *   pInt;  
          *pInt   =   1000;   //   错误,会导致Access   Violation.   因为pInt还没有指向合法内存  
   
          可以:  
          pInt   =   new   int;  
          *pInt   =   1000;  
   
   
  4:   看起来你还不会调试,问题出在如下函数  
   
  ostream&   operator<<(ostream&   osobject,const   opoverclass&   right)  
                  //定义重载流插入<<函数  
  {  
  osobject<<"("<<right.a<<","<right.b<<")";  
                    //                                                 这里的<改成<<就可以了  
  return   osobject;  
  }  
  Top

3 楼ssbull(初学者)回复于 2003-10-01 21:09:51 得分 11

第二题你只要把"e:about.txt"改成"e:\\about.txt"即可  
  第三题表明你的程序内存溢出,这说明你的程序中有问题,你要调试一下Top

4 楼tanyaliji(努力学习.net)(★)回复于 2003-10-01 21:14:50 得分 0

learningTop

5 楼TongNi(小白)回复于 2003-10-01 22:32:08 得分 10

第一题cin.get(ch)改为:infile.get(ch)  
  还有:'/0'好像不能判断空格,改为'   ',再用循环判断是否下一个字符为非空格Top

6 楼TongNi(小白)回复于 2003-10-01 22:41:45 得分 15

#include<iostream>  
  #include<fstream>  
  #include<iomanip>  
  using   namespace   std;  
  int   main()  
  {  
  int   space=0;                     //定义空格的存储变量  
  int   line=0;                       //定义行数的存储变量  
  int   words=0;                     //定义单词数的存储变量  
  char   ch;    
  ifstream   infile;  
  ofstream   outfile;  
  infile.open("e:text1.txt");       //打开文档  
  outfile.open("e:text.txt");       //建立输出文档  
  if(!infile)                                     //判断文档是否打开  
  {  
  outfile<<"can   not   open   the   file."<<endl;  
  };  
  infile.get(ch);                                 //读取一字符  
  if(ch=='\0')                                 //判断该第一个字符  
  {  
  cout<<"end.";  
  return   1;  
  };  
  while(infile)                             //逐个读取整个文档的字符  
  {  
  if(ch=='   '&&ch!='/n')  
  {  
  space++;     //空格数  
  words++;     //单词数  
  infile.get(ch);  
  if(ch=='   ')  
  {  
  while(ch=='   ')  
  infile.get(ch);  
  }  
  else  
  continue;  
  }  
  if(ch=='\n')  
  {  
  line++;    
  words++;  
  };//行数  
  infile.get(ch);  
  };  
  space-=line;  
  outfile<<"words:"<<--words<<"lines:"  
                  <<++line<<"spaces:"<<++space<<  
  endl;                       //输出结果  
  infile.close();  
  outfile.close();  
  return   0;  
  }  
  Top

7 楼TongNi(小白)回复于 2003-10-01 22:48:52 得分 12

第二题:  
                  1。定义名字上有错。  
                  2。int   main()要返回值Top

相关问题

  • C++的几个小问题
  • c&&c++几个问题
  • 介绍几本c,c++书
  • 几个C#小问题, 给分!
  • C语言的几个小问题
  • c小问题
  • c++小问题
  • c++小问题?
  • c++小问题?
  • C++小题

关键词

  • 文档
  • 函数
  • 内存
  • opoverclass
  • infile
  • 运算符
  • 定义
  • 存储变量
  • pint
  • outfile

得分解答快速导航

  • 帖主:sms88
  • ALNG
  • ALNG
  • ssbull
  • TongNi
  • TongNi
  • TongNi

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo