Censor Code not Compiling

Hello. I am writing a program that will read a text file containing "sensitive" words followed by a message, and will then censor any sentences containing a "sensitive" word. The code I wrote works in a single file, but upon trying to split it up into multiple .h and .cpp files I get error messages saying that one of my string variables has multiple definitions.

This is the main.cpp code:

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
#include <iostream>
#include <fstream>
#include <string>
#include "message.h"
#include "sensitiveWords.h"

using namespace std;


/*
*censors message in msg
*/
void censorMessage (string& msg)
{
  for (int i = 0; i < msg.length(); ++i)
    {
      if (wordBeginsAt(msg, i))
	{
	  string word = extractWord(msg, i);

	  if (isSensitive(word))
	    censorSentenceAt (msg, i);
	}
    }
}


/*
*reads sensitive words from file stream in
*/
void readSensitiveWords (istream& in)
{
  string word;
  while ((in >> word) && (word != "==="))
    addSensitiveWord (word);
  getline (in, word);
}

/*
*reads message from file stream in
*/
string readMessage (istream& in)
{
  string msg;
  string line;
  getline (in, line);
  while (in)
    {
      msg = msg + line + "\n";
      getline (in, line);
    }
  return msg;
}

int main()
{
  ifstream inFile;

  inFile.open("input.txt");

  readSensitiveWords(inFile);
  string msg = readMessage(inFile);
  censorMessage (msg);
  cout << msg << flush;
  return 0;


These are my .cpp files for message manipulations...
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <fstream>
#include <string>
#include "sensitiveWords.h"
#include "message.h"


/*
*returns true if character is punctuation
*/
std::string toLowerCase (std::string word)
{
  std::string result = word;
  for (int i = 0; i < word.length(); ++i)
    if (word[i] >= 'A' && word[i] <= 'Z')
      result[i] = result[i] + 'a' - 'A';
  return result;
}

bool sentencePunctuation (char c)
{
  return c == '.' || c == '?' || c == '!';
}

/*
*Return true if char is alpha numeric
*/
bool isAlphanumeric (char c)
{
  return (c >= 'A' && c <= 'Z')
    || (c >= 'a' && c <= 'z')
    || (c >= '0' && c <= '9');
}

int findSentenceStop (const std::string& msg, int pos)
{
  while (pos < msg.length()
	 && !paragraphEnd(msg,pos)
	 && !(pos > 0 && sentencePunctuation(msg[pos-1])))
    ++pos;
  return pos;
}

/*
 * Replace all characters of a sentence containing the index pos with '@'
 * except for '/n'.
 */
void censorSentenceAt (std::string& msg, int pos)
{
  int censorStart;
  int endPos;


  endPos = findSentenceStop(msg, pos);
  censorStart = findSentenceStart(msg, pos);

  for (censorStart; censorStart != endPos; censorStart++)
  {
    if(msg.at(censorStart) == '\n')
        {

        }
        else
        {
            msg.at(censorStart) = '@';
        }
  }
}

int findSentenceStart (const std::string& msg, int pos)
{
  while (pos > 0 && !paragraphStart(msg,pos) && !sentencePunctuation(msg[pos]))
    --pos;
  if (sentencePunctuation(msg[pos]))
    ++pos;
  return pos;
}

/*returns true if the index pos has reached the end of a paragraph
*or the end of the message
*/
bool paragraphEnd  (const std::string& msg, int pos)
{
  if (pos == msg.length())
    return true;
  if (pos == msg.length()-1)
    return false;
  return msg[pos] == '\n' && msg[pos+1] == '\n';
}

/*
 * Return true if the character at position pos is the first character
 * of a word.
 */
bool wordBeginsAt (const std::string& message, int pos)
{
  if (pos==0)
    return true;
  else if (!isAlphanumeric(message[pos-1]))
    return true;
  return false;
}


/*
 * Return the word that begins at
 * position beginningAt as a string
 */
std::string extractWord (const std::string& fromMessage, int beginningAt)
{
  std::string wordEx;

  int x = 0;

  while(isAlphanumeric(fromMessage[beginningAt+x]))
    {
      x++;
    }
    wordEx = fromMessage.substr(beginningAt, x);

  return wordEx;
}

/*
 * Returns true if pos is the index of the beginning of the message
 *or if pos is the index of the beginning of a paragraph.
 */
bool paragraphStart  (const std::string& msg, int pos)
{
  if (pos == 0)
    return true;
  if (pos == 1)
    return false;
  return msg[pos-1] == '\n' && msg[pos-2] == '\n';
}


and for adding sensitive words:
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
#include <iostream>
#include <fstream>
#include <string>
#include "message.h"
#include "sensitiveWords.h"

int numSensitiveWords = 0;

bool isSensitive(std::string& wordFromMessage)
{
  std::string wordx = toLowerCase(wordFromMessage);
  for (int i = 0; i < numSensitiveWords; ++i)
    if (wordx == sensitiveWords[i])
      return true;
  return false;
}

/*
*adds a word to the list of sensitive words, if it is not already
*in the list of Sensitive words
*/
void addSensitiveWord (std::string& wordFromMessage)
{
  std::string word = toLowerCase(wordFromMessage);
  if (!isSensitive(word))
    {
      sensitiveWords[numSensitiveWords] = word;
      ++numSensitiveWords;
    }
}


And these are my two header files:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef MESSAGE_H_INCLUDED
#define MESSAGE_H_INCLUDED

const int MaxSensitiveWords = 5000;
std::string sensitiveWords[MaxSensitiveWords];

std::string toLowerCase (std::string xword);
int findSentenceStop (const std::string& msg, int pos);
void censorSentenceAt (std::string& msg, int pos);
int findSentenceStart (const std::string& msg, int pos);
bool paragraphEnd  (const std::string& msg, int pos);
bool isAlphanumeric (char c);
bool sentencePunctuation (char c);
bool wordBeginsAt (const std::string& message, int pos);
std::string extractWord (const std::string& fromMessage, int beginningAt);
bool paragraphStart  (const std::string& msg, int pos);


#endif // MESSAGE_H_INCLUDED 


1
2
3
4
5
6
7
8
9
#ifndef SENSITIVEWORDS_H_INCLUDED
#define SENSITIVEWORDS_H_INCLUDED

bool isSensitive(std::string& wordFromMessage);
void addSensitiveWord (std::string& wordFromMessage);



#endif // SENSITIVEWORDS_H_INCLUDED 


The main problems are with the functions "isSensitive" and "toLowerCase, as the compiler says that there are multiple definitions of "sensitiveWords" relating to these functions.

Any help would be greatly appreciated!
closed account (48T7M4Gy)
Put the files back the way they were and make sure everything still runs. Then, one step at a time, make a single change and test.

BTW: Your code appears ultra-complex to do what appears from your description to be a very simple job. Searching a text file for a set of words against a separate updateable key word list is not a big job.
Topic archived. No new replies allowed.