gpt4 book ai didi

c++ - 句子转换器在换行时不起作用

转载 作者:行者123 更新时间:2023-11-27 23:38:33 25 4
gpt4 key购买 nike

我正在尝试编写一个程序,要求用户提供两个文件名。第一个文件将打开用于输入,第二个文件将打开用于输出。 (假定第一个文件包含以句点结尾的句子。)程序将读取第一个文件的内容,并将除每个句子的第一个字母外的所有字母更改为小写,这应该使大写。修改后的内容存放在第二个文件中。

我能够让代码正常工作,但它输出的行与行之间有很大的中断:

我的输入: https://imgur.com/a/pfFemjJ

这是 input.txt 的文本

c++ is a "High-Level Programming Language" developed by Bjarne Stroustrup at Bell Labs. it helps in optimizing resources AND supports multiplayer options with networking. google's homepage includes a button labeled I'm Feeling Lucky. When a user types in a search AND clicks on the button the user will be taken directly to the first search result, bypassing the search engine results page.

You'll notice the unnecessary spaces between lines in output.txt. I have no clue why it outputs like that.

I've tried to mess around with the while loop I have but I can't seem to debug this issue

#include <algorithm> 
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
char ch;
string inputNAME, outputNAME;
string line;

cout << "Please enter the filename for INPUT file: ";
cin >> inputNAME;

cout << "Please enter the filename for OUTPUT file: ";
cin >> outputNAME;

fstream inFile, outFile;

当“input.txt”在句子之间没有中断时,我的代码可以工作,因为每个句子都没有分开。但是,当句子之间有中断时,它就无法正确转换。

最终目标是让我的输出文件看起来像这样:(注意转换后的句子之间没有中断)

最佳答案

getline(inFile, string, '.')

将读取所有文本直到第一个 '.'。给定输入,string 将包含

c++ is a "High-Level Programming Language" developed by Bjarne Stroustrup at Bell Labs

and

 it helps in optimizing resources AND supports multiplayer options with networking. google's homepage includes a button labeled I'm Feeling Lucky. When a user types in a search AND clicks on the button the user will be taken directly to the first search result, bypassing the search engine results page.

will remain in the stream. Note the end of line that begins the data left in the stream. This is part of your bug.

string will be written to the file along with a '.' and an end of line.

The next call to getline will read

 it helps in optimizing resources AND supports multiplayer options with networking. 

into string. See the end of line character? It's going to be writen to the file. Now you have TWO end of lines. Oops.

Solution:

Don't manually add the end of line. Change

outFile << line << "." << endl;

outFile << line << ".";

如果您觉得需要完成endl,请手动添加flush

关于c++ - 句子转换器在换行时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57343337/

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