-
February 15th, 2011, 02:32 PM
#1
Help with Maps... Couple Errors
I got this assignment for my class. Its to create and write an interactive program to scan and process an input stream of an ordinary text. Then it checks for punctuation marks, and deletes them from the word. I have all of it written but I'm getting a couple errors and I don't know why it wont work.
Code:
using namespace std;
typedef map<string, int> mapType;
void get_words(map<string, int>&);
void print_words(const map<string, int>&);
void clean_entry(const string&, string&);
/****************************************************************
FUNCTION: Main
ARGUMENTS: None
RETURNS: None
NOTES: Calls the subroutine get_words and then get_words
calls clean_entry
****************************************************************/
int main()
{
map<string, int> wordList;
get_words(wordList);
print_words(wordList);
return 0;
}
/****************************************************************
FUNCTION: Void get_words
ARGUMENTS: map string, integer &
RETURNS: None
NOTES: Routine gets a word from the input stream and then
cleans its punctuation marks.
****************************************************************/
void get_words(map<string, int>& wordList)
{
string originalWord, cleanedWord;
while (cin >> originalWord)
{
clean_entry(originalWord, cleanedWord);
if (cleanedWord.length() > 0)
wordList[cleanedWord]++;
cleanedWord = "";
}
}
/****************************************************************
FUNCTION: Void clean_entry
ARGUMENTS: const map string&, string&
RETURNS: nothing
NOTES: Cleans a word from its punctuation marks. The
first argument is the original word from input
stream and the second contains the same word
after cleaning.
****************************************************************/
void clean_entry(const string& source, string& target)
{
int p1 = 0, p2;
while(!isalnum(source[p1]) && p1 < source.length()) p1++;
p2 = p1;
while(isalnum(source[p2])&& p2 < source.length()) p2++;
if( p2 == p1 ) target = "";
else target = source.substr(p1, p2-p1);
return;
}
/****************************************************************
FUNCTION: Void print_words
ARGUMENTS: const map string int &
RETURNS: None
NOTES: Prints the final list of words and their frequencies
It also prints hte number of nonempty words
and the number of distinct words in the input stream
****************************************************************/
void print_words(const map<string, int>& wordList, int nemptyw)
{
int i = 0;
cout << "Nonemmpty words : " << nemptyw << endl;
cout << "Distinct words : " << wordList.size() << endl;
for(mapType::const_iterator it = wordList.begin(); it != wordList.end(); ++it)
{
cout << setw (15);
cout << setiosflags (ios_base::left);
cout << it->first << " - " << it->second << " ";
if (++i >= 3 )
{
i = 0;
cout << endl;
}
}
cout << endl;
}
The Errors:
In function ‘void clean_entry(const std::string&, std::string&)’:
prog3.cc:105: warning: comparison between signed and unsigned integer expressions
prog3.cc:108: warning: comparison between signed and unsigned integer expressions
not sure what that is. When i nano prog3.out it shows...
-bash: prog3.exe: command not found
Anyone can help me out?
-
February 15th, 2011, 02:47 PM
#2
Re: Help with Maps... Couple Errors
Originally Posted by youngyou4
In function ‘void clean_entry(const std::string&, std::string&)’:
prog3.cc:105: warning: comparison between signed and unsigned integer expressions
prog3.cc:108: warning: comparison between signed and unsigned integer expressions
Those are warnings, not errors. You should fix them though; signed/unsigned mismatches can lead to subtle bugs in some situations (for instance, if a loop counter is counting down and the termination condition is "i >= 0", a problem could arise if i is unsigned). The basic issue is that p1 and p2 are signed ints, but source.length() returns an unsigned int. Simply casting it to int will remove the warnings.
-bash: prog3.exe: command not found
That means either you're in the wrong directory, or the program didn't build properly. If you didn't get any errors (only warnings), then the former is more likely.
-
February 15th, 2011, 02:49 PM
#3
Re: Help with Maps... Couple Errors
Are those lines within clean_entry?
source.length() returns a size_t. It's unsigned, p1 & p2 are not.
http://cplusplus.com/reference/string/string/length/
-
February 15th, 2011, 02:50 PM
#4
Re: Help with Maps... Couple Errors
in clean_entry
I changed int p1 = 0, p2;
to unsigned int p1 = 0, p2;
And it got rid of the warnings...
But when i open up prog3.out for the output of my file. It still shows
-bash: prog3.exe: command not found
-
February 15th, 2011, 02:50 PM
#5
Re: Help with Maps... Couple Errors
I would recommend changing from int to size_t instead of casting.
-
February 15th, 2011, 02:59 PM
#6
Re: Help with Maps... Couple Errors
Hmm... I got rid of all the errors and warnings and such. I figured out why its saying that command is not found. I forgot to link and create executable file. But now when i try and create the executable file i get this..
prog3.cc:53: undefined reference to `print_words(std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std:air<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, int> > > const&)'
Not sure what that means and it won't create it. hmm
-
February 15th, 2011, 03:26 PM
#7
Re: Help with Maps... Couple Errors
That one occurs if you declare but don't implement a function you're trying to call. In practice, if you think you did implement it (and I certainly do see a print_words implementation there), then one of two things is usually going on:
1) There's an inconsistency between the definition and declaration, or
2) The linkage of the definition is different than the default (for instance, if you were trying to link in a function compiled as C to a C++ program without specifying "extern "C"").
In this case, the culprit is the first one. See the declaration:
void print_words(const map<string, int>&);
versus your definition:
void print_words(const map<string, int>& wordList, int nemptyw)
{
...
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|