请高手指教!

cstrike 2006-10-25 10:26:27
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
using std::cin;
using std::vector;
using std::cout;
using std::endl;
using std::string;
int main ()
{string word;
int counter=0,i=0,n=0;
vector<string>text;
while(cin>>word)
{text.push_back(word);++counter;
}
for (i=0;i!=text.size();++i)
{text[i]=toupper(text[i]);
}
for (n=1;n%8!=0;++n)
{cout<<text[n-1]<<endl;
}
return 0;
}
程序如上,要解决的问题是读入一段文本,每个单词存为一个元素,再把每个单词转化为大写字母,输出转化后的元素.每8个一行输出.
E:\wrong\word out.cpp(18) : error C2664: 'toupper' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.
我现在有个疑问是我的输出是否正确,不清楚每8个单词输出怎么编写程序,请高手帮忙,谢谢了
...全文
170 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
mstlq 2006-10-25
  • 打赏
  • 举报
回复
vc6可用版本……
不过不知道为什么要连续输入两次^Z(ctrl+z 然后回车),才能结束输入……
ps:楼主给的分也太少了吧?严重打击某些朋友的积极性……

#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <algorithm>
//#include <functional>


using std::cin;
using std::vector;
using std::cout;
using std::endl;
using std::string;

int main ()
{string word;
int counter=0,i=0,n=0;
vector<string>text;
while(cin>>word)
{text.push_back(word);++counter;
}
char *c;
for (i=0;i!=text.size();++i)
{for(c=text[i].begin(); c!=text[i].end();c++)
*c=toupper(*c);
}
for (n=0;n<text.size();++n)
{cout<<text[n]<<" ";
if (n%8==7)
cout<<endl;
}
system("pause");
return 0;
}
mstlq 2006-10-25
  • 打赏
  • 举报
回复
lw1a2(一刀 CSDN的等级,只和上网时间有关)
关于处理单词的的方法是对的,不过输出部分有点小问题,修正如下
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <algorithm>
#include <functional>


using std::cin;
using std::vector;
using std::cout;
using std::endl;
using std::string;
int main ()
{string word;
int counter=0,i=0,n=0;
vector<string>text;
while(cin>>word)
{text.push_back(word);++counter;
}
for (i=0;i!=text.size();++i)
{transform(text[i].begin(), text[i].end(), //source
text[i].begin(), //destination
toupper);
}
for (n=0;n<text.size();++n)
{cout<<text[n]<<" ";
if (n%8==7)
cout<<endl;
}
system("pause");
return 0;
}

————————————————————————
在g++中通过编译(vc6不可)

输入
jsldk sd0ioj psdo8r psd0fu npsoer8 pdoisaru psdoiru ;eouf pa9sdd ol9asdy lasfdi7
oasidy asdhg8ww asldiuddf lasidyuaaa aosidywewew asod8yssss (回车)
^Z(回车)

输出

JSLDK SD0IOJ PSDO8R PSD0FU NPSOER8 PDOISARU PSDOIRU ;EOUF
PA9SDD OL9ASDY LASFDI7 OASIDY ASDHG8WW ASLDIUDDF LASIDYUAAA AOSIDYWEWEW
ASOD8YSSSS 请按任意键继续. . .

达到了楼主的要求……
lw1a2 2006-10-25
  • 打赏
  • 举报
回复
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
using std::cin;
using std::vector;
using std::cout;
using std::endl;
using std::string;
int main ()
{string word;
int counter=0,i=0,n=0;
vector<string>text;
while(cin>>word)
{text.push_back(word);++counter;
}
for (i=0;i!=text.size();++i)
{transform (text[i].begin(), text[i].end(), //source
text[i].begin(), //destination
toupper);
}
for (n=1;n%8!=0&&n<text.size();++n)
{cout<<text[n-1]<<endl;
}
system("pause");
return 0;
}
cstrike 2006-10-25
  • 打赏
  • 举报
回复
请问我改怎么改正呢?麻烦帮我修改一下好吧?
还有我觉得我的程序不会输出8个单词,可能会输出8个字母,请高手指教一下
wave_ 2006-10-25
  • 打赏
  • 举报
回复
up
lw1a2 2006-10-25
  • 打赏
  • 举报
回复
toupper的参数是char

64,643

社区成员

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

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