我写了一个程序,它读取字符的文本文件字符并计算通过的元音量。但是,如果出现一个符号,说“*”,我希望程序停止计算元音并开始计算非元音,显然从那一点开始继续。我写了一个不同的功能并输入以下内容:
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)显然会使一切草书,我希望我的意思清楚哪一个。
答案 0 :(得分:0)
这是一个简短的例子,因为你知道如何检查字母是否是元音。因为我怀疑这是一个家庭作业问题,所以我要修改它以便为你整个文件工作。
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;
}