My professor has assigned a machine problem that asks us to write a program using cin.getline, strcmp_s, and strcpy_s to get a string of words and then count how many times those words were used in the sentence. I have searched online for a solution to my problem but I'm still not sure what is going on so.. This forum is my last resort. I would appreciate ANY reply to this thread whether it answers my question or points out something else I've done wrong. My question will be at the end of the posting of my code. Thank you so much for your time as I know how valuable it is.
include <iostream>
#include <cstring>
using namespace std;
int main()
{
char wordlist[100];
char wordCount[20] = {0};
char tok_vals[] = " \n\t";
char wordToFind;
char foundWords = 0;
bool wordFound;
char * next_token;
char * p;
cout << "Please enter a phrase: " << endl;
cin.getline(wordlist, 100);
p = strtok_s(wordlist, tok_vals, &next_token);
while (*p != NULL)
{
p = strtok_s(NULL, tok_vals, &next_token);
wordToFind = *p;
wordFound = "FALSE";
for(int i = 0; i < 20; i++)
{
if(wordToFind == wordlist[i])
{
wordCount[i]++;
wordFound = "TRUE";
}
}
if(wordFound = "FALSE")
{
wordlist[foundWords]++;
strcpy_s((char *)foundWords, sizeof(foundWords), (char *)wordToFind);
wordCount[foundWords] = 1;
}
}
for(int i = 0; i < wordCount[foundWords]; i++)
{
cout<< wordlist[i] << wordCount[i];
}
cout <<endl;
return 0;
}
After all of this... I get an assertion failure. I have read about what it is but I'm not sure how to fix it. Does anyone have any suggestions??