opening and parsing a file in c++ to check for punctuation and to convert to lower case using two functions -


i trying open file , parse file separate functions.

when parse file, want read each line in input file , want ignore punctuation , make lowercase can print out separate file strings no spaces or punctuation.

i have tried implement cannot figure out doing wrong. not getting error output not correct.

this code:

#include <iostream> #include <fstream> #include <ostream> #include <sstream> #include <string>  using namespace std;  void processfile(); void parsefile(ifstream&, ofstream&);  int main() {      //call function open file , process     cout << "processing file" << endl;     processfile();      return 0; }   void processfile() {     string newstring;     ifstream infile;     ofstream outfile;      cout << "opening files" << endl;     // open files     infile.open("infile.txt");     outfile.open("outfile.txt");      cout << "parsing file" << endl;     //parse file capitalization & punctuation     parsefile(infile, outfile);      //close files     outfile.close();     infile.close(); }  void parsefile(ifstream &infile, ofstream &outfile) {     //create , initialize variables     string newstring;;     int = 0;      if(!infile)     {         cout << "error!!! cannot read file.";     }     else     {                 {             //read each line in input file until eof             getline(infile, newstring, '\n');              //parse each string punctuation             while(newstring[i])             {                 if(isalpha(newstring[i])) //check each char in each                                                 //string punctuation                 {                     if(isupper(newstring[i])) //check each string                                                //capitalization                     {                         newstring[i] = tolower(newstring[i]); //convert                                                            //string                                                            //lower case                     }                     outfile << newstring[i]; //output each line file                     cout << newstring[i];                 }                 i++;                 if(newstring[i] == '\n')                     break;             }         } while(!infile.eof());     } } 

this input:

racecar racecar rotator rotor civic red rum, sir, murder! rats live on no evil star. neil, trap! sid part alien! step on no pets. dammit, i’m mad! 

my expected output in input printed in lowercase, without punctuation , spaces

this actual output:

racecarsirismurderraliena 

you need reinitialize after each call getline(), otherwise continue @ offset previous string ended. getline() overwrites newstring every time it's called.

do     {         //read each line in input file until eof         getline(infile, newstring, '\n');         = 0;          //parse each string punctuation 

if compare input output makes sense.

the first string "racecar" parsed without problem. 7.

the next 4 calls getline() strings shorter 7 no new output in outfile.txt.

the call getline() after gets "red rum, sir, murder!", starting 7 "sirismurder" , 24.

the next call getline() gets "rats live on no evil star.", starting 24 "r" , is 26

the last call getline() gets "neil, trap! sid part alien!", starting 26 "alien".


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -