Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Can't figure out how to print out number of lines and words in a file in c++

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Can't figure out how to print out number of lines and words in a file in c++

    I was supposed to count the number of words in each file and the number of lines in each file. However, the compiler is giving me hell and I'm about ready to explode in frustration.

    I've tried this code and it keeps giving me the errors and I've no clue what they mean. (I was assigned this by an instructor who obviously thinks I know everything about C++, even though some in the class have never even taken that course. I have and I did ok, though I had a very hard time with it.)

    #include <iostream>
    #include<fstream>
    #include<istream>
    #include<string>
    #include<vector>
    #include<stdio.h>
    #include "string.h"
    using namespace std;
     
    iostream keyboard;
    ifstream file;
     
    vector<int> v;
    int main()
    {
     
    int lineCount = 0;
    string filename;
    cout << "Enter a file name.";
    cin >> filename;
     
    file(filename, ifstream::in);
     
    char* line;
     line = file.getline();
    char* ch;
    int tempWord = 0;
    	ch = strtok(line, " ");
     
    while (ch != NULL)
    {
    tempWord++;
    }
     
    v.push_back(tempWord);
    lineCount++;
    while(!file.eof())
    {
     
    line  = file.getline();
     
    char* ch;
    int tempWord = 0;
    	ch = strtok(line, " ");
     
    while (ch !=NULL)
    {
    tempWord++;
     
    }
     
    v.push_back(tempWord);
    lineCount++;
     
     
     
     
     
    }
     
    cout <<"Line count: " << lineCount;
     
    int total;
    for (int i =0; i < v.size(); i++)
    {
    total = total + v[i];
    }
     
    cout << "Word count: " << total;
     
    return 0;
    }
     

    g++ WordCount.cpp
    /usr/include/c++/4.3/istream:801: error: ‘std::basic_iostream<_CharT, _Traits>::basic_iostream() [with _CharT = char, _Traits = std::char_traits<char>]’ is protected
    WordCount.cpp:10: error: within this context
    WordCount.cpp: In function ‘int main()’:
    WordCount.cpp:22: error: no match for call to ‘(std::ifstream) (std::string&, const std::_Ios_Openmode&)’
    WordCount.cpp:25: error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::getline()’
    /usr/include/c++/4.3/istream:598: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
    /usr/include/c++/4.3/istream:409: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]
    WordCount.cpp:40: error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::getline()’
    /usr/include/c++/4.3/istream:598: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
    /usr/include/c++/4.3/istream:409: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]
    pradcoc@tranquility:~/IT383/16>




    I thought there would be c++ functions that would make this easy but they're nto working. In fact, I'm not even sure that, if I get this to work, that it'll even count the words and lines correctly but some credit is better than none (My instructor is unfair and won't count it if it won't compile. Which is really unfair as I've never done this kind of C++ before.)

    This thread has been cross posted here:

    http://www.codeguru.com/forum/showthread.php?p=2052666#post2052666

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Last edited by javapenguin; January 29th, 2012 at 06:33 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can't figure out how to print out number of lines and words in a file in c++

    I got as far as I could. I give up unless somebody tells me what's going wrong.

    It's compiling but going into an infinite loop (See other forum.).

Similar Threads

  1. Removing duplicate words from a file
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: January 13th, 2012, 06:34 AM
  2. Counting Words in a File with a Loop
    By bengregg in forum Loops & Control Statements
    Replies: 17
    Last Post: February 11th, 2011, 10:11 AM
  3. Replies: 1
    Last Post: October 16th, 2010, 04:32 PM
  4. counting words of a text file
    By maybach230 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 6th, 2010, 04:40 PM
  5. Question: Converting number to words.
    By shamed in forum Java Theory & Questions
    Replies: 6
    Last Post: January 1st, 2010, 04:28 AM