The problem is that only the last entered line words are counted.
The other words are not counted
Code:
#include<iostream>
#include<fstream>
#include<conio.h>
void main()
{
char text[50];
ofstream out;
out.open("c:/country.txt",ios::app);
cout<<"\nEnter text\n";
cin.getline(text,50);
out<<text<<"\n";
out.close();
ifstream inp;
const int n=80;
int nol=0,now=0;
char line[n],x[n];
inp.open("c:/country.txt");
cout<<"\nDisplay all records\n\n";
if(inp.good())
{
cout<<"Working";
}
while(inp)
{
inp.getline(line,80);
cout<<line<<endl;
nol++;
strcpy(x,line);
for(int i=0;i<80;i++)
{
if(x[i]==' '||x[i]=='\n')
{
now+=1;
}
}
}
inp.close();
cout<<"\nTotal number of lines = "<<nol-1<<"\n";
cout<<"\nTotal number of words = "<<now-1<<endl;
getch();
}
If at runtime i enter the words
the word counter outputs only 2 and not all the words from the file, even though the file contains many more words.
I check
Code:
if(inp)
{
cout<<"Working";
}
But "working" is never output. However when I try
Code:
if(inp.good())
{
cout<<"Working";
}
The word "working" is displayed on the screen.
Please help
Thanks