Count words and chars from a file
Dec 5, 2019 at 9:04pm UTC
So basically the problem I have is that when it counts the words then it no longer counts the chars , (which ever function I put first it only does that one and it makes the second one zero )
Can anyone help me out
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
void getfileName(ifstream&);
void countwords(ifstream&);
Void countchars(ifstream&);
int main()
{
ifstream infile;
getfileName(infile);
countwords(infile);
countchars(infile);
return 0;
}
void getfileName(ifstream& infile)
{
string name;
cout << " Please enter file name (including path of file):" ;
cin >> name;
infile.open(name);
if (!infile)
{
cout << "there is a error going on with your file" ;
exit(0);
}
}
void countwords(ifstream& infile)
{
int numw;
numw = 0;
string word;
while (infile)
{
infile >> word;
numw++;
}
cout << numw;
}
void countchars(ifstream& infile)
{
int numc = 0;
char chars;
while (infile)
{
infile >> chars;
numc++;
}
cout << endl << numc;
} Put the code you need help with here.
Last edited on Dec 5, 2019 at 9:24pm UTC
Dec 5, 2019 at 9:30pm UTC
Close the file and reopen it so you start counting at the beginning again.
Dec 5, 2019 at 9:42pm UTC
wow I didn't realize
it works now Thank you
Topic archived. No new replies allowed.