Thread: Problem with getchar and whitespace/punctuation

  1. #1
    Registered User
    Join Date
    Sep 2007

    Problem with getchar and whitespace/punctuation

    I am trying to write a "censor" program that will use a "message" file and a "words" file that contains the banned words. It should replace all the banned words with the the first letter followed by asterisks to indicate the word's length. I am having trouble trying to get the punctuation and spaces seperate from the words. here's what I've got:

    #include <string>
    #include <fstream>
    #include <vector>
    #include <iomanip>
    #include <sstream>
    #include <iostream>
    #include <iterator> 
    using namespace std;
    int main()
        char ch;
        string word="";
        ifstream messagefile("myfile.txt");
        ofstream out("outfile.txt");
        int bannedcount =0;
        std::vector<std::string> bannedfile;
        std::string line;
        std::ifstream infile ("words.txt", std::ios_base::in);
        while (infile >> line)
            bannedfile.push_back (line);
            bannedcount ++;
        while( messagefile.get(ch) )
               word += ch;
           while ( !ispunct(ch) && !isspace(ch));
           for (int h=0; h < bannedcount ; h++)
                 if (word== bannedfile[h])
                       cout <<"Changing " << bannedfile[h] << endl;
                       for(int i = 1; i < bannedfile[h].length(); i++)
                              word[i] = '*';
           out << word;
           string spaces="";
               spaces =+ ch;
           while ( ispunct(ch) || isspace(ch));
           out << spaces;
        system("del myfile.txt");
        int result;
        char oldname[] ="outfile.txt";
        char newname[] ="myfile.txt";
        result= rename( oldname , newname );
        if ( result == 0 )
           puts ( "File successfully renamed" );
           perror( "Error renaming file" );
           system ("pause");

    Anyone see the problem?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    What exactly is or is not happening? The better your description of the problem, the higher the likelihood of useful replies . . . .

    bannedcount is redundant -- bannedfile.size() is pretty much exactly the same thing.

    You should include <cctype> or <ctype.h>, because you're using isspace() and so on. system() and I believe rename() and perror() are in <cstdlib>/<stdlib.h> as well, and I know that puts() is in <cstdio>/<stdio.h>.
    Last edited by dwks; 04-09-2008 at 02:10 PM.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  3. problem with parser code
    By ssharish2005 in forum C Programming
    Replies: 2
    Last Post: 12-02-2005, 07:38 AM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM