On 2008-01-27, Alf P. Steinbach <al***@start.nowrote:
* samoukos:
>Hello everyone, around one weeek ago i started to learn the c++
language. And i try to solve different programs i found from a text
book i got here in greece. But i got to a chapter and i have met a
program that wants me to count the words from an input text but not
using UTF-8 of UTF-7 but ASCII.. does anyone knows how to help me. ? I
have tryed many things but i have managed only to print out the ascii
number of each letter of a word.. nothing more....
again if anyone knows please help...
It depends very much on the definition of "word".
If you can accept the definition that's implicit in using >>, then just
use >(on std::cin) to input the words.
The following program implements that idea (code taken out from my
Sandbox, slightly adapted from TICPP):
#include <iostream>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main (int argc, char** argv) {
int num = 0;
string word;
string filename;
if (argc < 2) {
cerr << "Usage: " << argv[0] << " <filename>" << endl;
exit(1);
}
filename=argv[1];
ifstream in(filename.c_str());
while (in >word) {
cout << word << endl;
num++;
}
cout << num << endl;
exit(0);
}
Regards.
--
Stefano Sabatini
Linux user number 337176 (see
http://counter.li.org)