Need to print out number of words and lines in a file
My instructor kind of assumed we'd know how to do this. I kind of get the idea, but I don't know what to do with the cstring I generate when I tokenize it.
He said to:
Write a program that prompts the user for the name of a file. Then it opens the file, and counts the number of words and lines in the file, and prints out those counts.
I think I possibly could somehow use a counter to increment using getLIne() until getLine() returns NULL, but the problem is, I'm worried if I try that, a file that looks like this:
Bla bla bla bla bla lkfdljkfaklafdskjladsjkdfkjlkdfjdfshafdsjkjrerjkkjfaddjkfsafkjdjakdfsjkasfjkjkfdskjldfjkfjkdjfkdsakdjfkjfdkjdfskjfdskjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjdfkerea
blkjadkjlfdskjldfkjlfdkjfdjkdfsjkldfskljfksfdljfd
Only register four lines and not get the one after the fourth line.
So I was thinking of something like
Code:
#include <iostream>
#include<ifstream>
#include<cstring>
#include<string>
#include<vector>
#include <cstdlib>
using namespace std;
iostream keyboard;
ifstream file;
iostream cin;
iostream cout;
vector<int> v;
int main()
{
int lineCount = 0;
string filename;
cout << "Enter a file name.";
cin >> filename;
file(filename);
char* ch;
int tempWord = 0;
ch = strtok(line, "%s\");
while (ch != NULL)
{
tempWord++;
}
v.push_back(tempWord);
lineCount++;
while(!file.eof())
{
string line = file.nextLine();
char* ch;
int tempWord = 0;
ch = strtok(line, "%s\");
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;
}
Did I do that right?
Will that count the number of words and number of lines correctly?
I really need help now. It's due tomorrow.
Last edited by jedipenguin; January 29th, 2012 at 04:45 PM.
I have never heard of this type called "iostream", and neither has the compiler (your very first error). There is a header called <iostream>, but there is no such type.
Re: Need to print out number of words and lines in a file
When I removed that from the header, it could no longer figure out what cin and cout are. As I said, I've very little knowledge of C++ and am quite furious, if more with the instructor for daring to give us the difficult assignment, especially as this isn't a C++ course even, than with anyone else.
I cooled down a bit and was able to eliminate some of the errors. Actually, there is most definitely a legal include like:
#include<iostream>
I've had it legally in other C++ classes that I wrote before.
Now I have these left as errors:
WordCount.cpp: In function ‘int main()’:
WordCount.cpp:23: error: no match for call to ‘(std::ifstream) (std::string&, const std::_Ios_Openmode&)’
WordCount.cpp:26: 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:41: 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>]
Last edited by jedipenguin; January 29th, 2012 at 05:31 PM.
Re: Need to print out number of words and lines in a file
Originally Posted by jedipenguin
Now I have these left as errors:
WordCount.cpp: In function ‘int main()’:
WordCount.cpp:23: error: no match for call to ‘(std::ifstream) (std::string&, const std::_Ios_Openmode&)’
Please post your updated code.
What are you trying to do on that line (also, it helps if you showed us where line 23 is, as you didn't post your code, and no one is going to count 23 lines).
Code:
cin >> filename;
file(filename);
Is that how you open a file? So how do you open a file:
Re: Need to print out number of words and lines in a file
Perhaps I've altered it since then, but right now it's not compiling.
Code:
#include<fstream>
#include<istream>
#include<string>
#include<vector>
#include<iostream>
#include<stdio.h>
#include<ios>
#include "string.h"
using namespace std;
ifstream keyboard;
ifstream file;
vector<int> v;
int main()
{
string str;
int lineCount = 0;
string filename;
cout << "Enter a file name.";
cin >> filename;
file.open(filename.c_str());
char* line;
file = getline(line, streamsize(10000), str );
char* ch;
int tempWord = 0;
ch = strtok(line, " ");
while (ch != NULL)
{
tempWord++;
}
v.push_back(tempWord);
lineCount++;
while(!file.eof())
{
file = getline(line, streamsize(10000), str);
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;
}
WordCount.cpp:29: error: cannot convert ‘char*’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
WordCount.cpp:44: error: cannot convert ‘char*’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
Line 29: file = getline(line, streamsize(100000), str );
Line 44: file = getline(line, streamsize(100000), str );
Last edited by jedipenguin; January 29th, 2012 at 06:06 PM.
Re: Need to print out number of words and lines in a file
I'd already figured that one before you posted. However, I'm also getting two more errors:
What's wrong with this bit of code:
file = getline(line, streamsize(100000), str );
WordCount.cpp:29: error: cannot convert ‘char*’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
WordCount.cpp:44: error: cannot convert ‘char*’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
Re: Need to print out number of words and lines in a file
Originally Posted by jedipenguin
Still not making sense.
What's wrong?
What did you get out of that example you saw at the link? Comparing it to your code, what is different about that example?
Honestly, you're not taking your time and look at the clear examples that I posted from that website. The examples show that getline() is a member of the stream class. Second, it shows how to use it. The "cin" is the stream, so what is your stream? Isn't it "file" in your example?
If you get this thing to compile, can you imagine what happens if you try to run it and it doesn't produce the results you want?
Regards,
Paul McKenzie
Last edited by Paul McKenzie; January 29th, 2012 at 06:30 PM.
Re: Need to print out number of words and lines in a file
It runs forever.
I figured it out. I was having trouble trying to figure out what to do exactly with the syntax but I got it before you posted. However, it appears to be going into an infinite loop.
I added the line
file.close() and it's still going on forever.
Last edited by jedipenguin; January 29th, 2012 at 06:35 PM.
Re: Need to print out number of words and lines in a file
Another reason I'm so angry is because if something goes wrong, it's likely I could only get a 60% at best, which I'm taking as a personal insult from the professor. And I feel that that is far too low a grade for all the time I'm spending, hence my anger and impatience.
I've spent all day on this. I could've been doing other assignments.
Re: Need to print out number of words and lines in a file
Originally Posted by jedipenguin
It runs forever.
I figured it out. I was having trouble trying to figure out what to do exactly with the syntax but I got it before you posted. However, it appears to be going into an infinite loop.
You should also have had a small, simple, test program that just opens a file, and reads until the end. Then when you get that working, you concentrate on the other aspects of the program. Trying to get 10 things to work at the same time, and the basics are still not working is not the way to complete assignments.
Also, making small test programs makes asking questions here easier for you. You now know what doesn't work, and the question becomes more targeted, as opposed to "my program doesn't work and here is all of my code".
Bookmarks