C++ 在从该点继续读取文件时结束函数

C++ 在从该点继续读取文件时结束函数,c++,file,C++,File,我已经写了一个程序,它读取一个字符对一个字符的文本文件,并计算通过的元音数量。但是,如果出现一个符号,比如*,我希望程序停止计算元音,开始计算非元音,显然从这一点开始继续。我编写了一个不同的函数,并输入了以下内容: if (kar == '*'){ reversecounting(i); return 0; } 其中reversecounting是计算非元音的int函数。 我现在遇到的问题是,每当它这样做时,计算非元音的函数都从文件的开头开始。有没有办法让它从另

我已经写了一个程序,它读取一个字符对一个字符的文本文件,并计算通过的元音数量。但是,如果出现一个符号,比如*,我希望程序停止计算元音,开始计算非元音,显然从这一点开始继续。我编写了一个不同的函数,并输入了以下内容:

if (kar == '*'){
        reversecounting(i);
        return 0;
}
其中reversecounting是计算非元音的int函数。 我现在遇到的问题是,每当它这样做时,计算非元音的函数都从文件的开头开始。有没有办法让它从另一个函数停止的地方继续

我正在使用get函数读取文件。提前谢谢

编辑:很抱歉没有包括相关的代码,我现在已经添加了它和一些注释,使它可以理解

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int countnonvowels( int &k ); //prototype

int countvowels( int k ){ //start function to count vowels
ifstream input; //to open textfile
char kar;      // this is the character that will be read
int countvowels = 0; //local counter of vowels


input.open ("input.txt",ios::in); //opening the textfile


    kar = input.get ( ); //first obtain a character from the textfile
    while ( ! input.eof ( ) ) { //reads the file untile end of file
        kar = input.get ( ); //the next character in the file gets "read"

      if( kar == 'e' or kar == 'a' or kar == 'i' or kar == 'o' or kar == 'u' ){ //checks whether the character is a vowel
            countvowels++; //local counter goes up by one
            kar = input.get(); //take the next letter and print it
            cout << kar << " " << countvowels << endl;
            countvowels = 0; //value becomes zero again
      }//if
      if (kar == '*'){ //if the character is a "*", I want the non-vowels to be counted and the k-th non-vowel to be printed
            countnonvowels(k);
            return 0; //this function has to stop running
      }//if
    } // while

    input.close ( );




}//countvowels

int countnonvowels(int &k){//counts the non-vowels

ifstream input;
char kar;
int countnonvowels = 0;

input.open ("input.txt",ios::in); //open the file


    kar = input.get ( ); 
    while ( ! input.eof ( ) ) { //same while loop
        kar = input.get ( );

      if( kar != 'e' or kar != 'a' or kar != 'i' or kar != 'o' or kar != 'u' ){ //now i want the counter to roll if it is NOT a vowel
            countnonvowels++;
            cout << kar << endl;
      }
      if (kar == '*'){ //if a "*" appears, I want the vowels to be counted again
            countvowels(k);
            return 0;

      }//if
    } // while

input.close ( );


}//countnonvowels


int main ( ) {

countvowels(2);

    return 0;
} // main
这里的问题是,我得到了一个无休止的循环:文件检查元音,它看到的是一个[星号],开始从文本开始计算非元音,再次看到的是[星号],并开始从文本文件开始计算元音等等


星号shift+8使一切都是草书的,我希望我的意思很清楚

这里有一个简短的例子,说明您知道如何检查字母是否为元音。因为我怀疑这是一个家庭作业问题,所以我将把修改整个文件的任务留给你

   void countLetters(const string& line)
    {
       int vowels, nonVowels;
       vowels = nonVowels = 0;
       bool countingVowels = true;
       for (int position = 0; position < line.length(); position++)
       {
          if (countingVowels && isVowel(line[position])
          {
             vowels++;
          }
          else if (!countingVowels && !isVowel(line[position]))
          {
             nonVowels++;
          } 
          else if (line[position] == '*')
          {
             countingVowels = !countingVowels;
          }
       }
       cout << vowels << " "<< nonVowels<<endl;
    }

传递打开的文件iostream?文件*?对于另一个函数,我只是添加了一点,很抱歉一开始没有这么做@crashmstr这正是我想要的!但是,我如何告诉另一个函数继续获取最后一个函数停止的字符?在这两个函数之外打开文件,然后将输入传递给这些函数,它们将从最后一个函数继续。这确实停止了无休止的循环,但上面所示的非元音计数函数不会响应。我不知道你所说的将输入传递给那些函数是什么意思,你的意思是我应该只使用第二个函数中的input.get而不打开它吗?如果input是main中的一个变量,并且你将ifstream&input作为参数添加到你的函数中,那么在那些函数中你可以使用input.get,而不是从文件的开头重新启动。