Write a program system that reads several lines of information from a data file and prints each word of the file on a separate line of an output file followed by the number of letters in that word.
Also, print a count of words in the file on the screen when done. Assume that words are separated by one or more blanks.
Not sure what the best way to attack this one would be... Any suggestions?
Thanks. But how do I process each word in a stream? My professor said something like you have to set up a constant char for each space and once it gets to that that is a new word. He also said said something about setting up EOLN='/n'. I don't know how to go about actually setting up this code. Any tips?
#include <iostream> //for the input output stream
#include <fstream> //for file stream
#include <cstring> //for strtok
usingnamespace std; //all objects are under namesapce std
int main(){
int wordCount = 0; //to store word count
char* fileName; //to store file name
cout << "\nInput File Name : ";
cin.getline(fileName); //to get the file name from user
ifstream file(fileName);//open the file for input
while(!file.eof()){ //continue to end of file
char* line; //to store a line
char* word; //to store a word
file.getline(line); //get a line from file
word = strtok(line," "); //get a pointer to string till space
while(word){ //till getting a null
word = strtok(NULL," "); //keep getting string
cout << word << endl; //print the word
wordCount++; //increment wordCount
}
}
cout << "\nTotal words in file" << fileName << " are " << wordCount;
return 0;
}
The example does need debugging, but would give you an idea of how to do the job.
Thanks for your help. I tried out your code and am in the process of debugging it. I am unfamiliar with using strtok so I am having a little trouble debugging this.
No overloaded function takes 1 arguments? I think it is referring to the cin.getline(filename) .
I don't see what is wrong becuase it was not predeclared with a certain amount of inputs. Any suggestions?
usingnamespace std; //all objects are under namesapce std
int main(){
int wordCount = 0; //to store word count
char* Ch8_3; //to store file name
cout << "\nInput File Name : ";
cin.getline(Ch8_3,80); //to get the file name from user
ifstream file(Ch8_3);//open the file for input
while(!file.eof()){ //continue to end of file
char* line; //to store a line
char* word; //to store a word
file.getline(line,80); //get a line from file
word = strtok(line," "); //get a pointer to string till space
while(word){ //till getting a null
word = strtok(NULL," "); //keep getting string
cout << word << endl; //print the word
wordCount++; //increment wordCount
}
}
cout << "\nTotal words in file" << Ch8_3 << " are " << wordCount;
return 0;
}
Ok thanks. I did the same with the second one to fix it as well (file.getline(line,80)). Both of these errors were fixed.
I also changed fileName to the file name of my text document (Maybe I shouldnt have done this). I get no errors until the cmd actually pops up.
Then it says Debug error! Run-time Check Failure #3 - the variable ch8_3 is being used without being initialized. Press retry...
Initialized? I am not sure what it means by that. The text document exists and we referenced it... Thanks.
char* Ch8_3; //to store file name
cout << "\nInput File Name : ";
cin.getline(Ch8_3,80); //to get the file name from user
ifstream file(Ch8_3);//open the file for input
and just use
ifstream file("Ch8_3.txt");//open the file for input
Ok thanks. I just tried that. It still gives me a debug error when I open up the cmd. This time it says:
Runt time check #3. The variable line is being used without being inititialized... I thought we initialized line here : char* line; //to store a line
Thanks for your help. Sorry for the nooby questions. I am really new to this :(
Initialized? I am not sure what it means by that. The text document exists and we referenced it...
1 2 3
char* Ch8_3; //to store file name
cout << "\nInput File Name : ";
cin.getline(Ch8_3,80); //to get the file name from user
Here you use Ch8_3 just as a variable name, so once get
Input File Name :
you enter the file name,
Ch8_3.txt
otherwise if you just press ENTER / RETURN, it gets no value (in other words it is uninitialized, as we just declared this variable but did not assign any value to it). So when you use it in
ifstream file("Ch8_3.txt");//open the file for input
There is nothing in Ch8_3, thus a Run time Error
but in this
ifstream file("Ch8_3.txt");//open the file for input
Ok I understand. So the original way would have allowed me to do this for multiple different text documents because you input it in the program. Where as in the second method it only works for ch8_3. I understand that now, but how do I get rid of the error I am getting in above post. (btw I am using the second way of directly passing the value right now)
Yah I checked that out earlier. So I understand what it does with the tokens.
I went back to the original way where you actually input the file you want to run the code on. Here is my current code:
#include <iostream>
#include <fstream>
usingnamespace std;
#include <iostream> //for the input output stream
#include <fstream> //for file stream
#include <cstring> //for strtok
usingnamespace std; //all objects are under namesapce std
int main(){
int wordCount = 0; //to store word count
char* fileName; //to store file name
cout << "\nInput File Name : ";
cin.getline(fileName,80); //to get the file name from user
ifstream file(fileName);//open the file for input
while(!file.eof()){ //continue to end of file
char* line; //to store a line
char* word; //to store a word
file.getline(line,80); //get a line from file
word = strtok(line," "); //get a pointer to string till space
while(word){ //till getting a null
word = strtok(NULL," "); //keep getting string
cout << word << endl; //print the word
wordCount++; //increment wordCount
}
}
cout << "\nTotal words in file" << fileName << " are " << wordCount;
return 0;
}