Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

C++ program need lines words and characters count

I have an assignment where i need to count lines, words and characters from a file. I'm having a problem counting the right amount of characters and words since if it gets doubled space it counts like a character and a word.

the output should be

Example

lines  words  characters  filename

 3     5        29        testfile

Code:

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

int main(int argc, const char * argv[]) {
string lines, words, chars, file1, file2;

ifstream infile;
ofstream outfile;
char c;

int countLines = 0;
int countChars = 0;
int countWords = 0;
cout<< "Enter the file name" << endl;
cin >> file1;
infile.open(file1.c_str());


while(!infile.eof())
{
    if(infile.peek() == 1)
        break;
    c = infile.get();
    if(c != '
')
       countChars++;
    else
        countLines++;

    if(c == ' '|| c =='
')
        countWords++;

}

  //  countChars = countChars - countWords;



cout << setw(12) << countLines << setw(12) << countWords << setw(12) << countChars  << endl;

infile.close();



return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I believe OP's purpose to ask this question is to find out why his/her code is not working, therefore I will answer in this perspective.

counting the right amount of words

  1. C++ define EOF(end of file) as -1, so include a check for EOF too, or you will miss a word count.

if it gets doubled space it counts like a character and a word.

  1. You can use a boolean test to solve this, if you encountered a space, turn on the boolean, and skip if next char is a space, too.

  2. I suppose your character count doesn't count in punctuation? so check for c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'. If your assignment count punctuation as character count too, ignore this point.

Below is a correct version code that is modified based on your code.

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

int main(int argc, const char * argv[]) {
    string lines, words, chars, file1, file2;

    ifstream infile;
    ofstream outfile;
    char c;
    bool findNextString = false;

    int countLines = 0;
    int countChars = 0;
    int countWords = 0;
    cout << "Enter the file name" << endl;
    cin >> file1;
    infile.open(file1.c_str());


    while (!infile.eof())
    {
        if (infile.peek() == 1)
            break;
        c = infile.get();

        // use the boolean to find next valid string
        if (findNextString && c == ' ')
            continue;
        else
            findNextString = false;

        // there is a structure issue with your code.
        // you should think of the priority of checking

        // do not check by rejection, because you will count in punctuation too.
        if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
        {
            countChars++;
        }
        else if (c == '
')
        {
            countLines++;
            countWords++; // <- add word too
        }
        else if (c == ' ' || c == EOF)
        {
            countWords++;
            findNextString = true;
        }
    }

    cout << setw(12) << countLines << setw(12) << countWords << setw(12) << countChars << endl;

    infile.close();

    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...