我为什么要把这个拿出来
Why am i getting this out put?
我已经为这个程序工作了很长时间,我想我快要完成了。然而,我的代码输出了一些奇怪的东西,我找不到问题所在。
预期输出:
这是一个很高兴的tESt来检查我的读者是否工作
一只快乐的鳄鱼在快乐的公园里,一头快乐的奶牛戴着快乐的围巾吹鼻子。你是一个快乐的八角形THe 吗
实际输出:
这是一个检查我的读者是否工作的tE happySt
快乐的
快乐
我如何才能使以下代码按我期望的方式运行?
#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
#include <sstream>
#include <locale>
using namespace std;
void
usage(char *progname, string msg){
cerr << "Error: " << msg << endl;
cerr << "Usage is: " << progname << " [filename]" << endl;
cerr << " specifying filename reads from that file; no filename reads standard input" << endl;
}
string capitalization(string word,string adj){
for(int i = 0; i <= word.length(); i++){
if(isupper(word[i])){
for(int j = 0; j <= adj.length(); j++){
adj[j] = toupper(adj[j]);
return adj;
}
}
else if(isupper(word[0])){
for(int j = 0; j <= adj.length(); j++){
adj[j] = tolower(adj[j]);
return adj;
}
}
else{
for(int j = 0; j <= adj.length(); j++){
adj[j] = tolower(adj[j]);
return adj;
}
}
}
}
int main(int argc, char *argv[]){
string adj;
string file;
cin >> adj;
cin >> file;
string line;
string articles[14] = {"a","A","an","aN","An","AN","the","The","tHe","thE","THe","tHE","ThE","THE"};
ifstream rfile;
rfile.open(file.c_str());
if(rfile.fail()){
cerr << "Error while attempting to open the file." << endl;
return 0;
}
string::size_type pos;
string word;
string words[1024];
while(getline(rfile,line)){
istringstream iss(line);
for(int i = 0; i <= line.length(); i++){
iss >> word;
words[i] = word;
for(int j = 0; j <= 14; j++){
if(word == articles[j]){
string article = word;
iss >> word;
pos = line.find(article);
//cout << pos << endl;
string adjec = capitalization(word,adj);
int position = (pos + word.length());
line.insert(position, " " + adjec);
continue;
}
}
}
cout << line << "n";
}
}
这可能无法解决任何问题,但。。。
这些行中的逻辑是错误的。
istringstream iss(line);
for(int i = 0; i <= line.length(); i++){
iss >> word;
假设你的线路是
This is a test.
对于这行,line.length()
是15,但没有15个单词。你需要的是
istringstream iss(line);
while ( iss >> word ) {
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 为什么在全局范围内使用"extern int a"似乎不行?
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 为什么会发生堆损坏
- 为什么使用 "this" 指针调用派生成员函数?
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 为什么比较运算符如此快速
- 为什么 Serial.println(<char[]>);返回随机字符?
- 我重载了 << 和 = 运算符。为什么当我将一个对象分配给另一个对象并尝试打印它时,我会被打印出来?
- 为什么'Hello'与CPP中的'World'进行比较时被打印出来?
- FFMPEG:为什么当我调用av_codec_next()时,我所有的编解码器都没有显示出来
- 为什么要拿参考的地址给我一个第二等级的指针
- 为什么我的一半没有打印出来?
- 为什么QList不是从QObject派生出来的?
- 我为什么要把这个拿出来
- 为什么我不能从C++中的模板化超类中独立出来?
- 为什么打印出来的价值都是垃圾?我们怎样才能解决这个问题
- 为什么有些顶点没有画出来