C++ 代码读取我循环中的额外行?

C++ 代码读取我循环中的额外行?,c++,loops,getline,stringstream,counting,C++,Loops,Getline,Stringstream,Counting,我的目标是从输入文件中读取并计算至少包含1个小写字母和1个数字的行数。我已经解决了我的代码的其余部分,其中计算了所有的小写、大写、数字、字符和单词,没有问题。我还读取了输入文件并逐字颠倒了行。我似乎不明白为什么代码只计算了8行,而只有7行,1个小写字母和1个数字。在对所有其他循环使用getline()时,我没有遇到任何问题。我不是专门找人为我写代码的人。如果可能的话,我想解释一下为什么会发生这种情况 我的输入文件包含: 这是hw3的测试文件 这个字母表有多少个大写字母? 这个字母里有多少个小写字

我的目标是从输入文件中读取并计算至少包含1个小写字母和1个数字的行数。我已经解决了我的代码的其余部分,其中计算了所有的小写、大写、数字、字符和单词,没有问题。我还读取了输入文件并逐字颠倒了行。我似乎不明白为什么代码只计算了8行,而只有7行,1个小写字母和1个数字。在对所有其他循环使用getline()时,我没有遇到任何问题。我不是专门找人为我写代码的人。如果可能的话,我想解释一下为什么会发生这种情况

我的输入文件包含:

这是hw3的测试文件
这个字母表有多少个大写字母?
这个字母里有多少个小写字母?
此文件中有多少个数据?
反向转动1npU7 N4m3
将线路反转到opp05173 coutnerpart
查找F1le中字符5的总数
这是一条测试线
我在这一部分的代码是:

infle.clear();
填充seekg(0,填充beg);
while(getline(infle,line)){
str(行);
clear();
wordInput.seekg(0);
while(wordInput.get(c)){
if(岛下(c)){
小写++;
}   
否则,如果(i数字(c)){
数字++;
}   
}   
如果(小写>=1&&digit>=1){
lineCount++;
}
}    

你可以把所有的
int
变量初始化为
0
,这很好;但是,让我们看看您的代码(格式化后缩进更有意义):

在这里,您读取一行,并遍历该行上的所有字符,如果您找到一个小写字符或数字,则递增一个变量。当你读下一行时会发生什么?您尚未将这些变量重置回
0
,因此在读取下一行时,它们都将位于
1
上方

您需要以下内容:

while(getline(inFile, line))
{
    wordInput.str(line);
    wordInput.clear();
    wordInput.seekg(0);

    // We're about to start reading this line, so obviously we haven't found any yet
    digit = 0;
    lowerCase = 0;
更好的是,您可以在读取行
中声明这些变量,而
循环:

while(getline(inFile, line))
{
    int digit = 0;
    int lowerCase = 0;
虽然还没有教过您如何使用调试器,但调试的一个好方法是使用
cout
语句。放入一些打印语句,以确定在任何给定时间内所有变量是什么:

while(getline(inFile, line))
{
    std::cout << "read line " << line << std::endl;
    while(wordInput.get(c))
    {
        std::cout << "lowercase found so far: " << lowerCase << std::endl;
        std::cout << "digits found so far: " << digit << std::endl;
        if(islower(c))
        {
            std::cout << "lowercase character found: " << c << std::endl;
            lowerCase++;
        }   
        else if(isdigit(c))
        {
            std::cout << "digit found: " << c << std::endl;
            digit++;
        }   
    }   

    if(lowerCase >= 1 && digit >= 1)
    { 
        std::cout << "found a lowercase character (" << lowerCase << ") or a digit (" << digit << ")" << std::endl;
        lineCount++;
    }

}   
while(getline(infle,line))
{

Std::CUT

自从我在C++中编码以来,已经有一段时间了。还有其他的调试方法比调试器程序要多。


我会在循环的开始添加
cout,在你读完这行之后,打印出
数字和
小写字母:你会发现它们没有被重置为
0
,所以本质上它们总是
=1
,即使行上没有数字。啊,我明白了。这完全有道理。我需要重置它们循环中的e值。这非常有效。非常感谢。我真的绞尽脑汁了。非常感谢你的回答!我真的非常感谢你花时间解释这一点。通过在while循环下的读取中添加小写=0和数字=0,它就像一个符咒。我没有想到这是个问题。我将继续让我的台词来再次检查正在发生的事情。我希望我能给你们的不仅仅是一张赞成票和一张支票!
while(getline(inFile, line))
{
    int digit = 0;
    int lowerCase = 0;
while(getline(inFile, line))
{
    std::cout << "read line " << line << std::endl;
    while(wordInput.get(c))
    {
        std::cout << "lowercase found so far: " << lowerCase << std::endl;
        std::cout << "digits found so far: " << digit << std::endl;
        if(islower(c))
        {
            std::cout << "lowercase character found: " << c << std::endl;
            lowerCase++;
        }   
        else if(isdigit(c))
        {
            std::cout << "digit found: " << c << std::endl;
            digit++;
        }   
    }   

    if(lowerCase >= 1 && digit >= 1)
    { 
        std::cout << "found a lowercase character (" << lowerCase << ") or a digit (" << digit << ")" << std::endl;
        lineCount++;
    }

}