Basically comparing what i had so far to what you have, yours is very complicated. Are you able to like add onto my program to make it count different words?
#include <iostream>
#include <string>
usingnamespace std;
void readdata(string [],int &);
void diffwords(); \\This is what i need help with
int main()
{
return 0;
}
void readdata(string lines[],int &n) \\reads in a string line by line
{
n = 0;
getline(cin,lines[n]);
while(cin){
n++;
getline(cin,lines[n]);
}
return;
}
void diffwords(string lines[],int &n) \\This is what i need help with
{
return;
}
So after you enter a sentence, in the function diffwords would count how many words are the same and how many words that are different.
you wouldnt. you would do that (or at least a variation of it) if you were writing c code or knew the size you wanted, then it would work. but you dont need it with my code
in the main() function would be OK!
because cin >> facility reads only one word at a time, so it happens to meet ur request.
and remember to add a cout << count; behind to see the output : P
If i type in one one one one, it should count one 4x but if i type in one two three four, it should count one 1x, two 1x, three 1x, and four 1x. How do i do this?
You need a container to store words you've read in. As you read in words you should check to see if they are already in the container. If the word is already in the container, increase the count associated with the entry. If the word is not already in the container, put the word in the container and associate it with a count of 1.
Using std::map is a convenient way to do this, as DTSCode illustrated (although the helper functions, IMO, didn't increase readability and made the code less efficient.)
#include <iostream>
#include <string>
#include <cctype>
#include <map>
// utility function for converting a string to lower case.
std::string as_lower(const std::string& text)
{
std::string result;
for (auto letter : text)
result += static_cast<char>(std::tolower(letter));
return result;
}
int main()
{
// a map is an associative container. In this case a word whose type is
// std::string is associated with a count whose type is unsigned
// http://www.cplusplus.com/reference/map/map/
std::map<std::string, unsigned> words;
// using the words["word"] notation will cause an entry to be created for
// "word" if one doesn't already exist, with a count of 0. A reference to
// an existing count will be returned by the call.
// http://www.cplusplus.com/reference/map/map/operator%5B%5D/
std::string word;
while (std::cin >> word) // while a word is succesfully extracted,
words[as_lower(word)]++; // increase the count associated with it.
// print the words and associated counts, w here will refer to a std::pair
// where the first member refers to the key which is the word and the second
// member refers to the data which is the count in this case.
// http://www.cplusplus.com/reference/utility/pair/for (auto& w : words)
std::cout << '"' << w.first << "\": " << w.second << '\n';
}