LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-22-2007, 01:09 PM   #1
nmansour
Member
 
Registered: Sep 2006
Location: Chicago
Distribution: Ubuntu 8.04 - Fedora 9 on an AMD 64bit Machine
Posts: 101

Rep: Reputation: 15
A small program


Hi,

I am learning C++ and I was trying this code from the book I use:

Code:
#include <iostream>
using namespace std;
#include <conio.h>	//for getche()

int main()
    {
    int chcount=0;
    int wdcount=1;
    char ch= 'a';

    cout << "Enter a phrase: ";
    while( ch != '\r' )
       {
       ch = getche();
       if( ch == ' ')
       wdcount++;
       else
       chcount++;
       }
    cout << "\nWords= " << wdcount << endl
         << "\nLetters= " << (chcount -1) << endl;
    return 0;
    }
As you see, it uses the library conio.h. I know that this is a win32 library. I have tried to find it for Linux and I failed.

How would I adjust this code to run it on a Linux machine?

I have tried to install this library through a package called linux-conio-102 from here but I had the following problem here.

I use Fedora 7 on an AMD 64 Athlon Machine.

Any help would be greatly appreciated!

Noha
 
Old 10-22-2007, 01:17 PM   #2
reverse
Member
 
Registered: Apr 2007
Distribution: Gentoo
Posts: 337

Rep: Reputation: 30
Just curious, what book are you using? (You should probably get a different one anyway)
 
Old 10-22-2007, 01:21 PM   #3
nmansour
Member
 
Registered: Sep 2006
Location: Chicago
Distribution: Ubuntu 8.04 - Fedora 9 on an AMD 64bit Machine
Posts: 101

Original Poster
Rep: Reputation: 15
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.

Noha

Last edited by nmansour; 10-22-2007 at 01:24 PM.
 
Old 10-22-2007, 01:26 PM   #4
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
Quote:
How would I adjust this code to run it on a Linux machine?
Code:
#include <iostream>
using namespace std;

int main()
{
    int chcount = 0;
    int wdcount = 0;
    int ch = 'a';

    cout << "Enter a phrase: ";
    while ( ch != '\n' )
    {
        ch = getchar()
        if ( ch == ' ' )
            wdcount++;
        else
            chcount++;
    }
    cout << "\nWords = " << wdcount << endl
         << "\nLetters = " (chcount - 1) << endl;
    return 0;
}
Works for me on my FC6 system.
 
Old 10-22-2007, 01:45 PM   #5
nmansour
Member
 
Registered: Sep 2006
Location: Chicago
Distribution: Ubuntu 8.04 - Fedora 9 on an AMD 64bit Machine
Posts: 101

Original Poster
Rep: Reputation: 15
Small typos in your code:

Code:
#include <iostream>
using namespace std;

int main()
{
    int chcount = 0;
    int wdcount = 0;
    int ch = 'a';

    cout << "Enter a phrase: ";
    while ( ch != '\n' )
    {
        ch = getchar();
        if ( ch == ' ' )
            wdcount++;
        else
            chcount++;
    }
    cout << "\nWords = " << wdcount << endl
         << "\nLetters = " << (chcount - 1) << endl;
    return 0;
}


But of course thank you very much for the quick help. So it is the getchar() library then!?

Noha

PS.
Whenever M$ is involved, you will always have trouble.
 
Old 10-22-2007, 02:24 PM   #6
nmansour
Member
 
Registered: Sep 2006
Location: Chicago
Distribution: Ubuntu 8.04 - Fedora 9 on an AMD 64bit Machine
Posts: 101

Original Poster
Rep: Reputation: 15
Another question

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
to be
Code:
cout << "\nWords = " << (wdcount + 1) << endl
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')
and the program compiled but after feeding in some words and spaces it does not respond after hitting the return key.
 
Old 10-22-2007, 03:50 PM   #7
reverse
Member
 
Registered: Apr 2007
Distribution: Gentoo
Posts: 337

Rep: Reputation: 30
I was thinking of going for a book that is somewhat Linux (or portability*) tolerant.
 
Old 10-22-2007, 04:49 PM   #8
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Rep: Reputation: 31
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.
 
Old 10-22-2007, 05:26 PM   #9
nmansour
Member
 
Registered: Sep 2006
Location: Chicago
Distribution: Ubuntu 8.04 - Fedora 9 on an AMD 64bit Machine
Posts: 101

Original Poster
Rep: Reputation: 15
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.

Noha
 
Old 10-22-2007, 08:21 PM   #10
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
For the sake of completeness:
Code:
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.

PAix
 
Old 10-22-2007, 08:26 PM   #11
nmansour
Member
 
Registered: Sep 2006
Location: Chicago
Distribution: Ubuntu 8.04 - Fedora 9 on an AMD 64bit Machine
Posts: 101

Original Poster
Rep: Reputation: 15
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.

Thanks again,

Noha
 
Old 10-23-2007, 11:02 AM   #12
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
Hi again,

Quote:
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.

I hope that this helps!

Last edited by rstewart; 10-23-2007 at 11:04 AM.
 
Old 10-24-2007, 09:35 AM   #13
nmansour
Member
 
Registered: Sep 2006
Location: Chicago
Distribution: Ubuntu 8.04 - Fedora 9 on an AMD 64bit Machine
Posts: 101

Original Poster
Rep: Reputation: 15
rstewart:
Thanks for the reply, it really helped me a lot.

Noha,

Sorry for late reply, trouble with the net.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
a small program nmansour Programming 23 06-13-2007 02:38 PM
Small database program? PatrickMay16 General 1 06-14-2006 11:32 PM
please help with small sort program stuckatc Linux - Newbie 1 06-01-2006 01:17 PM
Need help with small program batalia Linux - Newbie 8 11-07-2005 02:41 PM
Compile a small program Dauer Ubuntu 2 11-05-2005 04:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration