gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:47:38 24 4
gpt4 key购买 nike

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

预期输出:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com