我为什么要把这个拿出来

Why am i getting this out put?

本文关键字:拿出来 为什么      更新时间:2023-10-16

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

预期输出:

这是一个很高兴的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 ) {