Jump to content


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.


Trying To Use Getchar() Properly

  • Please log in to reply
2 replies to this topic

#1 charlie.reddington


  • Members
  • 2 posts
  • Local time:08:26 PM

Posted 11 January 2007 - 09:51 AM

Hi everyone,

I'm in the process of learning how to program c++. I have a book that is based on windows. Unfortunately I'm a Linux guy, and some things don't carry over the same.

For example, one of the books examples uses the header conio.h and the next closest thing I found for Linux is ncurses.h.

In the book code, it says to use getch(). I have found that this doesn't do the same thing in Linux, and that I should be using getchar(). <--- is this correct?

The program is supposed to ask for input. The program then goes through and counts words and letters and then outputs the info to the screen. Heres what I have.

// chcount.cpp
// counts characters and words typed in

#include <iostream>
#include <ncurses.h>					// for getche()

using namespace std;

int main()
		int chcount=0;				  // counts non-space characters
		int wdcount=1;				  // counts spaces between words
		char ch = 'a';				  // ensure it isnt '\r'

		cout << "Enter a phrase: ";
		while ( ch != '\r' )			// loop until enter typed
				ch = getchar();		 // read one character
				if ( ch == ' ' )		// if its a space
				wdcount++;			  // count a word
				else					// otherwise
				chcount++;			  // count a character
		cout << "\nWords=" << wdcount << endl
			 << "Letters=" << (chcount-1) << endl;
		return 0;

The program compiles fine. No errors or warnings. But it wont stop the loop. It asks for a phrase as it should. Then you enter it in. When you hit the enter key, i'm under the impression that the loop should stop, and finish with the output. It doesn't. It continues the loop either on a new line from the enter key, or just keeps writing to the line.

This all works fine in a windows using conio.h. I'm trying to make it work in Linux.

Many thanks.


BC AdBot (Login to Remove)


#2 charlie.reddington

  • Topic Starter

  • Members
  • 2 posts
  • Local time:08:26 PM

Posted 11 January 2007 - 11:22 AM

Okay, got it figured out. It was another Windows to Linux problem. In windows you use /r for return, and in linux I think you use /n. I made the changes and it worked fine.

#3 projectfocus


  • Members
  • 479 posts
  • Gender:Male
  • Location:Brighton
  • Local time:03:26 AM

Posted 12 January 2007 - 07:30 AM

In these cases I found the easiest way to solve the issue was to have a cout in the loop this way you can verify that what you are typing is what is being received and also that this matches with the cruiteria for the loop. Just for test purposes.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users