结束函数,同时从该点继续读取文件
Ending a function while continuing from that point reading a file
我写了一个程序,它读取一个字符一个字符的文本文件,并计算通过的元音数量。但是,如果出现一个符号,比如"*",我希望程序停止计算元音,并开始计算非元音,显然从该点开始继续。我写了一个不同的函数,输入如下:
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;
}
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- 使用新行和不使用新行读取文件
- 读取文件并输入到矢量中
- 循环后如何继续阅读
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 如何继续读取不同功能中的文件,而不是从头开始再次读取文件?
- 失败后可以继续从流读取吗?
- 如何从文件中读取,然后继续从cin读取?
- 控制台将继续关闭,而不是读取 if 语句中的字符串
- 如何只要用户愿意就继续读取输入
- 如何使用 ifstream 打开文件并继续读取直到最后
- C++流继续读取相同的前 5 行
- 如何从文件中读取,直到读取到一个字符,然后跳过一些字符,再次继续读取
- C++在eof()之后继续读取文件
- 分部分读取文件,在最后读取的部分之后继续
- 结束函数,同时从该点继续读取文件
- 程序不会在输入文件中继续读取它
- 从 arduino 读取到 c++ 程序时继续接收 NULL