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??

Member Avatar for jencas

1. Why are you assigning strings to bools?

wordfound = true;

or

wordfound = false;

2. if (wordfound = "FALSE") is an assignment and no comparison!!!
with respect to 1. rewrite this to

if (!wordfound)

3. why has wordlist a size of 100 but the index in the for loop addresses a maximum of 20 items in this list?

4. your algorithm does not work properly, you need a second wordlist of size 20 to remember the words found

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.