我從正在使用的輸入中讀取字母和數字的程序。但我不知道如何實現這個.txt文件。這是我的代碼:從.txt文件讀取字母和數字
#include <iostream>
#include <string>
using namespace std;
int main()
{
char ch;
int countLetters = 0, countDigits = 0;
cout << "Enter a line of text: ";
cin.get(ch);
while(ch != '\n'){
if(isalpha(ch))
countLetters++;
else if(isdigit(ch))
countDigits++;
ch = toupper(ch);
cout << ch;
//get next character
cin.get(ch);
}
cout << endl;
cout << "Letters = " << countLetters << " Digits = " << countDigits << endl;
return 0;
}
我在HW犯了一個錯誤,我想從.txt文件算的話,而不是字母。我在計算單詞時遇到了麻煩,因爲我對單詞之間的空間感到困惑。我怎麼能改變這個代碼來計算單詞而不是字母?我非常感謝幫助。
做一個網上搜索 「C++閱讀文本文件」。嘗試自己做,並在出現具體問題時回到這裏。 – hoijui