You are here:

C++/text file processing

Advertisement


Question
Could you please take a look at my code?
I cannot figure out why my code is displaying incorrect outputs.

My infile looks like this:
This is a test of the ((~!program...),
let's see what haPPens r2D2-23skidOO!!!
WHEN some ****data is
processed#!%% THE END

My code looks like this:
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;

int main()
{
 ifstream infile;
 string filename;
 string word;
 char char_match, ch;
 int nonwhitespace_count;
 int vowel_count = 0;
 int digit_count = 0;
 int letter_count = 0;
 int symbol_count = 0;
 int char_match_count = 0;

 cout << "Please enter input file name" << endl;
 cin >> filename;
 cout << "Please enter a character to be matched" << endl;
 cin >> char_match;
 cout << "Input file: " << filename << endl;
 cout << "Character to match: " << ch << endl << endl;

 infile.open(filename.c_str());
 infile >> word;
 while (!infile.eof())
   {    
     int length = word.length();
     int i;
     nonwhitespace_count = nonwhitespace_count + word.length();
     
     for (i=0; i<=length; i++)
     infile.get(ch);
     
     if (ch==char_match)
       char_match_count++;
     else if (isdigit(ch))
       digit_count++;
     else if (isalpha(ch))
       {
         letter_count++;
         ch == tolower(word[i]);
         if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch == 'y')
         vowel_count++;
       }
     else
       symbol_count++;

     infile >> word;
   }
 cout << "Nonwhitespace characters: " << letter_count + symbol_count + digit_count + char_match_count << endl;
 cout << "Vowels: " << vowel_count << endl;
 cout << "Letters: " << letter_count + char_match_count << endl;
 cout << "Digits: " << digit_count << endl;
 cout << "Length of longest word(s):" << endl;
 cout << "Times " << char_match << " was found: " << char_match_count << endl;

 infile.close();
 return 0;
}

My output looks like this:
Please enter input file name
myinput
Please enter a character to be matched
p
Input file: myinput
Character to match:

Nonwhitespace characters: 8
Vowels: 3
Letters: 5
Digits: 1
Length of longest word(s):
Times p was found: 0

Instead of this:
Nonwhitespace characters: 104
Vowels: 28
Letters: 78
Digits: 4
Length of longest word(s): 13
Times p was found: 2

I'm pretty sure the error is in my while loop.
Could you please help me? Thanks.

Answer
> I'm pretty sure the error is in my while loop.
yes, your while loop has a subtle error in handling end of file.
you need change it from

  while (!infile.eof())
  {
        ...    
        infile >> word;
  }

to

  while ( infile >> word )
  {
        ...    
  }
there are other, more serious, errors:
a. variable nonwhitespace_count is not initialized.
b. in the for loop, for (i=0; i <= length; i++), there is an off-by-one error. change it to for (i=0; i < length; i++)
c. braces are missing for the block for the for-loop.
d. in the loop, you are reading a character from the file infile.get(ch); instead, process the characters that you have already read. ch = word[i] ;
e. ch == tolower(word[i]); has no effect. should be ch = tolower(word[i]);

with these changes, you should be ok.

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;

int main()
{
ifstream infile;
string filename;
string word;
char char_match, ch;
int nonwhitespace_count = 0 ; // initialize
int vowel_count = 0;
int digit_count = 0;
int letter_count = 0;
int symbol_count = 0;
int char_match_count = 0;

cout << "Please enter input file name" << endl;
cin >> filename;
cout << "Please enter a character to be matched" << endl;
cin >> char_match;
cout << "Input file: " << filename << endl;
cout << "Character to match: " << /*ch*/ char_match << endl << endl;

infile.open(filename.c_str());
// infile >> word;
while ( /* !infile.eof() */ infile >> word)
  {
    int length = word.length();
    int i;
    nonwhitespace_count = nonwhitespace_count + word.length();

    for (i=0; i /*<= */ < length; i++)
    {   // added
    // infile.get(ch);
    ch = word[i] ;
    if (ch==char_match)
      char_match_count++;
    else if (isdigit(ch))
      digit_count++;
    else if (isalpha(ch))
      {
        letter_count++;
        /* ch == tolower(word[i]); */     ch = tolower( word[i] )  ;
        if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch == 'y')
         vowel_count++;
      }
    else
      symbol_count++;
    }

    //infile >> word;
  }     // added
cout << "Nonwhitespace characters: " << letter_count + symbol_count + digit_count + char_match_count << endl;
cout << "Vowels: " << vowel_count << endl;
cout << "Letters: " << letter_count + char_match_count << endl;
cout << "Digits: " << digit_count << endl;
cout << "Length of longest word(s):" << endl;
cout << "Times " << char_match << " was found: " << char_match_count << endl;

infile.close();
return 0;
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.