Here is the code I have but it refuses to remove non letters,digits being read from a file line by line then each word in a line still what is wrong with it?

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
#include <cctype>
#include <functional>
#include "BST.h"

using namespace std;



//Processword method
string processWord(string x)
{

// Removes all punctuation		 
	  x.erase(
      std::remove_if(x.begin(), x.end(), &ispunct), 
      x.end());

	   //Remove all numbers
	  x.erase(
      std::remove_if(x.begin(), x.end(), &isdigit), 
      x.end());

	//Change all in string to uppercase
	  transform(x.begin(),x.end(),x.begin(),toupper);	
	  //Remove all non letter characters and numbers





	   return x;


}

Edited 6 Years Ago by smoothe19: n/a

It works fine for me:

std::string MyString = "hello,world123";
  std::cout << "Original: " << MyString << std::endl;
      
  //Remove all punctuation
  MyString.erase(
  std::remove_if(MyString.begin(), MyString.end(), &ispunct), 
  MyString.end());
  
  std::cout << "Punctuation removed: " << MyString << std::endl;

  //Remove all numbers
  MyString.erase(
  std::remove_if(MyString.begin(), MyString.end(), &isdigit), 
  MyString.end());

  std::cout << "Numbers removed: " << MyString << std::endl;

the end result is simply

helloworld

Dave

It does work but when the word is being read from a file and then passed into that method it does not properly remove the punctuation.

Could you post the part of the code where you read from the file and then call the function

Here it is

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
#include <cctype>
#include <functional>
#include "BST.h"

using namespace std;



//Processword method
string processWord(string x)
{
	//Change all in string to uppercase
	  transform(x.begin(),x.end(),x.begin(),toupper);	
	  //Remove all non letter characters and numbers
	  x.erase(
      std::remove_if(x.begin(), x.end(), &isdigit), 
      x.end());




	   return x;


}


//Main function
int main () 
{
	//Define filename and then ask user for name of file in ".txt" format
	string filename;
	cout << "Enter the name of the file you wish to access: .txt format " << endl;
	cin >> filename;

	ifstream file(filename.c_str());

	//If file was not found output that
	if (!file) 
	{
		cout << "The file " << filename << " was not found " << endl;
		return 1;		
	}

	//IF file found print out opening file requested
	cout << endl << "Opening file requested" ; 

	//define myline and linenum
	string myLine;
	int lineNum = 1;
	BST<string> concord[27];

	//While file is good get the line then one word at a time
	while (file.good())
	{
		getline(file, myLine); //Get line
		stringstream readfile(myLine); // One at a time


		while (readfile.good()) 
		{
			//Read the words into the string word
			string word;
			readfile >> word;

			if (isalpha(word[0])) 
			{

				//Process the word
				word = processWord(word);
				concord[(word[0] - 'A')].insert(word, lineNum);
				cout << " " << endl; 
				
			}

			else 
			{
				concord[26].insert(word, lineNum);
			}
		}
		lineNum++;
	}

for (int i = 0; i < 27; i++)
{
	cout << "   " << endl;
	concord[i].graph(cout);
}

}

When I tried declaring it with a size

string myLine[100]

it shot out 2 errors

you need to pass the string by reference into your function

string processWord(string & x)
{
	//Change all in string to uppercase
	  transform(x.begin(),x.end(),x.begin(),toupper);	
	  //Remove all non letter characters and numbers
	  x.erase(
      std::remove_if(x.begin(), x.end(), &isdigit), 
      x.end());




	   return x;


}

Edited 6 Years Ago by NathanOliver: n/a

It does work but when the word is being read from a file and then passed into that method it does not properly remove the punctuation.

Looking at the code you've posted (post #6), I notice that you've removed the following line ..

x.erase(std::remove_if(x.begin(), x.end(), &ispunct), x.end());

so, would that explain this behaviour or did you post wrong code?

This article has been dead for over six months. Start a new discussion instead.