求指点,这个程序错在哪?急

C++语言 码拜 8年前 (2016-04-04) 889次浏览
程序如下,输入输出分别为
Sample Input:
Welcome to our party!
The party begins at 9:00.
party
7:00
Sample Output:
–Searching in Email–
We found “party” in
“–Email–
Sender:Teng
Recipient:Wang
Content:Welcome to our party!

–Searching in File–
Not Found!
//程序:
#include <iostream>
#include <string>
using namespace std;
class Document
{
protected:
string text;
public:
Document(string te):text(te){};
string getText() const {return text;}
};
class Email:public Document
{
private:
string sender,recipient,Etext;
public:
Email(string se,string re):Document(string()),sender(se),recipient(re){};  //!!!!!!!!!!
void setText(string text1){Etext=text1;}
friend ostream & operator<<(ostream &output,Email &email)
{
cout<<“–Email–“<<endl<<“Sender:”;
output<<email.sender<<endl<<“Recipient:”<<email.recipient<<endl<<“Content:”<<email.Etext<<endl;
return output;
}
};
class File:public Document
{
private:
string path,Ftext;
public:
File(string pa,string Ft):Document(Ft),path(pa){};
void setText(string text1){Ftext=text1;}
friend ostream & operator<<(ostream &output,File &file)
{
cout<<“–File–“<<endl<<“Path:”;
output<<file.path<<endl<<“Content:”<<file.Ftext<<endl;
return output;
}
};
bool ContainsKeyword(const Document& docObject,string keyword)
{
if (docObject.getText().find(keyword) != string::npos)
return true;
return false;
}
int main()
{
string word,text;
Email testEmail(“Teng”,”Wang”);
File testFile(“c:\documents\myfile”,”The party begins at 7:00pm.”);
getline(cin,text);
testEmail.setText(text);
getline(cin,text);
testFile.setText(text);
cout<<“–Searching in Email–“<<endl;
cin>>word;
if(ContainsKeyword(testEmail,word))
cout<<“We found “”<<word <<“” in \n””<<testEmail<<“””<<endl;
else
cout<<“Not Found!”<<endl;
cout<<“–Searching in File–“<<endl;
cin>>word;
if(ContainsKeyword(testFile,word))
cout<<“We found “”<<word <<“” in \n””<<testFile<<“””<<endl;
else
cout<<“Not Found!”<<endl;
return 0;
}
解决方案

40

代码功能归根结底不是别人帮本人看或讲解或注释出来的;而是被本人静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
提醒:再牛×的老师也无法代替学生本人领悟和上厕所!
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明求指点,这个程序错在哪?急
喜欢 (0)
[1034331897@qq.com]
分享 (0)