如何修复无休止的周期? 作为一个作业,我们应该简单地把代码从D.S. Malik的C++书中拷贝出来。这就是所谓的“文本处理”。我已经准确地复制了它,并在这本书上再读了几遍,试图找出问题所在

如何修复无休止的周期? 作为一个作业,我们应该简单地把代码从D.S. Malik的C++书中拷贝出来。这就是所谓的“文本处理”。我已经准确地复制了它,并在这本书上再读了几遍,试图找出问题所在,c++,arrays,string,text-processing,C++,Arrays,String,Text Processing,它应该在内嵌中复制并粘贴文本,然后计算字母数量,并列出字母在文本中出现的次数。它粘贴文本;然而,无论文本中的最后一个字符是什么,它只是无限重复该字符。对我来说,它只是在课文末尾重复一次 在编写代码之前,我将在下面显示输入的文本 填充文本 已知的第一个进行计算的设备是算盘。 算盘是在亚洲发明的,但在古巴比伦使用, 中国和整个欧洲直到中世纪晚期。aba- cus使用机架中的滑珠系统进行添加和细分- 牵引。1642年,法国哲学家兼数学家布莱斯 帕斯卡发明了一种叫做帕斯卡林的计算装置。信息技术 在轮子上

它应该在内嵌中复制并粘贴文本,然后计算字母数量,并列出字母在文本中出现的次数。它粘贴文本;然而,无论文本中的最后一个字符是什么,它只是无限重复该字符。对我来说,它只是在课文末尾重复一次

在编写代码之前,我将在下面显示输入的文本

填充文本

已知的第一个进行计算的设备是算盘。
算盘是在亚洲发明的,但在古巴比伦使用,
中国和整个欧洲直到中世纪晚期。aba-
cus使用机架中的滑珠系统进行添加和细分-
牵引。1642年,法国哲学家兼数学家布莱斯
帕斯卡发明了一种叫做帕斯卡林的计算装置。信息技术
在轮子上有八个可移动的刻度盘,可以计算总计
八位数长。算盘和帕斯卡林都能演奏
只有加法和减法运算。17世纪后期-
tury Gottfried von Leibniz发明了一种能够添加,
减法、乘和除。
输出文件中应该包含什么内容

#include <iostream>
#include <fstream>
#include <cctype>

using namespace std;

void initialize(int& lc, int list[]);
void copyText(ifstream& intext, ofstream& outtext, char& ch, int list[]);
void characterCount(char ch, int list[]);
void writeTotal(ofstream& outtext, int lc, int list[]);

int main()
{
    //Step 1; Declare variables
int lineCount;
int letterCount[26];
char ch;
ifstream infile;
ofstream outfile;

infile.open("textin.txt");                              //Step 2

if (!infile)                                            //Step 3
{
    cout << "Cannot open the input file."
        << endl;
    return 1;
}

outfile.open("textout.out");                            //Step 4

initialize(lineCount, letterCount);                     //Step 5

infile.get(ch);                                         //Step 6

while (infile)                                          //Step 7
{
    copyText(infile, outfile, ch, letterCount);         //Step 7.1
    lineCount++;                                        //Step 7.2
    infile.get(ch);                                     //Step 7.3
}

writeTotal(outfile, lineCount, letterCount);            //Step 8

infile.close();                                         //Step 9
outfile.close();                                        //Step 9

return 0;
}

void initialize(int& lc, int list[])
{
lc = 0;

for (int j = 0; j < 26; j++)
    list[j] = 0;
} //end initialize

void copyText(ifstream& intext, ofstream& outtext, char& ch, int list[])
{
while (ch != '\n')                  //process the entire line
{
    outtext << ch;                  //output the character

    characterCount(ch, list);       //call the function character count

    intext.get(ch);                 //read the next character
}
outtext << ch;                      //output the newline character
} //end copyText

void characterCount(char ch, int list[])
{
int index;

ch = toupper(ch);                                               //Step a

index = static_cast<int>(ch) - static_cast<int>('A');           //Step b

if (0 <= index && index < 26)                                   //Step c
    list[index]++;
}//end characterCount

void writeTotal(ofstream& outtext, int lc, int list[])
{
outtext << endl << endl;
outtext << "The number of lines = " << lc << endl;

for (int index = 0; index < 26; index++)
    outtext << static_cast<char>(index + static_cast<int>('A')) << " count = " << list[index] << endl;
} //end writeTotal
今天,我们生活在一个信息几乎是一次性处理的时代
光速。通过计算机,技术革命正在发生
彻底改变了我们的生活方式和沟通方式
另一个像“互联网”这样的术语,刚开始还不熟悉
几年前,这种情况在今天非常普遍。在计算机的帮助下,你
可以给你所爱的人写信,也可以接收你所爱的人的来信
秒。你再也不需要通过邮件发送回复来申请签证了
工作;在很多情况下,你可以通过
互联网。你可以实时观察股票的表现
立即买卖它们。学生们经常上网冲浪
并使用计算机设计课堂项目。他们还使用
强大的文字处理软件,完成他们的学期论文。
许多人在电脑上维护和平衡支票簿。
行数=15
A计数=53
B计数=7
C计数=30
D计数=19
E计数=81
F计数=11
G计数=10
H计数=29
我数=41
J计数=4
K计数=3
L计数=31
M计数=26
N计数=50
O计数=59
P计数=21
Q计数=0
R计数=45
S计数=48
T计数=62
U计数=24
V计数=7
W计数=15
X计数=0
Y计数=20
Z计数=0
代码

#include <iostream>
#include <fstream>
#include <cctype>

using namespace std;

void initialize(int& lc, int list[]);
void copyText(ifstream& intext, ofstream& outtext, char& ch, int list[]);
void characterCount(char ch, int list[]);
void writeTotal(ofstream& outtext, int lc, int list[]);

int main()
{
    //Step 1; Declare variables
int lineCount;
int letterCount[26];
char ch;
ifstream infile;
ofstream outfile;

infile.open("textin.txt");                              //Step 2

if (!infile)                                            //Step 3
{
    cout << "Cannot open the input file."
        << endl;
    return 1;
}

outfile.open("textout.out");                            //Step 4

initialize(lineCount, letterCount);                     //Step 5

infile.get(ch);                                         //Step 6

while (infile)                                          //Step 7
{
    copyText(infile, outfile, ch, letterCount);         //Step 7.1
    lineCount++;                                        //Step 7.2
    infile.get(ch);                                     //Step 7.3
}

writeTotal(outfile, lineCount, letterCount);            //Step 8

infile.close();                                         //Step 9
outfile.close();                                        //Step 9

return 0;
}

void initialize(int& lc, int list[])
{
lc = 0;

for (int j = 0; j < 26; j++)
    list[j] = 0;
} //end initialize

void copyText(ifstream& intext, ofstream& outtext, char& ch, int list[])
{
while (ch != '\n')                  //process the entire line
{
    outtext << ch;                  //output the character

    characterCount(ch, list);       //call the function character count

    intext.get(ch);                 //read the next character
}
outtext << ch;                      //output the newline character
} //end copyText

void characterCount(char ch, int list[])
{
int index;

ch = toupper(ch);                                               //Step a

index = static_cast<int>(ch) - static_cast<int>('A');           //Step b

if (0 <= index && index < 26)                                   //Step c
    list[index]++;
}//end characterCount

void writeTotal(ofstream& outtext, int lc, int list[])
{
outtext << endl << endl;
outtext << "The number of lines = " << lc << endl;

for (int index = 0; index < 26; index++)
    outtext << static_cast<char>(index + static_cast<int>('A')) << " count = " << list[index] << endl;
} //end writeTotal
#包括
#包括
#包括
使用名称空间std;
无效初始化(int&lc,int list[]);
void copyText(ifstream&intext、ofstream&outtext、char&ch、int list[]);
void characterCount(char ch,int list[]);
void writeTotal(流和输出文本、整数lc、整数列表[]);
int main()
{
//步骤1;声明变量
整数行数;
整数字母计数[26];
char ch;
河流充填;
出流孔的直径;
infile.open(“textin.txt”);//步骤2
if(!infle)//步骤3
{

请在debugger中检查此项。通过肉眼调试后,我在
copyText()
中发现以下弱点:如果文件未以
\n
结尾,则无法跳出循环。(你可以用一个很短的一行程序来检查它,而不是原来的输入文本。如果我忽略了什么,请尝试一个以
\n
结尾的文件,而不是一个以
\n
结尾的文件。)你应该在(ch!='\n')
时扩展
,并附加一个条件,以
intext
的良好状态(请在调试器中确认我的猜测。) ;-)顺便说一句,你有没有注意到你的输出确实包含了与输入不同的文本?这是老师测试你注意力的把戏还是你的复制粘贴错误?@Scheff谢谢你的输入我完全忘记了我在这里发布的内容!我的代码实际上是完全正确的,只是我包含文本的输入文件不正确。My教授给我发送了输入文本文件应该是什么样子的,唯一的区别是在输入文件的开头有一个缩进,它修复了错误,每次运行都很好!谢谢你的帮助!