adjusting a code for Linuxconio.h


 
Thread Tools Search this Thread
Top Forums Programming adjusting a code for Linuxconio.h
# 1  
Old 04-25-2009
adjusting a code for Linuxconio.h

Hi All,

I am using a book to learn C++. Unfortunately the book, sometimes, uses the maddening phrase, #include <conio.h>, which is a M$ related file and not a part of STL, in some of its examples.

My question is: How would you modify the following code so it would compile and run on Linux? Here is the code:
Code:
// chcount.cpp
// counts characters and words typed in
#include <iostream>
using namespace std;
#include <conio.h>             //for getche()
int main()
   {
   int chcount=0;              //counts non-space characters
   int wdcount=1;              //counts spaces between words
   char ch = ‘a';              //ensure it isn't ‘\r'
   cout << “Enter a phrase: “;
   while( ch != ‘\r' )         //loop until Enter typed
      {
      ch = getche();          //read one character
      if( ch==' ‘ )           //if it's a space
      wdcount++;              //count a word
      else                    //otherwise,
      chcount++;              //count a character
      }                       //display results
   cout << “\nWords=” << wdcount << endl
        << “Letters=” << (chcount-1) << endl;
   return 0;
   }

The code is primitive, I know, as I got it from the first couple of chapters of this book.

Thank you so much in advance,

eager

PS.
I am also open to any suggestions for more books, although I find the current one very useful, except this conio.h file!
# 2  
Old 04-26-2009
Wow, I haven't seen conio.h since my Turbo C for DOS days. I doubt you really want to go to all the trouble of using ncurses... you can't just include it and start throwing around getch-es, you have to set up a window and mode and all that stuff. Unless interactivity is really that important to the code(it almost never is), just use stdio i/o and expect things to be buffered by lines.
# 3  
Old 04-26-2009
Thanks

Hi,

Thanks for the reply.
So, does this mean there is no easy way to adjust it to Linux?

eager
# 4  
Old 04-26-2009
Yes, like I said, there is no easy way to adjust it for linux because it's using DOS-specific features. Like I said, if you really wanted to, you could use ncurses, but that's a lot more complicated than just including a header and using its functions instead. So, like I said, using normal stdio i/o and expecting it to be line buffered would work.
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adjusting my awk output format

I've been diving into awk but still learning how to use it for text formatting. Below you can see my results are separated by a comma. Can somebody show me how to separate by TAB as well? An explanation would be appreciated as I did not comprehend the answer in the man pages and would like to gain... (1 Reply)
Discussion started by: sudo
1 Replies

2. UNIX for Dummies Questions & Answers

Problem adjusting the output

Hi i m running a command watch -n 1 -d netstat -i to see the packet drops every 1 second. but the problem is the output is so long(Due to large number of virtual interfaces) it doesn't fit into the putty prompt. I dont need to monitor each and every network interface I m more interested in... (5 Replies)
Discussion started by: pinga123
5 Replies

3. Shell Programming and Scripting

Adjusting Dates

What is the easiest way to find the date 6 month prior to the current date. Example: Today is 2011/01/29 I need to find the 1st day of the month, 6 month ago, which is 2010/08/01. I have to count 1/1/2011 as a previous month, since the current day is past 1/1/2011. Is there any easy... (4 Replies)
Discussion started by: jclanc8
4 Replies

4. Solaris

Setting timezone Sunos 5.8 & adjusting

I am new to this so i figure this is an easy one.. How do i change the time zone from Central to Eastern time in SunOS 5.8 ? I thought I needed to edit the /etc/TIMEZONE and them issue TZ=US/Eastern but I want to check. Is a reboot required afterwards? If I want to change the system time... (3 Replies)
Discussion started by: jay6ird
3 Replies

5. Shell Programming and Scripting

Adjusting the output in a file

QUERY SCENARIO Here is the actual scenario LOOP echo "$COLNAME $TYPENAME($LENGTH) $NULLS ">>$DDL_FILE END-LOOP COLNAME, TYPENAME, LENGTH, NULLS are the variables and within echo statment the output of which has to go into file specified by DDL_FILE. ... (1 Reply)
Discussion started by: skyineyes
1 Replies
Login or Register to Ask a Question