1 Replies - 6195 Views - Last Post: 16 April 2008 - 03:18 PM Rate Topic: -----

#1 AuntieEm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-March 08

trying to count the number of non-whitespace characters

Posted 16 April 2008 - 02:47 PM

I'm trying to count the number of non-whitespace characters of a filestream input by the user. It compiles just fine. However, when I run the program I run into 2 issues--first when it prints the # of non-whitespace characters it won't count beyond 1. Also, I can't get out of the loop either. Any help would be greatly appreciated! Thanks!

[code]

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

void nonblank(int&,ifstream&);
void numwords(int&,ifstream&);
void numvowels(int&,ifstream&);
void numupper(int&,ifstream&);
//void numpattern();
int avglength(double avg);
//int or void for reprinting the filestream

int main()
{
ifstream infile; //the input filestream to be read
string pattern; //the pattern to be matched in the file
string fname; //variable used to hold actual name of file

int n; //the number of non-whitespace characters
int w; //the number of words in the file
int v; //the number of vowels in the file
int u; //the number of uppercase letters in the file
int pat; //the number of times the pattern occurs in file
double avg; //the average characters per word

cout << "Please enter the file name to be read." << endl;
cin >> fname;
infile.open(fname.c_str());
cout << "Please enter the pattern to be matched." << endl;
cin >> pattern;
cout << " " << endl;
cout << "Pattern: " << pattern << endl;
cout << "Input file: " << fname << endl;
cout << " " << endl;

cout << left;

while(!cin.eof())
{
nonblank(n, infile); //call the function that counts non-whitespace characters
cout << "# non-blank characters: " << n << endl;

/* numwords(w, infile); //call function that counts number of words
cout << "# words: " << w << endl;
numvowels(v,infile); //call function that counts upper and lower case vowels
cout << "# vowels: " << v << endl;

numupper(u, infile); //call function that counts # uppercase letters in file
cout << "# uppercase letters: " << u << endl;

avglength(avg); //call function that computes average length of words
cout << "average characters/word: " << avg << endl;
*/
/*
numpattern();
cout << "# times " << pattern << " found: " << /*THE NUMBER*/ << endl;
*/
}
infile.close(); //command to close the input filestream

return 0;
}

void nonblank(int& n, ifstream& infile)
{
char character;
n = 0;

cin.get(character);
if (character != ' ' || character != '\n')
{
n = n + 1;
}
else
{
n = n + 0;
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: trying to count the number of non-whitespace characters

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: trying to count the number of non-whitespace characters

Posted 16 April 2008 - 03:18 PM

The main problem is that you had a line that looked like this: while (!cin.eof())

cin is used for console input, not file input... you need to use your file name, not cin - so it should be:
while(!infile.eof())

Take a look at this: (I've put it all together instead of splitting it over functions so that you can see what's going on)
while(!infile.eof()) //while not at the end of infile, NOT cin
{
    infile.get(character); //get each character
    if (character != ' ' || character != '\n')
        n += 1; //add one to n
}
n -= 1; //gets rid of one, it always seemed to add one - not quite sure why, sorry =[
cout <<"number of non blank spaces: " << n <<endl; //output n



Hope this helps :)
Was This Post Helpful? 1

Page 1 of 1