473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Number of words in a string read from a file

Hello! I need to count the number of words in a string read in from an infile, in a function, but I don't know how to do this. This is what I have so far.

//Tiffany Lynn Goodseit
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void diffwords(string &line);

int main()
{
string line;

//open input file
ifstream infile("c:\\Dev-Cpp\\phraseinput.txt");
//ifstream infile("con");

while (getline(infile,line))
{
diffwords(line);
cout << line << endl;
}

infile.close();
return 0;


//Function to count the number of words in the entire text
void diffwords(string &line);
{
string line;
int pos, count = 0;

getline (infile, line);
cout << line << endl;


cout << "the is found " << count << "times" << endl;

}
Dec 9 '06 #1
0 2357

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: 73blazer | last post by:
Hello, I'm writing some C++ code, and I need to be able to find the number of files in a given directory. Is it possible under AIX4.3.3 with C++ 3.6.4? I cannot seem to locate anything of this...
18
by: prasanna.hariharan | last post by:
Hi guys, I want to remove certain words from a c++ string. The list of words are in a file with each word in a new line. I tried using the std::transform, but it dint work. Anybody got a clue...
12
by: dough | last post by:
I want to read in lines from a file and then seperate the words so i can do a process on each of the words. Say the text file "readme.txt" contains the following: In the face of criticism from...
51
by: moosdau | last post by:
my code: do { printf("please input the dividend and the divisor.\n"); if(!scanf("%d%d",&dend,&dor)) { temp1=1; fflush(stdin); } else
6
by: Gary Wessle | last post by:
hi I have a data file with equal number of columns for each row. I need to get the number of rows and columns to allocate a matrix in gsl. getline (in, line) and parse the line for the number of...
3
by: Nhd | last post by:
I have a question which involves reading from cin and counting the number of words read until the end of file(eof). The question is as follows: Words are delimited by white spaces (blanks,...
20
by: dmurray14 | last post by:
Hey guys, I'm a C++ newbie here - I've messed with VB, but I mostly stick to web languages, so I find C++ to be very confusing at times. Basically, I am trying to import a text file, but I want...
1
by: sma0698 | last post by:
I am very new to C++. In my assignment I have to read in a phrase from a file and send out to another file the number of words in the phrase and a list of letters in the phrase and how many times...
2
by: alwaali | last post by:
Hi I need help please This is my project and i need a help to solve it with you A page of text is to be read and analyzed to determine number of occurrences and locations of different words. The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.