How to count am,is,are (to be) from a text file?
Dec 1, 2015 at 8:33pm UTC
Hey guys, I need a program which counting am,is,are from a text file.
I find lots of program which is counting vowels from in a text file
But when i use am to any vowel place it gives me to this error.
[Warning] multi-character character constant [-Wmultichar]
This is the codes when i trying to change. Please help me..
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
#include <iostream>
#include<fstream>
using namespace std;
int main( )
{
fstream file;
char c;
int nNum = 0, x=0, y=0, z=0;
file.open("words.txt" );
while (!file.eof())
{
file.get(c);
if (c == 'a' || c == 'e' || c == 'o' || c == 'i' || c == 'u' )
nNum++;
if (c == 'a' )
x++;
if (c == 'e' )
y++;
if (c == 'o' )
z++;
}
file.close();
cout << nNum <<endl;
cout<<"Number of a :" <<x<<endl;
cout<<"Number of e :" <<y<<endl;
cout<<"Number of o :" <<z<<endl;
return 0;
}
Last edited on Dec 1, 2015 at 8:33pm UTC
Dec 1, 2015 at 8:40pm UTC
use strings instead of single char's as in line 8
Dec 1, 2015 at 11:22pm UTC
Something like this?
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
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_map>
int main()
{
using dictionary = std::unordered_map<std::string, unsigned >;
dictionary words_to_track =
{
{"am" , 0}, {"be" , 0}, {"are" , 0},
{"was" , 0}, {"were" , 0}, {"been" , 0}
};
std::ifstream in("words.txt" );
std::string token;
while (in >> token)
{
if (words_to_track.count(token))
++words_to_track[token];
}
for (auto & pair : words_to_track)
std::cout << pair.first << ": " << pair.second << '\n' ;
}
Untested. Doesn't make any attempt to handle variations in case or contractions.
Topic archived. No new replies allowed.