Thread: getch problem

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    56

    getch problem

    I have spen a lot of time trying to figure this out, but I can't get it through my head.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream.h>
    #include <windows.h>
    #include <wininet.h>
    #include <time.h>
    #include <conio.h>
    
    //don't question the amount of includes please
    
    int main()
    {
    
    	int c;
    	FILE *fp;
    	
    
       if ((fp = fopen("C:\\Documents and Settings\\Famliy\\Desktop\\loggerkey.txt", "wb")) == NULL)
       {
    	   printf("Cannot open file for writing");
    	   exit(EXIT_FAILURE);
       } 
    
    
      while (c = getch() != 'l')
       {
    	  putchar(c);
    	  fputc(c, fp);
       }
       fclose(fp);
    
       return 0;
    } // main

    Now, when I run this, everytime I press a key it is recorded as a "smiley face" character. And in the file, it is recorded as that vertical rectangle character (it kind of looks like this: []). Now I am wondering, why isn't getch recognizing the right character?
    Last edited by johnchain; 08-17-2005 at 12:48 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    while (c = getch() != 'l')
    Use parentheses to say what you mean.
    Code:
    while ((c = getch()) != 'l')
    You had written this:
    Code:
    while (c = (getch() != 'l'))
    [edit]
    //don't question the amount of includes please
    I won't question the amount, but I will question a C++ header for C code.
    Last edited by Dave_Sinkula; 08-17-2005 at 12:50 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    56
    Thank you, it would have taken me hours to find that. I thought I already fixed the parentheses problem, guess not. Thanks for the fast post.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > //don't question the amount of includes please
    Why not?
    You've used C and C++ headers
    You've used DOS and windows headers
    No wonder there's confusion all around.

    > loggerkey.txt"
    I draw your attention to the forum rules about keyloggers and other malware.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  2. Problem with my file opener
    By kzar in forum C Programming
    Replies: 7
    Last Post: 04-20-2005, 04:20 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Problem with getch() and fgetc()
    By Krupux in forum C Programming
    Replies: 3
    Last Post: 10-01-2002, 11:38 PM