0

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 by smoothe19: n/a

5
Contributors
9
Replies
16
Views
6 Years
Discussion Span
Last Post by mitrmkar
0

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

0

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.

0

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);
}

}
0

When I tried declaring it with a size

string myLine[100]

it shot out 2 errors

0

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 by NathanOliver: n/a

0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.