Question

In: Computer Science

Map word scanning in C++ For some context, I've been working on a program in which...

Map word scanning in C++

For some context, I've been working on a program in which I must enter in words and clear them of any potential punctuations. If a word has punctuation in the middle of the word, I must ignore the letters AFTER the punctuation and delete the punctuation as well. For example if I enter in "fish_net" I will only get "fish" back. I must also keep track of the occurrence of each word as well as number of words.

Here's my problem, I'm using a do while loop to continuously enter in words and break from it when needed. When I do, my output is supposed to be separate words, but they are getting merged with each other and counting it as one word. For example, lets say I enter in the words: "hello" "there" "done". The INTENDED output should be the following: hello 1 (newline) there 1 (newline) done 1. However, the output CURRENTLY goes like this: hello 1 (newline) hellothere 1 (newline) hellotheredone 1. Mind you that the numbers after the words are the number of occurrences that word is entered.

#include <iostream>
#include<map>
#include<string>
#include <algorithm>
using namespace std;

void get_words(map<string, int>&);
void print_words(const map<string, int>&);
void clean_entry(const string&, string&);

int main()
{
   map<string,int>m;
   get_words(m);
   print_words(m);
}

void get_words(map<string, int>&m)
{
   string word, cleaned_words = "";
   cout << "Enter in a string of text: ";
   do
   {  
       cin >> word;
       clean_entry(word, cleaned_words);

       if (cleaned_words.length() != 0)
       {
           m[cleaned_words]++;//inserting clean words into map
       }
  
   } while (word != "done");
}

void print_words(const map<string, int>&m)
{  
   int non_empty_words = 0;
   for (auto it = m.begin(); it != m.end(); it++)
   {
       cout << it->first << " " << it->second << endl;
       non_empty_words += it->second;
   }
   cout << "Non-empty words: " << non_empty_words << endl;
   cout << "Words: " << m.size();
}

void clean_entry(const string&words, string&cleaned_words)
{
   int len = words.length();
   int i = 0;
   while (i < len && ispunct(words[i])) i++;//parse through initial punctuation (make sure that punctuation is deleted while leaving word intact if in front or back)
   while (i < len && !ispunct(words[i]))//while we come across any words with no punctuation, we add it to cleaned words
   {
       cleaned_words += words[i];
       i++;
   }
}

Solutions

Expert Solution

Modified code:

#include <iostream>
#include<map>
#include<string>
#include <algorithm>
using namespace std;

void get_words(map<string, int>&);
void print_words(const map<string, int>&);
void clean_entry(const string&, string&);

int main() {
        map<string,int>m;
        get_words(m);
        print_words(m);
}

void get_words(map<string, int>&m) {
        string word, cleaned_words = "";
        cout << "Enter in a string of text: ";
        do {
                cin >> word;
                cleaned_words = "";
                clean_entry(word, cleaned_words);
                if (cleaned_words.length() != 0) {
                        m[cleaned_words]++;//inserting clean words into map
                }

        } while (word != "done");
}

void print_words(const map<string, int>&m) {
        int non_empty_words = 0;
        for (auto it = m.begin(); it != m.end(); it++) {
                cout << it->first << " " << it->second << endl;
                non_empty_words += it->second;
        }
        cout << "Non-empty words: " << non_empty_words << endl;
        cout << "Words: " << m.size();
}

void clean_entry(const string&words, string&cleaned_words) {
        int len = words.length();
        int i = 0;
        while (i < len && ispunct(words[i])) i++;//parse through initial punctuation (make sure that punctuation is deleted while leaving word intact if in front or back)
        while (i < len && !ispunct(words[i])) { //while we come across any words with no punctuation, we add it to cleaned words
                cleaned_words += words[i];
                i++;
        }
}

Screenshots:

Code output:

=========================

The issue was with the cleaned_words string that was passed as reference each time to clean_entry. Since it was passed by reference each time the same string, new words went on adding to it one after another.

So we need to ensure that it is empty before it is passed.

Line no. 22 does the same thing.

Also please note that, using map we can't ensure the output order to be same as input order. It will always be alphabetically arranged in sorted order.

If you still want the same order consider using another map, that stores the index of each string in the order they are entered.

============================

for any query comment.


Related Solutions

Write a working C++ program to ask the user for a set of grades which are...
Write a working C++ program to ask the user for a set of grades which are to be stored in an array in increasing sequence. The user must first provide the number which represents the count of all grades to be provided. For example, if there will be 10 grades, the user is first prompted to provide the number 10. Subsequently, each grade is provided, one at a time. Allow for a maximum of 30 grades in defining the array....
( USE C++ ) The program prompts the user to enter a word. The program then...
( USE C++ ) The program prompts the user to enter a word. The program then prints out the word with letters in backward order. For example, if the user enter "hello" then the program would print "olleh" show that it works .
in basic c++ program please!! Write a word search and word count program. Assign the following...
in basic c++ program please!! Write a word search and word count program. Assign the following text to a string constant. For God so loved the world that he gave his one and only Son, that whoever believes in him shall not perish but have eternal life. For God did not send his Son into the world to condemn the world, but to save the world through him.Whoever believes in him is not condemned, but whoever does not believe stands...
Language is C# (i've got some code but it seems to not run correctly, would love...
Language is C# (i've got some code but it seems to not run correctly, would love a new take) Create an Employee class with five fields: first name, last name, workID, yearStartedWked, and initSalary. It includes constructor(s) and properties to initialize values for all fields. Create an interface, SalaryCalculate, class that includes two functions: first,CalcYearWorked() function, it takes one parameter (currentyear) and calculates the number of year the worker has been working. The second function, CalcCurSalary() function that calculates the...
Language is C# (i've got some code but it seems to not run correctly, would love...
Language is C# (i've got some code but it seems to not run correctly, would love a new take) Create an Employee class with five fields: first name, last name, workID, yearStartedWked, and initSalary. It includes constructor(s) and properties to initialize values for all fields. Create an interface, SalaryCalculate, class that includes two functions: first,CalcYearWorked() function, it takes one parameter (currentyear) and calculates the number of year the worker has been working. The second function, CalcCurSalary() function that calculates the...
Write a  program in c++ using a map to create the following output. Here is the list...
Write a  program in c++ using a map to create the following output. Here is the list of students: 100: Tom Lee 101: Joe Jones 102: Kim Adams 103: Bob Thomas 104: Linda Lee Enter a student an ID to get a student's name: ID:  103 students[103] - Bob Thomas # of students: 5 Delete a student (Y or N)?  Y Enter ID of student to be deleted:  103 Here is the list of students after the delete: 100: Tom Lee 101: Joe Jones...
C++ Write a word search program that searches an input data file for a word specified...
C++ Write a word search program that searches an input data file for a word specified by the user. The program should display the number of times the word appears in the input data file. In addition, the program should count and display the number of grammatical characters in the input data file. Your program must do this by providing the following function: void processFile(ifstream &inFile, string wordSearch, int &wordCount, int &grammaticalCount); void processFile(ifstream &inFile, string wordSearch, int &wordCount, int...
Create a program in C that counts the number of characters in a word when a...
Create a program in C that counts the number of characters in a word when a user inputs a string. Use stdin to read an input string. For example, if a user inputs: “The dog is good” the output should be a= [The], b=3 a= [dog], b=3 a= [ is], b=2 a= [good], b=4 a= [ ], b=0000 Take into account EOF. If an EOF is reached at the end of the string then the output should be 0000. (example...
Create a program in C that counts the number of characters in a word when a...
Create a program in C that counts the number of characters in a word when a user inputs a string. Use stdin to read an input string. For example, if a user inputs: “The dog is good” the output should be a= [The], b=3 a= [dog], b=3 a= [ is], b=2 a= [good], b=4 a= [ ], b=0000 Take into account EOF. If an EOF is reached at the end of the string then the output should be 0000. (example...
C++ ^ ^ Write a menu driven program to perform following operations using a map container...
C++ ^ ^ Write a menu driven program to perform following operations using a map container that stores the information about USA Population (In Million). @@@Menu@@@ 1. Add Information 2. Display Information 3. Update Information 4. Erase Information 5. Clear Information For example, Suppose the map initially contains following information (Use emplace() function to store the information) 2010, 309.33 2011, 311.58 2012, 313.87 2015, 320.74 2016, 323.07 The program should produce the desired output when a valid input is provided,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT