ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Ubuntu 8.04 - Fedora 9 on an AMD 64bit Machine
Posts: 101
Original Poster
Rep:
Umm,
I thought about changing the book, too. Bit I think it is a good book (just personal opinion)
The Book's name is "Waite Group's Object-Oriented Programming in C++, Third Edition" by "Rober Lafore".
If you have any other book suggestions, please let me know.
You should not use a kludge in you code without really understanding why (wdcount + 1) works instead of just (wdcount). I think that your word stream ends up with '\n', which leads to the last word not be counted. By the way, if you have few consecutive spaces between words your program also will give the wrong answer.
BTW: there is a Linux command 'wc' which does exactly what you trying to do, plus more.
Distribution: Ubuntu 8.04 - Fedora 9 on an AMD 64bit Machine
Posts: 101
Original Poster
Rep:
Hi,
reverse: Name some!
rsashok: thanks for the notices. the fact is that I am still learning, and as it may appear from this naive code, I am still getting my feet wet. I do know about teh multiple spaces problem (it is mentioned in the book) but thanks for the 'wc' command.
wc [option] filename
-c, --bytes
print the byte counts
-m, --chars
print the character counts
-l, --lines
print the newline counts
-L, --max-line-length
print the length of the longest line
-w, --words
print the word counts
I appreciate however that your object is learning and exercising your learning in C and not necessarily concerned that the functionality is available in the shell.
THE best book on the subject is:
Code:
The C Programming Language, 270 pages,
by Brian W. Kernighan and Dennis M.Richie, 2nd Edition, Published by Prentice Hall
ISBN 0-13-110362-8 (paperback)
ISBN 0-13-110370-9
C was originally designed for and implemented on the UNIX operating system on the DEC PDP-11, by Dennis Richie. It has an interesting history, as it was implemented on the UNIX operating system which it was subsequently used to re-write. Written by the people that defined the language. It really is recommended reading and has lots of exercises for us to do. If you are using this book and come across any problems, then you can at least cite the section of the book and almost all C programmers will be able to dust off their books and know what you are looking at. Very much a standard. A must in any C programmers library.
Distribution: Ubuntu 8.04 - Fedora 9 on an AMD 64bit Machine
Posts: 101
Original Poster
Rep:
PAix,
Thank you for your comment. I am learning C++ not C, though.
I do have the book you mentioned but almost simultaneously I decided to go for C++ not C as it is more likely the one I will use in my future studies.
For this code to work and give correct results, I had to make a small change to the following line:
Code:
cout << "\nWords = " << wdcount << endl
Quote:
to be
Code:
cout << "\nWords = " << (wdcount + 1) << endl
Quote:
The code in its original form (the one I had from the book, do not need such a change, would someone explain?
Noha
PS.
I have tried
Code:
while (ch != '\r')
Quote:
and the program compiled but after feeding in some words and spaces it does not respond after hitting the return key.
Sorry for the delay in responding (and the typos), but I am at work and can only spend a few minutes at a time...
The difference between using the '\r', and the '\n' is one of the many differences between the way Linux and Windows handles end-of-line. Windows automatically adds the '\r' (cursor-return) character before the new-line character, Linux (UNIX) does not. This is why it is a good idea to run a source file through unix2dos, or dos2unix when transferring files between Linux and Windows. Those utilities take care of adding/stripping the cursor-return characters as well as an actual EOF character.
As far as making the change to increment wdcount before printing out the final stats, look at the algorithm. It is incrementing wdcount when it encounters a space character. Because the data line does not contain blank spaces after each word then the count will be off by one. Why it works under Windows I'm not really sure. You could run the code under Visual Studio's debug mode and take a look at the buffer being passed to your app, see if an extra blank character has been added or not.
And lastly, the getchar() function is part of the stdio (c runtime) library. Do a "man getchar" to see descriptions of it, and other supported functions.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.