Thread: explain

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    4
    Rep Power
    0

    Question explain


    Here is a very simple program that counts words and other things in a document. Some of this code I got help on. I am pretty new to C++ and I was wondering if someone can explain something to me. Here is the code:

    #include <fstream.h>
    #include <iostream.h>
    #include <ctype.h>
    #include <conio.h>
    #include <stdlib.h>

    int main()
    {
    int letters = 0, digits = 0, words = 0, puncts = 0, others = 0, average = 0;
    char ch;
    ifstream txtdoc;

    txtdoc.open("a:computer.txt");
    if(txtdoc.fail())
    {
    clrscr;
    //goto(26,11);
    cout<< "File not found!!! Exiting...........";
    exit(1);
    }
    else
    {
    clrscr;
    //goto(26,11);
    cout<< " File has been successfuly opened!";
    }
    txtdoc.get(ch);
    while(!txtdoc.eof())
    {
    while((isspace(ch) || isdigit(ch) || ispunct(ch)) && !txtdoc.eof())
    {
    if(isdigit(ch))
    digits++;
    else
    if(ispunct(ch))
    puncts++;
    else
    others++;
    txtdoc.get(ch);
    }
    if (!txtdoc.eof())
    words++;
    while(isalpha(ch) && !txtdoc.eof())
    {
    letters++;
    txtdoc.get(ch);
    }
    }

    average = (letters/double(words) + 0.5);

    txtdoc.close();
    //goto(26, 13);
    cout<< "Number of letters = " << letters << endl << endl;
    //goto(26, 15);
    cout<< "Number of digits = " << digits << endl << endl;
    //goto(26, 17);
    cout<< "Number of words = " << words << endl << endl;
    //goto(26, 19);
    cout<< "Number of punctuation characters = " << puncts << endl << endl;
    system("PAUSE");
    return 0;
    }


    My question is : how does it count the words? All I see is
    "if (!txtdoc.eof())
    words++; "
    but how does it tell words appart?

    My second question is how to do the function of gotoxy (which positions the cursor at a given position on the screen) on dev.?

    Thanks in advance;

    Konrad
    Last edited by konradj; March 30th, 2003 at 08:28 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    My question is : how does it count the words? All I see is
    "if (!txtdoc.eof())
    words++; "
    but how does it tell words appart?
    That's what all the lines of code before and after the code you cited does. Here are some questions for you to consider:

    1) What does ch have to be in order to reach the lines you cited?
    2) What do the lines after the lines you cited do?
    Last edited by 7stud; March 31st, 2003 at 01:30 AM.

IMN logo majestic logo threadwatch logo seochat tools logo