Reading input doesn't work

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • NaN

    Reading input doesn't work

    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?
  • Default User

    #2
    Re: Reading input doesn't work

    NaN wrote:
    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

    Comment

    • Richard Heathfield

      #3
      Re: Reading input doesn't work

      [Subject line: Reading input doesn't work]

      NaN said:
      Hi
      Hi
      I'm using Dev-C++.
      Presumably you mean you're using a C compiler of some kind which happens to
      have a few +s in its name. That's fine.
      Here is my sourcecode.
      Okay. Let me fix it for you.

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

      #include <stdio.h>
      #include <ctype.h>

      int main(void)
      {
      int ch;
      puts("Type 'Y' when finished typing keys.");
      while((ch = toupper(getchar ())) != EOF && ch != 'Y')
      {
      }

      puts("Y");
      return 0;
      }

      --
      Richard Heathfield <http://www.cpax.org.uk >
      Email: -http://www. +rjh@
      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
      "Usenet is a strange place" - dmr 29 July 1999

      Comment

      • Keith Thompson

        #4
        Re: Reading input doesn't work

        NaN <no@spam.invali dwrites:
        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> .

        --
        Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
        Nokia
        "We must do something. This is something. Therefore, we must do this."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        Working...