CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Help with Maps... Couple Errors

  1. #1
    Join Date
    Nov 2010
    Posts
    23

    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?

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help with Maps... Couple Errors

    Quote Originally Posted by youngyou4 View Post
    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.

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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/
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  4. #4
    Join Date
    Nov 2010
    Posts
    23

    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

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Help with Maps... Couple Errors

    I would recommend changing from int to size_t instead of casting.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  6. #6
    Join Date
    Nov 2010
    Posts
    23

    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

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)