Unwanted characters keep appearing

This is a discussion on Unwanted characters keep appearing within the C++ Programming forums, part of the General Programming Boards category; Consider the following program. Why is it that when the statement gets(str) is executed, the character pressed earlier automatically appears ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    79

    Unwanted characters keep appearing

    Consider the following program. Why is it that when the statement gets(str) is executed, the character pressed earlier automatically appears on the screen? Furthermore, is there a way to prevent this?

    Code:
    #include<stdio.h>
    #include<iostream.h>
    #include<conio.h>
    int main()
    {
       char str[50],choice;
       choice=char(getch()); cout<<choice<<endl;
       gets(str); getch();
       return(0);
    }
    Compiler:Borland C++ 5.5

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: Unwanted characters keep appearing

    Originally posted by sundeeptuteja
    cout<<choice<<endl;
    It seems to me you're printing it, so why shouldn't it show up?
    If you mean something else, reply!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    But why should it appear twice? That is what is happening. Please try executing the code posted, you'll see what I mean.

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    >> Please try executing the code posted, you'll see what I mean.

    I can't. The getch() function is not ANSI C (it's not supported by my compiler).

    >> But why should it appear twice?

    Because the first is printed by the operating system (console window)

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    I'll try to explain exactly what is happening.
    First the statement is
    choice=char(getch()); There is no output at this stage.
    Now, the statement executed is cout<<choice<<endl; Assume that the character you pressed is a. The output will be the character a, and the cursor will go to the next line.
    Now comes the problem. The next statement being executed is gets(str); But even before the user starts to enter str, the character a automatically appears on the screen, as if it were the first character of str. Now why does that happen?

  6. #6
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Doesn't do that with MSVC..
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    it doesn't do that with mingw 3.2 or borland bcc55 with me.

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    This is quite interesting. It is definitely happening on Dev C++ and Borland C++. Perhaps the following code will help...
    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<stdio.h>
    int main()
    {
       char str[50],choice;
       choice=char(getch()); cout<<endl; gets(str);
       getch(); return(0);
    }
    Everyone should agree that this program should not give any output, except for the new line character, after hitting a key only once. Please try running it one more time. If it still works the way it is expected, please let me know. Absolutely nothing should appear on the console window.

    Compiler:Borland C++ 5.5
    Last edited by sundeeptuteja; 02-26-2003 at 06:57 AM.

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Still works as expected with Borland BCC55.

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    I tested your code with MSVC++ 6.0, Borland 5.5, and DevC++.
    In DevC++, apparently getch() is not executed until it receives a newline, so the keyboard input is showing up until that happens. With both other compilers, getch() executes immediately with no keyboard input appearing on the screen (which I guess is what it's supposed to do), but in your first code segment, you follow it with cout<<choice, so ALL of the compilers output the character that was entered there.
    Also, all three compilers display any characters that are entered when it pauses for gets(str).

    In the second code segment, you eliminated the cout<<choice, so the only output from the Borland & MS compiled programs are the characters that are entered for the gets(str). DevC++ displays all characters that are entered for both getch() and gets(str).

    In fact, DevC++ seems to allow any number of characters to be entered for the getch(). Any extra ones are grabbed by the gets(str), so if you enter extra characters at the first pause (for getch()), it doesn't pause for the gets(str) since there are characters and a newline already sitting in the buffer.

    By the way, in this line:

    choice=char(getch()); // why put char here? Isn't choice=getch(); equivalent?

  11. #11
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    Thank you, that was helpful. So what you are basically saying is, the input for getch will always appear as the input for gets, in this case. Now for the next question, is there any way to prevent this? Please let me know.

    choice=char(getch()); // why put char here? Isn't choice=getch(); equivalent?
    I have used type casting here because getch is supposed to return an int value. Without this statement I would get a warning.

    Compiler : Borland C++ 5.5
    Last edited by sundeeptuteja; 03-01-2003 at 02:02 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Replies: 10
    Last Post: 07-10-2008, 04:45 PM
  3. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 07:05 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  5. Unwanted space inbetween characters
    By SAMSAM in forum Windows Programming
    Replies: 4
    Last Post: 01-26-2003, 12:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21