string型变量的定义问题

angelcm51 2008-07-04 11:20:41
//程序用另一个单词代替指定单词
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;

int main(){
//从键盘读取字符串
string text;
cout<<"please Enter a string terminated by #: ";//terminated终止
std::getline(cin,text,#);
//获得要被取代的单词
string word;
cout<<"please Enter the word to be replaced: ";//replaced取代
cin>>word;
//得到更换的单词
string replacement;
cout<<endl<<"Enter the replacement word: ";
cin>>replacement;

if(word==replacement){
cout<<endl
<<"The word and it's replacement are the same."<<endl
<<"Operation aborted."<<endl;
exit(1);
}
size_t start=text.find(word);

while(start!=string::npos){
text.replace(start,word.length(),replacement);
start=text.find(word,start+replacement.length());
}
cout<<endl
<<"Your string is now: "<<endl
<<text<<endl;
return 0;
}


为什么它说string 的定义不对呢`?说word 没有定义
...全文
102 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
macfan 2008-07-04
  • 打赏
  • 举报
回复
1楼正解.
bargio_susie 2008-07-04
  • 打赏
  • 举报
回复
LZ粗心了。

std::getline(cin,text, '#');
xkyx_cn 2008-07-04
  • 打赏
  • 举报
回复

//程序用另一个单词代替指定单词
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;

int main(){
//从键盘读取字符串
string text;
cout<<"please Enter a string terminated by #: ";//terminated终止
std::getline(cin,text,'#'); // 要细心:#要用'' 或者 ""扩起来,不然就成宏连接了
//获得要被取代的单词
string word;
cout<<"please Enter the word to be replaced: ";//replaced取代
cin>>word;
//得到更换的单词
string replacement;
cout<<endl<<"Enter the replacement word: ";
cin>>replacement;

if(word==replacement){
cout<<endl
<<"The word and it's replacement are the same."<<endl
<<"Operation aborted."<<endl;
exit(1);
}
size_t start=text.find(word);

while(start!=string::npos){
text.replace(start,word.length(),replacement);
start=text.find(word,start+replacement.length());
}
cout<<endl
<<"Your string is now: "<<endl
<<text<<endl;
return 0;
}

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧