C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old March 30th, 2003, 08:19 PM
konradj konradj is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 4 konradj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #2  
Old March 31st, 2003, 01:27 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
Quote:
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > explain


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway