如何解决不打印任何字符而只计算元音和字符并读取文件中的每个字符的问题

How to fix not printing any character and just count vowels and characters and read every character in a file

我只想读取文件中的每个字符,我在其中放置了 A 到 Z 的字符,但程序每次都打印 A 并计算元音 4 和字符 25,但期望打印元音 5 和字符 26 如何修复该程序在过去 4 小时内修复但没有任何进展? 代码:

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main() {
  int i, count = 0, vowel_count = 0;
  string file_name;
  cout << "enter file name:";
  cin >> file_name;
  ifstream fin;
  fin.open(file_name);
  char ch;
  while (!fin.eof()) {
    fin.get(ch);
    cout << ch;
    while (fin >> ch) {
      i = ch;
      if ((i > 63 && i < 91) || (i > 96 && i < 123))
        count++;
      if (i == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
        vowel_count++;
    }
    cout << "\n No. of Characters in a File : " << count;
    cout << "\n No. of vowel characters in the File  : " << vowel_count;
  }
  fin.close();
  return 0;
}

你的代码中有一些非常小的错误,我已经为你修正了。

此外,我添加了一个检查,文件是否可以打开。这就是大多数情况下的问题。

请看下面:

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main() {
    int count = 0, vowel_count = 0;
    string file_name;
    cout << "\nEnter file name: ";
    cin >> file_name;
    ifstream fin(file_name);
    if (fin) {
        char ch;
        while (fin.get(ch)) {
            cout << ch;
            if ((ch >= 'A' && ch <= 'Z') || (ch > 'a' && ch <= 'z'))
                count++;
            if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
                vowel_count++;

        }
        fin.close();

        cout << "\n No. of Characters in a File : " << count;
        cout << "\n No. of vowel characters in the File  : " << vowel_count;
    }
    else std::cerr << "\n\n*** Error. Could notopen '" << file_name << "'\n\n";
    return 0;
}