结束函数,同时从该点继续读取文件

Ending a function while continuing from that point reading a file

本文关键字:继续 读取 文件 函数 结束      更新时间:2023-10-16

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

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;
    }