c++ - 为什么我得到这个输出?

标签 c++

我已经为这个项目工作了很长时间,我想我快完成了。但是,我的代码输出了一些奇怪的东西,我找不到问题所在。

预期输出:

This is a happy tESt to check if my reader works!
An happy alligator was AT tHe happy park and a happy a cow blew its nose in a happy scarf. are you an happy Octagon THe

实际输出:

This is a tE happySt to check if my reader works!
a happy
THe Happy

我怎样才能让下面的代码按我预期的方式运行?

#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 ) {

关于c++ - 为什么我得到这个输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28512193/

相关文章:

c++ - obb 旋转计算

c++ - 用于 C++ 2D GameEngine 的 Chipmunk Physics 或 Box2D?

c++ - googletest 有没有办法为被调用函数中的断言生成调用者行号?

c++ - QT从信号返回值?

c++ - 如何使用 strcpy_s() 将多个 char* 字符串复制到一个字符串中?

c++ - 在 iPhone 上使用现有的 C++ 引擎

c++ - 如何在可观察对象列表上使用RxCpp运算符?

c++ - 什么是 undefined reference /未解析的外部符号错误以及如何修复它?

c++ - OSG : Get transform matrix from a node

c++ - 对 C++ 哈希表有一个好的哈希函数吗?