Jump to content

Recommended Posts

Posted (edited)

Hello, I'm writing a C++ program that scans emails for words on our school "banned word list".

 

I have got to the point where we can paste text from an email into command line which gets saved in a .txt file called 'emailscan' - this is then read by ifstream and scanned for the chosen word which if found is returned with its location

 

My pickle is that the user cannot paste more than a single paragraph of text.

 

For example if we copy an entire email, which more often than not contains several carriage returns, only the first block of text before the first carriage return is fed into the emailscan.txt file and subsequently only this part of the email is scanned.

 

Is there a way of ignoring/discarding carriage returns or converting text after the cin >> operator in order to merge all the text into one 'paragraph'?

 

Thanks

Michael

 

 

#include "stdafx.h"

#include

#include

#include

using namespace std;

 

 

int _tmain(int argc, _TCHAR* argv[])

{

string line, paragraph, text, textin;

int i, location;

char* word = "virus"; // <--for example

 

// USER TO PASTE IN EMAIL

 

cout << "Paste text here:" << endl;

cin >> text;

cout << "You entered: " << text << endl;

 

getline(cin, text);

cin.ignore('\n');

 

ofstream filewriter;

filewriter.open("emailscan.txt"); // read in text to file called

filewriter << text << endl; // emailscan

 

 

if(!filewriter) // check that emailscan can be opened {

cout << "Error opening file for output" << endl;

return -1;

}

filewriter.close();

 

cout << "The text has been saved in emailscan.txt" << endl; // checkpoint

 

///////////////////////////////////////////////////////////////////////////////////////

 

// OPEN AND READ EMAILSCAN.TXT

 

ifstream filereader("emailscan.txt"); // first, read the text file

 

if( !filereader) // check that emailscan can be opened {

cout << "Error opening file for output" << endl;

return -1;

}

 

for(i = 0; ! filereader.eof(); i++){ // run a loop to read lines

getline(filereader,line); cout << line << endl;

 

 

///////////////////////////////////////////////////////////////////////////////////////

 

// FIND A WORD FOR EXAMPLE 'VIRUS'

 

if ((location = line.find(word, 0)) != string::npos) {

cout << "found \"" << word << "\" at location " << location << endl;

} else filereader.close(); // look for the word and if found state where

}

 

cout << line << endl;

filereader.close();

 

cout << "Iterations:" << i << endl; // how many lines?

 

return 0;

}

Edited by mcheung0
Posted
Hello, I'm writing a C++ program that scans emails for words on our school "banned word list".

 

Now open: StackOverflow, for all your programming questions:

 

Stack Overflow

 

As created by Joel Spolsky:

 

Stack Overflow Launches - Joel on Software

 

And Jeff Atwood:

 

Coding Horror: Stack Overflow: None of Us is as Dumb as All of Us

 

I have got to the point where the user pastes the email which gets saved in a .txt file called 'emailscan' - this is then read by ifstream and scanned for the chosen word which if found is returned with its location

 

Sorry, what's being pasted where, and on what? Are you writing a filter for an email system? Those generally tend to work by taking input on stdin and writing to stdout. Writing to an intermediate text file is going to slow you down somewhat.

 

--

David Hicks

Posted

Hello thanks for both replies -

 

the user copies text from an email and pastes it to a text box (the command line for now)

 

it's not as complicated as streaming directly from email (yet)

 

michael

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...