Thread: Malfunctioning Getch

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    13

    Malfunctioning Getch

    Would anyone here know why, when calling getch() from the conio library to pause the program until the user hits enter, that that line of code would not work?

    For some reason, in my program the getch has done just that - I can't think of any reason why it shouldn't work, and neither can my teacher. We are both baffled as to why the getch isn't functioning normally.

    I hesitate to post the code, because, I am not sure how much of the code would be required, and to put it all into a single post would be a bit much.

    Thanks to anyone for help!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I hesitate to post the code, because, I am not sure how much of the code would be required, and to put it all into a single post would be a bit much.
    Post the smallest and simplest compilable code that demonstrates the problem. In other words, this code should be as simple as possible, but no simpler.

    Also, especially since conio (and hence getch) is non-standard, you should post what compiler you are using.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > that that line of code would not work?
    Maybe it read the \n left behind by some previous call to scanf()

    Or do as laserlight says and post a small complete example showing the problem.
    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.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I can make an educated guess and suggest that the input stream has some data still in it by the time you reach your program's end and call the function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    1. Is this compiled for C++ or C? Because the include directives become different.

    2. What's the compiler. Visual C++ 2005, at least, doesn't supply the conio library and also deprecated getch().

    Regardless, if you are compiling for C++, there's no reason why you should use getch().

    [EDITED]
    the above in grey is not true. Sorry. It includes the conio library, but deprecated getch() in favor of _getch()
    Last edited by Mario F.; 06-01-2006 at 08:30 AM.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    Well, for the compiler we're using Borland C++ version 5.02. The getch I found in one of the graphics sections of our book (Fundamentals of C++: Understanding programming and problem solving, Kenneth A. Lambert & Douglas W. Nance) and turned out to be a more practical use to stop and display data as opposed to the only other method we know - stopping for input

    In the book (just in case anyone knows it), it's pg. 488 #8 that we're doing. The sorting of the array was taken from another program; I know that code works fine, there should be nothing wrong with it.

    So here are the two other functions that might be of some use; where the data is displayed and the input function (someone said something about that might be the cause)

    Code:
    void input(double scores[], int length)
    {  cout << "Welcome to the judge's auto-scorer!" << endl;
    	cout << "When prompted, please enter each score." << endl << endl;
    
    	for (int lcv = 0; lcv <= length; ++lcv)
    	{	cout << "Please enter score #" << (lcv + 1) << endl;
       	cin >> scores[lcv];
       }
    }
    Code:
    void average_scores(double scores[], int length)
    {  double sum, average;
    
    	scores[0] = 0;
    	scores[length] = 0;
       sum = 0;
    
       for(int m = 0; m <= length; ++m)
       	sum = sum + scores[m];
    
       average = sum / (length - 1);
    
       cout << endl << "The average score given is: " <<
       	setiosflags(ios::showpoint|ios::fixed) << setprecision(2) << average << endl;
       getch();
    }
    Tried switching the getch out of the function to the int main after calling the function, but still nothing. I'm so confused...

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void input(double scores[], int length)
    Well if you have

    double scores[5];
    input( scores, 5 );
    average_scores( scores, 5 );

    Then both of your functions run off the end of the array by 1 element, causing who knows what damage to your program.
    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.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    So... then, if I had declared the array as such:

    const int MAX_LIST_SIZE = 7;

    typedef double list_scores[MAX_LIST_SIZE];
    list_scores scores;

    then set int length = MAX_LIST_SIZE, it would still be running off, wouldn't it?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for (int lcv = 0; lcv <= length; ++lcv)
    Yes,
    To loop over an array, its <, not <=

    for (int lcv = 0; lcv < length; ++lcv)
    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.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    Ok, so now I enter that, and get the input to work like it should - except it only reads in seven numbers, where I want eight, but then on the next run it gives me the following error: Error writing output file. WTF?

    I'm just going to put the entire program, because I have NO clue which part of it is going wrong and how the other parts could affect it.

    Code:
    #include <iostream.h>
    #include <iomanip.h>
    #include <conio.h>
    
    const int MAX_LIST_SIZE = 7;
    
    typedef double list_scores[MAX_LIST_SIZE];
    list_scores scores;
    
    void input(double scores[], int length);
    void sort(double scores[], int length);
    void average_scores(double scores[], int length);
    int find_minimum(double scores[], int first, int length );
    void swap(double &X, double &Y);
    
    int main()
    {  int length = MAX_LIST_SIZE;
    
     	input(scores, length);
       sort(scores, length);
       average_scores(scores, length);
    	return 0;
    }
    
    void sort(double scores[], int length)
    {	int min_index = 1;
    
    	for (int j = 0; j < length - 1; ++j)
       {	min_index = find_minimum(scores, j, length);
       	if (min_index != j)
          	swap(scores[j], scores[min_index]);
       }
    }
    
    void input(double scores[], int length)
    {  cout << "Welcome to the judge's auto-scorer!" << endl;
    	cout << "When prompted, please enter each score." << endl << endl;
    
    	for (int lcv = 0; lcv < length; ++lcv)
    	{	cout << "Please enter score #" << (lcv + 1) << endl;
       	cin >> scores[lcv];
       }
    }
    
    void average_scores(double scores[], int length)
    {  double sum, average;
    
    	scores[0] = 0;
    	scores[length] = 0;
       sum = 0;
    
       for(int m = 0; m < length; ++m)
       	sum = sum + scores[m];
    
       average = sum / (length - 1);
    
       cout << endl << "The average score given is: " <<
       	setiosflags(ios::showpoint|ios::fixed) << setprecision(2) << average << endl;
       getch();
    }
    
    int find_minimum(double scores[], int first, int length)
    { 	int min_index = first;
    
    	for (int k =  first + 1; k < length; ++k)
       	if (scores[k] < scores[min_index])
          	min_index = k;
    
       return min_index;
    }
    
    void swap(double &x, double &y)
    {  double temp = x;
    	x = y;
       y = temp;
    }
    The Error writing output File happens at the end of the code, after the swap function.

    Apparently, programs have chosen to hate me as of yesterday...

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Please change your headers to something standardized where you can.
    Code:
    #include <iostream>
    #include <iomanip>
    #include <conio.h>
    If you take the time to use stuff in the standard namespace, people will thank you for it later. Now your program actually uses well documented libraries.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > except it only reads in seven numbers, where I want eight
    Well Duh!!!
    const int MAX_LIST_SIZE = 8;
    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.

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    Ok, so I use 8 for the array size and almost every problem goes away - except it still doesn't pause for the getch.

    At least we're back to square one! It was doing some wierd things to me on Friday :/

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Stop using getch() and use something from iostream instead.

    It's an old DOS-type function (and a C function IIRC), which is likely to mean that it isn't synchronised with iostream at all.
    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.

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    So then, other than cin, is there any other functions that could pause the program for me? Having to stop it with cin seems, I don't know, kind of primitive... lol

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