Question

I need to add something to this C++ program.Additionally I want it to remove 10 words...

I need to add something to this C++ program.Additionally I want it to remove 10 words from the printing list (Ancient,Europe,Asia,America,North,South,West ,East,Arctica,Greenland)

#include <iostream>
#include <map>
#include <string>
#include <cctype>
#include <fstream>
#include <iomanip>

using namespace std;
void addWord(map<std::string,int> &words,string s);
void readFile(string infile,map<std::string,int> &words);
void display(map<std::string,int> words);
  
int main() {
// Declaring variables
std::map<std::string,int> words;
  
  
//defines an input stream for the data file
ifstream dataIn;

  
string infile;
cout<<"Please enter a File Name :";
cin>>infile;
readFile(infile,words);
  
display(words);

  
return 0;
}
void readFile(string infile,map<std::string,int> &words)
{
string s;
//defines an input stream for the data file
ifstream dataIn;
//Opening the input file
dataIn.open(infile.c_str());
//checking whether the file name is valid or not
if(dataIn.fail())
{
cout<<"'"<<infile<<"' File Not Found **";
exit(0);
}
else
{
//counting how many words in the file
while(dataIn>>s)
{
addWord(words,s);
}
dataIn.close();
}
}
void addWord(map<std::string,int> &words,string s)
{
char c;
string str="";
for(unsigned int i=0;i<s.length();i++)
{
c=s[i];
if(isalpha(c))
str.push_back(std::toupper(c));
}
++words[str];
}
void display(map<std::string,int> words)
{

cout<<"---------------------"<<endl;
cout<<"Word Counter Summary "<<endl;
cout<<"---------------------"<<endl;
cout<<"\nWord\t\tCount"<<endl;
cout<<"------\t\t-----"<<endl;
  
for(std::map<std::string,int>::iterator iter = words.begin(); iter!=words.end(); ++iter)
{
cout<<setw(16)<<left<<iter->first<<setw(5)<<right<<iter->second<<std::endl;
}
  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

hI, i HAVE MODIFIED THE CODE AS PER YOUR REQUIREMENT ( WITH COMMENTS) & it works perfectly.

Please do upvote

File Edit Selection View Go Run Terminal Help • rough.cpp - Visual Studio Code é one.py 1 3 4 go rough.java G+ rough.cpp Java

ifstream datan; //opening the input file dataIn.open(infile.c_str()); //checking whether the file name is valid or not if (da

File Edit Selection View Go Run Terminal Help rough.cpp - Visual Studio Code é one.py rough.java G rough.cpp X Java Overview

#include <iostream>

#include <map>

#include <string>

#include <cctype>

#include <fstream>

#include <iomanip>

using namespace std;

void addWord(map<std::string, int> &words, string s);

void readFile(string infile, map<std::string, int> &words);

void display(map<std::string, int> words);

int main()

{

    // Declaring variables

    std::map<std::string, int> words;

    //defines an input stream for the data file

    ifstream dataIn;

    string infile;

    cout << "Please enter a File Name :";

    cin >> infile;

    readFile(infile, words);

    display(words);

    return 0;

}

void readFile(string infile, map<std::string, int> &words)

{

    string s;

    char option;

    int a = 1;

    string word;

    //defines an input stream for the data file

    ifstream dataIn;

    //Opening the input file

    dataIn.open(infile.c_str());

    //checking whether the file name is valid or not

    if (dataIn.fail())

    {

        cout << "'" << infile << "' File Not Found **";

        exit(0);

    }

    else

    {

        //counting how many words in the file

        while (dataIn >> s)

            addWord(words, s);

        dataIn.close();

    }

    //do you want to add something

    cout << "do you want to add something(Y/N): ";

    cin >> option;

    if (option == 'Y' || option == 'y')

        while (a)

        {

            cout << "Enter your word: ";

            cin >> word;

            addWord(words, word);

            cout << "Enter 0 to stop & press 1 to add more words: ";

            cin >> a;

        }

}

void addWord(map<std::string, int> &words, string s)

{

    char c;

    string str = "";

    for (unsigned int i = 0; i < s.length(); i++)

    {

        c = s[i];

        if (isalpha(c))

            str.push_back(std::toupper(c));

    }

    ++words[str];

}

void display(map<std::string, int> words)

{

    cout << "---------------------" << endl;

    cout << "Word Counter Summary " << endl;

    cout << "---------------------" << endl;

    cout << "\nWord\t\tCount" << endl;

    cout << "------\t\t-----" << endl;

    for (std::map<std::string, int>::iterator iter = words.begin(); iter != words.end(); ++iter)

    {

        //I want it to remove 10 words from the printing list

        if (iter->first !="ANCIENT" || "EUROPE" || "ASIA" || "AMERICA" || "NORTH" || "SOUTH" || "WEST" || "EAST" || "ARCTICA" || "GREENLAND")

            cout << setw(16) << left << iter->first << setw(5) << right << iter->second << std::endl;

    }

}

PLEASE LIKE IT RAISE YOUR THUMBS UP
IF YOU ARE HAVING ANY DOUBT FEEL FREE TO ASK IN COMMENT SECTION

Add a comment
Know the answer?
Add Answer to:
I need to add something to this C++ program.Additionally I want it to remove 10 words...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to make a few changes to this C++ program,first of all it should read...

    I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...

  • I want to change this code and need help. I want the code to not use...

    I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...

  • The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...

    The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...

  • Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters...

    Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters student first name, 10 characters middle name, 20 characters last name, 9 characters student ID, 3 characters age, in years (3 digits) 2- Open the input file. Check for successful open. If the open failed, display an error message and return with value 1. 3- Use a pointer array to manage all the created student variables.Assume that there will not be more than 99...

  • I have 2 issues with the C++ code below. The biggest concern is that the wrong...

    I have 2 issues with the C++ code below. The biggest concern is that the wrong file name input does not give out the "file cannot be opened!!" message that it is coded to do. What am I missing for this to happen correctly? The second is that the range outputs are right except the last one is bigger than the rest so it will not output 175-200, it outputs 175-199. What can be done about it? #include <iostream> #include...

  • Objectives: The main objective of this assignment is checking students’ ability to implement membership functions. After...

    Objectives: The main objective of this assignment is checking students’ ability to implement membership functions. After completing this assignment, students will be able to:  implement member functions  convert a member function into a standalone function  convert a standalone function into a member function  call member functions  implement constructors  use structs for function overloading Problem description: In this assignment, we will revisit Assignment #1. Mary has now created a small commercial library and has managed...

  • Hello I need a small fix in my program. I need to display the youngest student...

    Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...

  • C++ Programming question For this exercise, you will receive two string inputs, which are the names...

    C++ Programming question For this exercise, you will receive two string inputs, which are the names of the text files. You need to check if the input file exists. If input file doesn't exist, print out "Input file does not exist." You are required to create 4 functions: void removeSpace (ifstream &inStream, ofstream &outStream): removes white space from a text file and write to a new file. If write is successful, print out "Text with no white space is successfully...

  • C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one...

    C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one file, and output the result to another file. But i am confused about how to remove whitespace. i need to make a function to do it. It should use string, anyone can help me ? here is my code. #include <iostream> #include <iomanip> #include <cstdlib> #include <fstream> using namespace std; void IsFileName(string filename); int main(int argc, char const *argv[]) { string inputfileName = argv[1];...

  • Directions: Develop a C++ program that can solve any matrix, up to 15 equations with 15...

    Directions: Develop a C++ program that can solve any matrix, up to 15 equations with 15 unknowns. Put the result on the screen as well as write the results to a text file This is what we were given as a hint: /* how to read matrix data file in rows and colloms written by tom tucker 03/26/2018 it uses a string array to read in the first line and then a nested for statement to read in the matrix...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT