Thread: getch() prob

  1. #1
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638

    getch() prob

    Getch() is not working to stop the console from cont. Is this a problem with borland?

    After adding conio.h to the greyscale sample prog and compile with borland the getch() does not stop the prog. And it does not continue to ask for input files .... the prog just exits. With out doing anything to the pic at all.

    Ideas? Thank you. meow.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    without code, all I can guess is that it's coded wrong.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Ideas?
    You probably nullified the antideuteron injector assembly's Heisenberg destabilizers and caused a cascading magneton collapse in the ambivulent bivationary falvebarms. If you post your code we can help you bypass the quantum phase-modulator arrays in the plasma conduits, thereby frequency-limiting the gravimetric fluctuations and hopefully inducing a soliton static-warp shield-harmonic attenuation grid over the triassic subresonance field.

    >Thank you. meow.
    You're welcome. nyan.
    My best code is written with the delete key.

  4. #4
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    You probably nullified the antideuteron injector assembly's Heisenberg destabilizers and caused a cascading magneton collapse in the ambivulent bivationary falvebarms. If you post your code we can help you bypass the quantum phase-modulator arrays in the plasma conduits, thereby frequency-limiting the gravimetric fluctuations and hopefully inducing a soliton static-warp shield-harmonic attenuation grid over the triassic subresonance field.
    what is sad is that I actually understood that.

    Code:
    /* greypic.cpp */
    
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <conio.h>
    
    
    /*-------STRUCTURES---------*/
    typedef struct {int rows; int cols; int bits; unsigned char* data;} sImage;
    
    /*-------PROTOTYPES---------*/
    long getImageInfo(FILE*, long, int);
    void copyImageInfo(FILE*, FILE*);
    void copyColorTable(FILE*, FILE*, int);
    
    int main()
    {
      FILE         *bmpInput, *bmpOutput;
      sImage      originalImage;
      unsigned char      someChar;
      unsigned char*   pChar;
      int         nColors;  /* BMP number of colors */
      long         fileSize; /* BMP file size */
      int         vectorSize; /* BMP vector size */
      int         r, c;       /* r = rows, c = cols */
      int           pixels[600][600];
      char imagein[30];
      char imageout[30];
      /* initialize pointer */
      someChar = '0';
      pChar = &someChar;
      printf("Enter input image : ");
      gets(imagein);
     
      printf("Enter output image : ");
      gets(imageout);
     
      /*--------READ INPUT FILE------------*/
      bmpInput = fopen(imagein, "rb");
      fseek(bmpInput, 0L, SEEK_END);
     
      /*--------DECLARE OUTPUT FILE--------*/
      bmpOutput = fopen(imageout, "wb");
    
      /*--------GET BMP DATA---------------*/
      originalImage.cols = (int)getImageInfo(bmpInput, 18, 4);
      originalImage.rows = (int)getImageInfo(bmpInput, 22, 4);
      originalImage.bits = (int)getImageInfo(bmpInput, 28, 4);
      fileSize = getImageInfo(bmpInput, 2, 4);
      nColors = getImageInfo(bmpInput, 46, 4);
      vectorSize = fileSize - (14 + 40 + 4*nColors);
    
      /*-------PRINT DATA TO SCREEN-------------*/
      printf("Width: %d\n", originalImage.cols);
      printf("Height: %d\n", originalImage.rows);
      printf("Bits per Pixel: %d\n", originalImage.bits);
      printf("File size: %ld\n", fileSize);
      printf("# Colors: %d\n", nColors);
      printf("Vector size: %d\n", vectorSize);
     
      copyImageInfo(bmpInput, bmpOutput);
      copyColorTable(bmpInput, bmpOutput, nColors);
    
      /*----START AT BEGINNING OF RASTER DATA-----*/
      fseek(bmpInput, (54 + 4*nColors), SEEK_SET);
    
      /*----------READ RASTER DATA----------*/
      for(r=0; r<=originalImage.rows - 1; r++)
      {
        for(c=0; c<=originalImage.cols - 1; c++)
        {
          /*-----read data, reflect and write to output file----*/
          fread(pChar, sizeof(char), 1, bmpInput);
          pixels[r][c] = *pChar;
          *pChar = 255 - *pChar;
          fwrite(pChar, sizeof(char), 1, bmpOutput);
        }
      }
     
      fclose(bmpInput);
      fclose(bmpOutput);
     
    
    
    
    
        getch();
    
    
    }
    
    /*----------GET IMAGE INFO SUBPROGRAM--------------*/
    long getImageInfo(FILE* inputFile, long offset, int numberOfChars)
    {
      unsigned char      *ptrC;
      long         value = 0L;
      unsigned char      dummy;
      int         i;
    
      dummy = '0';
      ptrC = &dummy;
    
      fseek(inputFile, offset, SEEK_SET);
    
      for(i=1; i<=numberOfChars; i++)
      {
        fread(ptrC, sizeof(char), 1, inputFile);
        /* calculate value based on adding bytes */
        value = (long)(value + (*ptrC)*(pow(256, (i-1))));
      }
      return(value);
    
    } /* end of getImageInfo */
    
    /*-------------COPIES HEADER AND INFO HEADER----------------*/
    void copyImageInfo(FILE* inputFile, FILE* outputFile)
    {
      unsigned char      *ptrC;
      unsigned char      dummy;
      int i;
      dummy = '0';
      ptrC = &dummy;
    
      fseek(inputFile, 0L, SEEK_SET);
      fseek(outputFile, 0L, SEEK_SET);
    
      for(i=0; i<=50; i++)
      {
        fread(ptrC, sizeof(char), 1, inputFile);
        fwrite(ptrC, sizeof(char), 1, outputFile);
      }
    }
    
    /*----------------COPIES COLOR TABLE-----------------------------*/
    void copyColorTable(FILE* inputFile, FILE* outputFile, int nColors)
    {
      unsigned char      *ptrC;
      unsigned char      dummy;
      int i;
    
      dummy = '0';
      ptrC = &dummy;
    
      fseek(inputFile, 54L, SEEK_SET);
      fseek(outputFile, 54L, SEEK_SET);
    
      for(i=0; i<(4*nColors); i++)  /* there are (4*nColors) bytesin color table */
      {
        fread(ptrC, sizeof(char), 1, inputFile);
        fwrite(ptrC, sizeof(char), 1, outputFile);
      }
    
    }
    as I said it was the greyscale sample code already posted.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Prelude View Post
    You probably nullified the antideuteron injector assembly's Heisenberg destabilizers and caused a cascading magneton collapse in the ambivulent bivationary falvebarms. If you post your code we can help you bypass the quantum phase-modulator arrays in the plasma conduits, thereby frequency-limiting the gravimetric fluctuations and hopefully inducing a soliton static-warp shield-harmonic attenuation grid over the triassic subresonance field.
    Hmm.... and all this time I was working on the deathray from the wrong angle. Appreciate the hint, but keep the deathray hints off the public forums! I want a head start!

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Borland's getch() works fine.

    Your error is before that, but the question is "where"?

    I'd try a quick "binary" placement of the getch() call to quickly nail down where the error is making your program quit.

    A quick confirmation of getch() on your machine/compiler, would be to just place it as the first command in your main(). Then you'd know that, at least.

    When you step through your program, (or have it run to the cursor via F4 key, how far does it get before it exits?

    As an aside, I'd really you used the standard idiom for traversing an array:

    Code:
    for (i = 0; i < ArraySize; i++)
    
    instead of:
    for (i = 0; i <= ArraySize - 1; i++)
    There's nothing wrong with your line of code's logic, but there WILL be a day or two, when you'll forget to add either the = sign, or the -1, and of course, the compiler will have no warnings for you.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    check for fopen failure and don't use gets use fgets see faq

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Also main has to return a value...

    Code:
        getch();
    
        return 0;
    }

  9. #9
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    i did not write the prog. it was the test prog of the greyscale thread example prog.

    changes made same prob. i think it is the compiler that is messed up. when getch was put at the start it still exited. after doing nothing. check the fgets please to see if that is correct.

    Code:
    /* greypic.cpp */
    
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <conio.h>
    
    
    /*-------STRUCTURES---------*/
    typedef struct {int rows; int cols; int bits; unsigned char* data;} sImage;
    
    /*-------PROTOTYPES---------*/
    long getImageInfo(FILE*, long, int);
    void copyImageInfo(FILE*, FILE*);
    void copyColorTable(FILE*, FILE*, int);
    
    int main()
    {
    
    getch();
    
    
      FILE         *bmpInput, *bmpOutput;
      sImage      originalImage;
      unsigned char      someChar;
      unsigned char*   pChar;
      int         nColors;  /* BMP number of colors */
      long         fileSize; /* BMP file size */
      int         vectorSize; /* BMP vector size */
      int         r, c;       /* r = rows, c = cols */
      int           pixels[600][600];
      char imagein[30];
      char imageout[30];
      /* initialize pointer */
      someChar = '0';
      pChar = &someChar;
      printf("Enter input image : ");
      fgets(imagein , sizeof(imagein) , bmpInput) ;
     
      printf("Enter output image : ");
      fgets(imageout , sizeof(imageout) , bmpOutput) ;
      
     
      /*--------READ INPUT FILE------------*/
      bmpInput = fopen(imagein, "rb");
      fseek(bmpInput, 0L, SEEK_END);
     
      /*--------DECLARE OUTPUT FILE--------*/
      bmpOutput = fopen(imageout, "wb");
    
      /*--------GET BMP DATA---------------*/
      originalImage.cols = (int)getImageInfo(bmpInput, 18, 4);
      originalImage.rows = (int)getImageInfo(bmpInput, 22, 4);
      originalImage.bits = (int)getImageInfo(bmpInput, 28, 4);
      fileSize = getImageInfo(bmpInput, 2, 4);
      nColors = getImageInfo(bmpInput, 46, 4);
      vectorSize = fileSize - (14 + 40 + 4*nColors);
    
      /*-------PRINT DATA TO SCREEN-------------*/
      printf("Width: %d\n", originalImage.cols);
      printf("Height: %d\n", originalImage.rows);
      printf("Bits per Pixel: %d\n", originalImage.bits);
      printf("File size: %ld\n", fileSize);
      printf("# Colors: %d\n", nColors);
      printf("Vector size: %d\n", vectorSize);
     
      copyImageInfo(bmpInput, bmpOutput);
      copyColorTable(bmpInput, bmpOutput, nColors);
    
      /*----START AT BEGINNING OF RASTER DATA-----*/
      fseek(bmpInput, (54 + 4*nColors), SEEK_SET);
    
      /*----------READ RASTER DATA----------*/
      for(r=0; r< originalImage.rows ; r++)
      {
        for(c=0; c< originalImage.cols ; c++)
        {
          /*-----read data, reflect and write to output file----*/
          fread(pChar, sizeof(char), 1, bmpInput);
          pixels[r][c] = *pChar;
          *pChar = 255 - *pChar;
          fwrite(pChar, sizeof(char), 1, bmpOutput);
        }
      }
     
      fclose(bmpInput);
      fclose(bmpOutput);
     
    
        getch();
    
     return 0;
    
    
    }
    
    /*----------GET IMAGE INFO SUBPROGRAM--------------*/
    long getImageInfo(FILE* inputFile, long offset, int numberOfChars)
    {
      unsigned char      *ptrC;
      long         value = 0L;
      unsigned char      dummy;
      int         i;
    
      dummy = '0';
      ptrC = &dummy;
    
      fseek(inputFile, offset, SEEK_SET);
    
      for(i=1; i< numberOfChars; i++)
      {
        fread(ptrC, sizeof(char), 1, inputFile);
        /* calculate value based on adding bytes */
        value = (long)(value + (*ptrC)*(pow(256, (i-1))));
      }
      return(value);
    
    } /* end of getImageInfo */
    
    /*-------------COPIES HEADER AND INFO HEADER----------------*/
    void copyImageInfo(FILE* inputFile, FILE* outputFile)
    {
      unsigned char      *ptrC;
      unsigned char      dummy;
      int i;
      dummy = '0';
      ptrC = &dummy;
    
      fseek(inputFile, 0L, SEEK_SET);
      fseek(outputFile, 0L, SEEK_SET);
    
      for(i=0; i<=50; i++)
      {
        fread(ptrC, sizeof(char), 1, inputFile);
        fwrite(ptrC, sizeof(char), 1, outputFile);
      }
    }
    
    /*----------------COPIES COLOR TABLE-----------------------------*/
    void copyColorTable(FILE* inputFile, FILE* outputFile, int nColors)
    {
      unsigned char      *ptrC;
      unsigned char      dummy;
      int i;
    
      dummy = '0';
      ptrC = &dummy;
    
      fseek(inputFile, 54L, SEEK_SET);
      fseek(outputFile, 54L, SEEK_SET);
    
      for(i=0; i<(4*nColors); i++)  /* there are (4*nColors) bytesin color table */
      {
        fread(ptrC, sizeof(char), 1, inputFile);
        fwrite(ptrC, sizeof(char), 1, outputFile);
      }
    
    }
    should the
    Code:
     dummy = '0';
    be a zero instead of an "Oscar"? <Oh>
    Last edited by kryptkat; 07-02-2007 at 08:40 AM.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    try this program:
    Code:
    #include <stdio.h>
    #include <conio.h>
    int main()
    {
        printf("Test\n");
        getch();
        return 0;
    }
    delete the executable you're running and then recompile and run the program. It could be that you're not running the executable you're compiling.

    Also, run it from the command line (ie start->run->command(enter))

  11. #11
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    Code:
    /* chtest.c */
    
    #include <stdio.h>
    #include <conio.h>
    int main()
    {
        printf("Test\n");
        getch();
        return 0;
    }
    ok that test worked. it printed test then waited. i del .exe .obj .tds and recompiled and ran.

    it failed. just exited with out doing anything.

    note. before posting question about code i did try fgetc() and fgetchar() replace getch() also did not work.

    borland command line tool is command line compiler.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    try compiling the code in the forum just to make sure we're working with the same code.

    barring that, try dynamically allocating this line:
    Code:
    int           pixels[600][600];
    it might be too big for the stack.
    check the fgets please to see if that is correct.
    it's correct. edit: see dwks' post
    Last edited by robwhit; 07-02-2007 at 12:15 PM.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    check the fgets please to see if that is correct.
    It's not correct. You need to strip the newlines from the strings. fopen() doesn't take kindly to "file.txt\n". (I speak from experience.)

    robwit's suggestion is a good one: try dynamically allocating pixels[][].
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    Two ways were tried with the dynamic memory allocation. I get “Invalid indirection in function main()”

    tried with new int [] and malloc() . What way did you mean?

    I copied one line at a time from greypic.cpp to the testch.c prog to see what line caused it not to work.
    When I got to “int pixels[600][600];” getch() stops pawsing and just exits to the command line cursor.
    meow how to malloc “pixels[600][600]” ?

    Code:
    /* greypic.cpp */
    
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <conio.h>
    
    
    /*-------STRUCTURES---------*/
    typedef struct sImage{int rows; int cols; int bits; unsigned char* data;} sImage ;
    
    /*-------PROTOTYPES---------*/
    long getImageInfo(FILE*, long, int);
    void copyImageInfo(FILE*, FILE*);
    void copyColorTable(FILE*, FILE*, int);
    
    int main()
    {
    
    
    
    getch();
    
    
    
      FILE         *bmpInput;
      FILE         *bmpOutput;
      sImage        originalImage;
      unsigned char  someChar;
      unsigned char *pChar;
      int         nColors;  /* BMP number of colors */
      long         fileSize; /* BMP file size */
      int         vectorSize; /* BMP vector size */
      int         r;
      int         c;       /* r = rows, c = cols */
    /*   int         pixels[600][600]; */
    
    /* dynamic memory allocation of pixels[][] */
    
      int * pixels;
    /*  pixels = new int [600][600] ; */
    
      pixels = (int *)malloc(sizeof(pixels)); 
      
    /* also tried   pixels = (int *)malloc(sizeof([600][600])); to see if that would work. */ 
    /* also tried   pixels = (int *)malloc(sizeof([1200])); to see if that would work. */ 
      int i;
    
      char imagein[30];
      char imageout[30];
    
      /* initialize pointer */
      someChar = '0';
      pChar = &someChar;
      printf("Enter input image : ");
      fgets(imagein , sizeof(imagein) , bmpInput) ;
    
    /* slash n strip added */
    
      for(i = 0; i < sizeof(imagein) ; i++ )
      {
        if ( imagein[i] == '\n' )
        {
            imagein[i] = '\0';
            break;
        }
      }
     
      printf("Enter output image : ");
      fgets(imageout , sizeof(imageout) , bmpOutput) ;
    
    /* slash n strip added */
    
      for(i = 0; i < sizeof(imageout) ; i++ )
      {
        if ( imageout[i] == '\n' )
        {
            imageout[i] = '\0';
            break;
        }
      }
      
     
      /*--------READ INPUT FILE------------*/
      bmpInput = fopen(imagein, "rb");
      fseek(bmpInput, 0L, SEEK_END);
     
      /*--------DECLARE OUTPUT FILE--------*/
      bmpOutput = fopen(imageout, "wb");
    
      /*--------GET BMP DATA---------------*/
      originalImage.cols = (int)getImageInfo(bmpInput, 18, 4);
      originalImage.rows = (int)getImageInfo(bmpInput, 22, 4);
      originalImage.bits = (int)getImageInfo(bmpInput, 28, 4);
      fileSize = getImageInfo(bmpInput, 2, 4);
      nColors = getImageInfo(bmpInput, 46, 4);
      vectorSize = fileSize - (14 + 40 + 4*nColors);
    
      /*-------PRINT DATA TO SCREEN-------------*/
      printf("Width: %d\n", originalImage.cols);
      printf("Height: %d\n", originalImage.rows);
      printf("Bits per Pixel: %d\n", originalImage.bits);
      printf("File size: %ld\n", fileSize);
      printf("# Colors: %d\n", nColors);
      printf("Vector size: %d\n", vectorSize);
     
      copyImageInfo(bmpInput, bmpOutput);
      copyColorTable(bmpInput, bmpOutput, nColors);
    
      /*----START AT BEGINNING OF RASTER DATA-----*/
      fseek(bmpInput, (54 + 4*nColors), SEEK_SET);
    
      /*----------READ RASTER DATA----------*/
      for(r=0; r< originalImage.rows ; r++)
      {
        for(c=0; c< originalImage.cols ; c++)
        {
          /*-----read data, reflect and write to output file----*/
          fread(pChar, sizeof(char), 1, bmpInput);
          pixels[r][c] = *pChar;
          *pChar = 255 - *pChar;
          fwrite(pChar, sizeof(char), 1, bmpOutput);
        }
      }
     
      fclose(bmpInput);
      fclose(bmpOutput);
     
    
        getch();
    
     return 0;
    
    
    }
    
    /*----------GET IMAGE INFO SUBPROGRAM--------------*/
    long getImageInfo(FILE* inputFile, long offset, int numberOfChars)
    {
      unsigned char      *ptrC;
      long         value = 0L;
      unsigned char      dummy;
      int         i;
    
      dummy = '0';
      ptrC = &dummy;
    
      fseek(inputFile, offset, SEEK_SET);
    
      for(i=1; i< numberOfChars; i++)
      {
        fread(ptrC, sizeof(char), 1, inputFile);
        /* calculate value based on adding bytes */
        value = (long)(value + (*ptrC)*(pow(256, (i-1))));
      }
      return(value);
    
    } /* end of getImageInfo */
    
    /*-------------COPIES HEADER AND INFO HEADER----------------*/
    void copyImageInfo(FILE* inputFile, FILE* outputFile)
    {
      unsigned char      *ptrC;
      unsigned char      dummy;
      int i;
      dummy = '0';
      ptrC = &dummy;
    
      fseek(inputFile, 0L, SEEK_SET);
      fseek(outputFile, 0L, SEEK_SET);
    
      for(i=0; i<=50; i++)
      {
        fread(ptrC, sizeof(char), 1, inputFile);
        fwrite(ptrC, sizeof(char), 1, outputFile);
      }
    }
    
    /*----------------COPIES COLOR TABLE-----------------------------*/
    void copyColorTable(FILE* inputFile, FILE* outputFile, int nColors)
    {
      unsigned char      *ptrC;
      unsigned char      dummy;
      int i;
    
      dummy = '0';
      ptrC = &dummy;
    
      fseek(inputFile, 54L, SEEK_SET);
      fseek(outputFile, 54L, SEEK_SET);
    
      for(i=0; i<(4*nColors); i++)  /* there are (4*nColors) bytesin color table */
      {
        fread(ptrC, sizeof(char), 1, inputFile);
        fwrite(ptrC, sizeof(char), 1, outputFile);
      }
    
    }
    Thank You all for the help. i know it was a cpp prog but i thought getch as c prob so here the prog was posted. most of
    the code looks c.

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
      int * pixels; pointer to int (aka array of int)
      int ** pixels; pointer to pointer of int (aka array of array of int)
    //use this:
    
      int ** pixels;
      pixels = malloc(600*600*sizeof(**pixels)); //no cast needed in C see faq
    if(!pixels)
    {
     printf("Error allocating memory\n");
      exit(1);
    }
    //if C++ use this
      int ** pixels;
      pixels = new int[600][600];
    if(!pixels)
    {
      cout<<"Error allocating memory" << endl;
      exit(1);
    }
    be sure to free(pixels) or delete [] pixels

    also, check for fopen failure
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    http://www.cplusplus.com/reference/c...ib/malloc.html
    http://www.cplusplus.com/reference/c...dlib/free.html
    Last edited by robwhit; 07-04-2007 at 05:59 AM. Reason: indirection problem fixed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 05:15 AM
  3. Difference between getch() and getchar()
    By codec in forum C Programming
    Replies: 4
    Last Post: 04-04-2004, 02:34 AM
  4. Clearing input buffer after using getch()
    By milkydoo in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2003, 11:04 PM
  5. Problems with getch()
    By GrNxxDaY in forum C++ Programming
    Replies: 14
    Last Post: 08-12-2002, 02:11 AM