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




