Reading input doesn't work

N

NaN

Hi I'm using Dev-C++.

Here is my sourcecode.


/* GETCH.C: This program reads characters from
* the keyboard until it receives a 'Y' or 'y'.
*/

#include <conio.h>
#include <ctype.h>

void main( void )
{
int ch;

_cputs( "Type 'Y' when finished typing keys: " );
do
{
ch = _getch();
ch = toupper( ch );
} while( ch != 'Y' );

_putch( ch );
_putch( '\r' ); /* Carriage return */
_putch( '\n' ); /* Line feed */
}

Can u help?
 
D

Default User

NaN said:
Hi I'm using Dev-C++.

Here is my sourcecode.


/* GETCH.C: This program reads characters from
* the keyboard until it receives a 'Y' or 'y'.
*/

#include <conio.h>
#include <ctype.h>

void main( void )
{
int ch;

_cputs( "Type 'Y' when finished typing keys: " );
do
{
ch = _getch();
ch = toupper( ch );
} while( ch != 'Y' );

_putch( ch );
_putch( '\r' ); /* Carriage return */
_putch( '\n' ); /* Line feed */
}

Can u help?

You have a bunch of non-standard, proprietary stuff here. You'll need a
Dev C++ forum of some sort. If I read it correctly, you want to get
individual keystrokes from the input. There's no standard way to do
that.

See the FAQs:

<http://c-faq.com/osdep/cbreak.html>




Brian
 
K

Keith Thompson

NaN said:
Hi I'm using Dev-C++.

Presumably you're using it as a C compiler, not as a C++ compiler.
Here is my sourcecode.


/* GETCH.C: This program reads characters from
* the keyboard until it receives a 'Y' or 'y'.
*/

#include <conio.h>
#include <ctype.h>

void main( void )
{
int ch;

_cputs( "Type 'Y' when finished typing keys: " );
do
{
ch = _getch();
ch = toupper( ch );
} while( ch != 'Y' );

_putch( ch );
_putch( '\r' ); /* Carriage return */
_putch( '\n' ); /* Line feed */
}

Can u help?

Please don't use silly abbreviations like "u". Take the time to type
out the whole word. Don't make reading your message more difficult
than it has to be.

Please put your question in the body of your message. Some
newsreaders don't make it easy to see the subject header while reading
a message. (Your subject was "Reading input doesn't work".)

Can we help with what exactly? You say it "doesn't work". There are
a nearly unlimited number of ways in which something can "not work".
You need to tell us what you expected what you got, and how they
differ; we're not mindreaders.

"void main(void)" needs to be "int main(void)". And since main()
returns an int, you should actually return an int; add "return 0;"
before the closing brace.

The following are non-standard:

<conio.h>
_cputs
_getch
_putch

The standard I/O routines are declared in <stdio.h>. Consult your
textbook for instructions on how to use them. Nothing you're trying
to do here (as far as I can tell) requires anything beyond standard C
features.

Assuming that _putch is intended to print a character to standard
output (or the console, or whatever), you probably don't need the
'\r'. A single '\n' character represents a new-line; it should be
translated as necessary for your system.

You only call _putch for a single character, followed by '\r' and
'\n'. Since the loop won't terminate until ch has the value 'Y', that
single character will inevitably be 'Y'. Without knowing what you're
trying to do, I can't guess whether that's what you want.

Suggested reading:

The comp.lang.c FAQ, at <http://www.c-faq.com/>.

Eric Raymond's "How To Ask Questions The Smart Way", at
<http://www.catb.org/~esr/faqs/smart-questions.html>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top