我的目标是编写一个可以读取文本文件的C ++程序:
将使用命令“./a.out< textfile”
读取文本文件我不明白为什么输出是空白的。
循环运行时,应检查每个字符。如果是一封信,请加上字母数;如果是数字,请加上数字计数;如果它是。,!或?,添加到行数。
用linecount划分字母以找到平均值。
但它不起作用。
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cctype>
using namespace std;
int main()
{
int letters; //Total number of letters
int digits; //Total number of digits
double sentencecount; //Number of sentences
float averageletters; //Average number of letters per sentence
int linecount=0; //Count of lines
char current; //Current character
cin.get(current);
while (cin) //while receiving input
{
digits = 0;
letters = 0;
linecount++;
while (current && '.' || current && '!' || current && '?') //checks each line
{
if (isalpha(current))//counts alphabet characters
letters++;
if (isdigit(current))//counts digits
digits++;
cin.get (current);
}
sentencecount = letters/linecount;
cout << "avg number of letters" << sentencecount << "digits" << digits << endl;
cin.get (current);
}
return 0;
}