i am trying to open an text file and trying to count the numbers of characters and words in the file, i have created while " while (!infile.eof()); " to scan the whole file till the end . However only one of the function is working and other one is also printing the same answer as the first one.
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;
int main()
{
ifstream infile;
infile.open("tomorrow.txt");
int count1_char = 0;
int count2_word = 0;
while (!infile.eof())
{
{
char ch;
infile.get(ch); //number of characters
count1_char++;
}
{
char word[30];
infile >> word; //numner of words
count2_word++;
}
}
cout << " the number of characters :" << count1_char << endl;
cout << " the number of words :" << count2_word << endl;
infile.close();
return 0;
}
the output: the number of characters :17 the number of words :17 Press any key to continue . . .