Use of getchar

Discussion in 'C++' started by Cam, May 30, 2004.

  1. Cam

    Cam Guest

    Hi everyone,

    Before I answer to a (hopefully) helpful reply to this post, I have been
    rapped over the knuckles for 'top-posting' and I do not wish to be a learner
    poster who observes poor netiquette as I am a firm believer in consideration
    .... to this end, could somebody please tell me how I reply to a message
    without top-posting? Do I need to have my original post selected when I hit
    the 'reply' button?

    Here's my question (and please remember that I'm teaching myself as I go and
    initially learning what I need to complete assignments although it is my
    intention to obtain some sort of proficiency at this code ..)

    Previously, I was using the conio.h header and the getche function to read
    keyboard input one character at a time to place individual characters into
    an array.

    As the code is required to compile on a Unix system, I have changed to the
    non-platform specific stdio.h header and am using getchar as suggested on
    this NG.

    The user inputs two 8 bit numbers which then have arithmetic performed on
    them. I do this by calling a KeyboardInput() function twice and having the
    function modify two arrays by using pointers.

    The problem is that the first call of the KeyboardInput() function works
    fine. When the function is called a second time (for key2[7] to key2[0]) the
    values from the first first call are placed into the key2 array as if the
    user had entered them.

    Advice is greatly appreciated.

    Kind regards,

    Cam

    code follows:

    #include <iostream>
    #include <stdio.h> // Re-coded to use ISO standard header (conio.h only used
    by Microsoft and Borland)

    using namespace std;

    void KeyboardInput(); // Function prototype

    int key[8], *key_ptr = key, key1[8], *key1_ptr = key1, key2[8], *key2_ptr =
    key2;

    ....

    int main()
    {
    ....
    KeyboardInput(); // key1[7] to key1[0]
    for (counter = 0; counter < 8; counter ++)
    {
    origkey1[counter] = key_ptr[counter];
    key1_ptr[counter] = key_ptr[counter];
    addkey1_ptr[counter] = key1_ptr[counter];
    } // Get key1 array

    KeyboardInput(); // key2[7] to key2[0]
    for (counter = 0; counter < 8; counter ++)
    {
    origkey2_ptr[counter] = key_ptr[counter];
    key2_ptr[counter] = key_ptr[counter];
    addkey2_ptr[counter] = key_ptr[counter];
    } // Get key2 array
    ....
    return 0;
    }

    ....

    void KeyboardInput () // Accepts an 8 bis binary input from the user
    terminated by ENTER
    {
    restart:
    int ch, counter = 0;
    cout << "\nEnter a 2's complement 8 bit binary number: ";

    for ( counter = 7; (counter > -1) && ((ch = getchar()) != EOF) && (ch !=
    '\n'); counter -- )
    key_ptr[counter] = (char)ch - 48; // Subtract 48 from ASCII number to
    give integer value

    cout << "\n";
     
    Cam, May 30, 2004
    #1
    1. Advertising

  2. Cam

    John Carson Guest

    "Cam" <retsigerymmudathotmaildotcom> wrote in message
    news:
    > Hi everyone,
    >
    > Before I answer to a (hopefully) helpful reply to this post, I have
    > been rapped over the knuckles for 'top-posting' and I do not wish to
    > be a learner poster who observes poor netiquette as I am a firm
    > believer in consideration ... to this end, could somebody please tell
    > me how I reply to a message without top-posting? Do I need to have my
    > original post selected when I hit the 'reply' button?


    It is simply a question of where you choose to type your reply.

    > Here's my question (and please remember that I'm teaching myself as I
    > go and initially learning what I need to complete assignments
    > although it is my intention to obtain some sort of proficiency at
    > this code ..)
    >
    > Previously, I was using the conio.h header and the getche function to
    > read keyboard input one character at a time to place individual
    > characters into an array.
    >
    > As the code is required to compile on a Unix system, I have changed
    > to the non-platform specific stdio.h header and am using getchar as
    > suggested on this NG.
    >
    > The user inputs two 8 bit numbers which then have arithmetic
    > performed on them. I do this by calling a KeyboardInput() function
    > twice and having the function modify two arrays by using pointers.
    >
    > The problem is that the first call of the KeyboardInput() function
    > works fine. When the function is called a second time (for key2[7] to
    > key2[0]) the values from the first first call are placed into the
    > key2 array as if the user had entered them.
    >
    > Advice is greatly appreciated.
    >
    > Kind regards,
    >
    > Cam
    >
    > code follows:


    Since you are keen on newsgroup etiquette, a couple more pointers. Supply
    compileable code, i.e., compile it yourself and then copy and paste it
    exactly as is. The code you have given below is missing the definition of
    some variables, making it difficult to figure out where things have gone
    wrong.

    The quickest way to diagnose most problems is to run the code through a
    debugger. If the code won't compile and people have to guess at missing
    code, then this process is tedious and inaccurate.

    A couple of comments:

    1. All of the pointer variables you define seem redundant. You can just use
    the array names directly.

    2. Expressions like:

    for ( counter = 7; (counter > -1) && ((ch = getchar()) != EOF) && (ch
    !='\n'); counter -- )

    are wonderfully succinct but impossible to debug. Separate out the various
    pieces so you can see how they are working.

    In as far as I can reproduce your code, given the many omissions, I think
    that the problem is that, when you press Enter after the first number, a
    '\n'
    is placed in the input stream. This is never cleared, so it is the first
    character read the second time around, causing the KeyboardInput function to
    immediately exit. You can solve the problem by adding

    while (ch != '\n')
    ch = getchar();

    after the for loop.

    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
     
    John Carson, May 30, 2004
    #2
    1. Advertising

  3. Cam

    Cam Guest

    Hi John,

    Thankyou very much. I thought that the problem may have been due to a
    pointer within the string that the helpfile was talking about but I couldn't
    understand how to rectify the problem.

    As my code is about 400 lines (I'm probably not as succinct a programmer as
    I should be) I wasn't sure as to how much I should post. I tried to just
    include the salient lines of code to illustrate the problem.

    I hope that I can be in a position to return the favour ... :eek:)

    Cheers,

    Cam
     
    Cam, May 30, 2004
    #3
  4. Cam

    John Carson Guest

    "Cam" <retsigerymmudathotmaildotcom> wrote in message
    news:
    > Hi John,
    >
    > Thankyou very much.


    You're welcome.

    > I thought that the problem may have been due to a
    > pointer within the string that the helpfile was talking about but I
    > couldn't understand how to rectify the problem.
    >
    > As my code is about 400 lines (I'm probably not as succinct a
    > programmer as I should be) I wasn't sure as to how much I should
    > post. I tried to just include the salient lines of code to illustrate
    > the problem.



    A lot of people do the same for the same reason. However, it is a good
    practice to try to reproduce the problem in the simplest possible
    compileable example in order to isolate the problem. I this do often with my
    own code. I sometimes cut and paste the code into a new project and then
    delete most of it just to isolate the problem. The less code I have to deal
    with, the easier it is to find the problem.

    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
     
    John Carson, May 30, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kristo

    getchar vs. cin

    Kristo, Mar 23, 2005, in forum: C++
    Replies:
    1
    Views:
    2,996
    Thomas Matthews
    Mar 24, 2005
  2. Replies:
    21
    Views:
    1,241
    Richard Herring
    Apr 20, 2005
  3. Ben Fitzgerald

    Re: getchar returns int, assign to array of char?

    Ben Fitzgerald, Jun 26, 2003, in forum: C Programming
    Replies:
    9
    Views:
    963
    Ben Fitzgerald
    Jun 27, 2003
  4. Ben Fitzgerald

    Re: getchar returns int, assign to array of char?

    Ben Fitzgerald, Jun 27, 2003, in forum: C Programming
    Replies:
    1
    Views:
    6,601
    Micah Cowan
    Jun 28, 2003
  5. Replies:
    39
    Views:
    357
    Öö Tiib
    Apr 8, 2014
Loading...

Share This Page